macOS/BestPractices

macOS Best Practices

Shotcuts

Screenshot

Screenshot Shotcuts

  • Press Shift-Command-3 to take a screenshot of your entire screen
  • Press Shift-Command-4 to take a screenshot of a selected portion of your screen
  • Find the screenshot as a .png file on your desktop or custom folder
  • Press Ctrl-Shift-Command-4 to do as Shift-Command-4 does, but save screenshot to clipboard

Screenshot Settings

  • Location
$ defaults write com.apple.screencapture location $HOME/Pictures/Screenshots
$ killall SystemUIServer
  • File Format
$ defaults write com.apple.screencapture type <format>
$ # available formats: [png , pdf, jpg, jp2 (JPG2000), gif, tif (TIFF), bmp, pict, tga]
  • Disable-shadow
$ defaults write com.apple.screencapture disable-shadow -bool true

Password

Store passphrase of ssh identities in the user’s keychain

$ ssh-add -K

Login and Startup

Login Items

System Preferences -> Users & Groups -> Login Items

Launch Daemons and Agents

Apple provides mechanism for lanuching items automatically: Creating Launch Daemons and Agents.

Launchd loads .plist documents (XML format) in the following locations:

  • /Library/LaunchDaemons and /System/Library/LaunchDaemons: load when your Mac starts up and run as the root user
  • /Library/LaunchAgents and /System/Library/LaunchAgents: load when any user logs in, and run as that user
  • /Users/$USER/Library/LaunchAgents: load only when $USER logs in, and run as that user

Example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>EnvironmentVariables</key>
  <dict>
    <key>PATH</key>
    <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    <key>HOME</key>
    <string>/Users/$USER</string>
  </dict>
  <key>StandardOutPath</key>
  <string>/Users/$USER/.unison/unison-launchd.log</string>
  <key>StandardErrorPath</key>
  <string>/Users/$USER/.unison/unison-launchd.log</string>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>unison.launch</string>
  <key>Program</key>
  <string>/usr/local/bin/unison</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/unison</string>
    <string>config_name</string>
  </array>
  <key>StartInterval</key>
  <integer>30</integer>
</dict>
</plist>

Load config without restarting:

$ launchctl load ~/Library/LaunchAgents/my-startup.plist

Stop a launch item without restarting:

$ launchctl unload ~/Library/LaunchAgents/my-startup.plist

Case Sensitive Volume

Create a case-sensitive disk image

$ hdiutil create -verbose -type SPARSEBUNDLE -fs 'Case-sensitive Journaled HFS+' -size 100g -volname Unix ~/Unix.sparsebundle
$ hdiutil attach ~/Unix.sparsebundle
$ open ~/Unix.sparsebundle

Mount the case-sensitive disk image

Two ways of mounting the case-sensitive disk image:

  • Add ~/Unix.sparsebundle to Login Items
  • Create /Library/LaunchDaemons/Unix.sparsebundle.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
          <key>RunAtLoad</key>
          <true/>
          <key>Label</key>
          <string>system.dmg.mount.Unix.sparsebundle</string>
          <key>ProgramArguments</key>
          <array>
              <string>hdiutil</string>
              <string>attach</string>
              <string>/Users/${USERNAME}/Unix.sparsebundle</string>
          </array>
    </dict>
</plist>

Applications

Get the bundle identifier of an macOS application

$ osascript -e 'id of app "Finder"'