Mac OS

The best OS a parent's money can buy

  • Steve Jobs, probably

Keyboard Shortcuts

What you'll see below is incomplete, for up to date information, check Apple's documentation on keyboard shortcuts. Dan Rodney's compilation is a great source as well.

Finder

Shortcut Function
⌘ ⌫ Send file to trash
⌥ ⌘ ⌫ Completely delete a file
⌥ ⌘ ⇧ ⌫ Empty the trash
⌘ ⇧ N Create a new folder
⌥ ⌘ V Transfer copied file/folder to the current folder
⌘ ⇧ . Show hidden files/folders
⌘ E Eject selected disk/drive
Shortcut Function
⌘ ⇧ ] Switch to the next tab
⌘ ⇧ [ Switch to the previous tab
⌘ ] Go forward to the next page
⌘ [ Go back to the previous page
⌘ ` Switch to the previous windows of frontmost application
⌘ ⇥ Switch to the previous frontmost application
⌃ ↓ View all open windows for the current frontmost application
⌃ ↑ View all windows for all open applications

Sleep, Restart, Shut Down, Log Out

Document Editing

These work when editing text using Safari, Notes, and Mail, and any Mac OS application in general. They aren't arbitrary, they're the emacs hotkeys, and work on many applications, not just ones on Mac OS.

Shortcut Function
⌃ A Move to the beginning of the line
⌃ E Move to the end of the line
⌃ H Delete the character left of the the cursor
⌃ D Delete the character right of the cursor
⌃ U Delete all text left of the cursor
⌃ K Delete all text right of the cursor
⌃ O Add a newline without moving the cursor
⌃ T Swap the characters left and right of the cursor

Cursor Movement

If you're ever stuck without arrow keys (it could happen!) you can take advantage of these 4 shortcuts.

Shortcut Function
⌃ F Move cursor →
⌃ B Move cursor ←
⌃ P Move cursor ↑
⌃ N Move cursor ↓
Shortcut Function
⌘ , Open preferences window
⌘ ⇧ / Open help menu for current app
⌘ ⌃ F Open app in full-screen
⌥ ⌘ I Open inspect element
Shortcut Function
⌘ ↑ Open the parent folder
⌘ ↓ Open the file/folder
⌘ → Open the folder (list view)
⌘ ← Close the folder (list view)

Opening Folders

Shortcut Function
⌘ ⇧ G Go to folder
⌘ ⇧ D Open the Desktop folder
⌘ ⇧ O Open the Documents folder
⌥ ⌘ L Open the Downloads folder
⌘ ⇧ H Open the Home folder
⌘ ⇧ A Open the Applications folder
⌘ ⇧ R Open the AirDrop folder
⌘ ⇧ I Open the iCloud folder

Adjusting the Finder Window

Shortcut Function
⌘ ⌥ P Show/hide the path bar
⌘ ⌥ S Show/hide the side bar
⌘ ⌥ T Show/hide the toolbar
⌘ ⇧ T Show/hide the tab bar
⌘ / Show/hide the status bar
⌘ ⇧ P Show/hide preview of selected file
⌘ 1 View folder's items as icons
⌘ 2 View folder's items as list
⌘ 3 View folder's items as columns
⌥ ⌘ ⌃ 1 Sort files by name
⌥ ⌘ ⌃ 2 Sort files by type
⌥ ⌘ ⌃ 4 Sort by date added
⌥ ⌘ ⌃ 5 Sort by date modified

Safari

Shortcut Function
⌘ ⇧ \ Show all tabs
⌘ L Select the current page's URL
⌘ ⇧ R Open in reader-view
⌘ ⇧ I Mail link of current page
⌘ D Add current page to bookmarks
⌘ ⇧ D Add current page to reading list
⌥ ⌘ L Open the downloads folder
⌥ ⌘ E Reset the cache
⌘ ⇧ C Start/stop select element
⌥ ⌘ C Open JavaScript Console
⌥ ⌘ I Open/close inspect element (also closes JavaScript console)
⌥ ⌘ R Hard refresh page

Mail

Shortcut Function
⌘ ⇧ N Get new messages
⌘ N Write new message
⌥ ⌘ F Search mail for message
⌘ ⇧ D Send message
⌥ ⌘ J Empty junk mailbox
⌘ ⇧ ⌫ Empty trash mailbox

Screenshot directly from terminal

There's a hidden terminal command screencapture that you can use to capture a screenshot of your current window.

Start screen recording from the terminal

You can even use screencapture to record video. The -V flag specifies to capture a video and the -A flag specifies to capture audio as well.

Change default screenshot location

I like to put my screenshots in the ~/Downloads directory. Luckily there's a terminal command for that.

Change default screenshot filetype

By default, screenshots save as PNG files, but you can change this

Audio

Custom Sound Effects

The default system alert sounds are located in /System/Library/Sounds, and you can place your own custom alert sounds in the ~/Library/Sounds directory. Your custom alert sound must be an AIFF file (e.g. example.aiff).

If you'd like to discover sounds on your computer, use a recursive glob to discover audio files within the system library

print -l /System/Library/**/*.aiff

For instance, to play a sound, use the afplay command, and provide it the sound file you'd like to play. For example, the command below plays the default alert sound on macOS

afplay '/System/Library/Sounds/Tink.aiff'

Move cursor with the mouse in terminal

This is a cool trick I just discovered. Start typing out a command on the terminal, and then hold ⌥

Now try clicking on a different part of the command you're entering. It will move the cursor to that location!

Update Software in the Terminal

You can actually update the software on your computer directly from the terminal.

Fonts

Disable User Photo

Setting Keyboard Shortcuts from Terminal

Store your keyboard shortcuts in the directory ~/Library/KeyBindings/DefaultKeyBinding.dict. Note that you have to first create ~/Library/KeyBindings since it won't exist at first.

Fix Permissions

If you mess up your /etc files, like I did when I accidentally made every file executable, you can run this command to reset all of the permissions.

If you mess up your /etc/sudoers file, you'll have to boot into single-user mode with ⌘ S during power on. From there, you can enter the following commands.

Customize Launch Application for File Extension

The property list file that controls which app launches for a particular filetype is located deep in Launch Services.

Default Application

You can install the command duti on homebrew to configure the default application that opens when you click on a file with a particular file extension. I've included an example below:

Convert SVG to PNG

Install librsvg, which is built by GNOME

If you want to convert example.svg into a 1024x1024 PNG file, you have a few choices. These are ordered from best to worst.

avconvert

I'm going to write some notes about conversions between audio and video file formats here. Some useful commands will appear below, but first I want to give a breakdown of the ecosystem around audio, video, and image file formats.

First, the Moving Picture Experts Group (MPEG) is a group formed by ISO and IEC to standardize the way audio and visual data is compressed and transmitted across the internet. MPEG-4 is a method of defining compression of audio and visual (AV) digital data. It was introduced in late 1998 and designated a standard for a group of audio and video coding formats and related technology agreed upon by the ISO/IEC Moving Picture Experts Group (MPEG). It was introduced in late 1998 and designated a standard for a group of audio and video coding formats and related technology agreed upon by the ISO/IEC Moving Picture Experts Group (MPEG)

Convert MP3 to M4A

networksetup

The networksetup command allows you to configure your System Preference's Network settings directly from your terminal.

airport

There is a hidden command, called airport.

scutil

tmutil

Speeding Up Time Machine Backups

If you are having trouble because time machine is taking too long, there's a way to speed up the pace of the back-up.

The changes made in this command are temporary, they will automatically reset to their default state on their own once the computer has been rebooted.

Saving Local Snapshots

By default, Time Machine will create local snapshots, locally stored backup volumes when it can't connect to your Time Capsule.

Excluding Certain Files & Folders

By default, Time Machine will back up everything on your drive. You may, however, have data from large directories stored elsewhere. If so, it might be a good idea to exclude that directory from Time Machine's backups, in the interest of saving space.

You can also check if a given file, directory, or volume is excluded

It's pretty hard to reliably exclude things using the native CLI tools. I've heard that asimov is a great tool to reduce the size of time machine backups.

Create User from Command Line

Adding a new user to a Mac computer from a Terminal window requires you to define the user’s name, set a password, create the user’s home directory and configure their system permissions.

  1. Create a user named tommy

    dscl . create /Users/tommy
    
  2. Set tommy's shell to /bin/zsh

    dscl . create '/Users/tommy' UserShell '/bin/zsh'
    
  3. Set Tommy's have the home directory /Users/tommy

    dscl . create '/Users/tommy' NFSHomeDirectory '/Users/tommy'
    createhomedir -u tommy -c
    
  4. Set tommy's RealName to Tommy Trojan

    dscl . create '/Users/tommy' RealName 'Tommy Trojan'
    
  5. Give tommy a UID number, (for instance, 502)

    dscl . create '/Users/tommy' UniqueID 502
    
  6. Give tommy the primary group ID of 20 (the default for the staff group on macOS)

    dscl . create '/Users/username' PrimaryGroupID 20
    
  7. Give tommy the password fighton

    dscl . passwd /Users/tommy 'fighton'
    
  8. Add tommy to the list of user's that can be logged into with ssh

    dseditgroup -o edit -t user -a tommy com.apple.access_ssh
    
  9. Optionally add tommy to the list of admin users on the computer

    dseditgroup -o edit -d tommy -t user admin
    

Hidden Users

sysadminctl

fdesetup

Single User Mode

Power Sound

System Integrity Protection

If you need to make changes to the file system, it might require disabling system integrity protection. By default, the following directories are protected:

  1. /System
  2. /usr
  3. /bin
  4. /sbin
  5. /var

There are three directories that are not protected:

  1. /Applications
  2. /Library
  3. /usr/local

Configuring the Login Page

diskutil

Something learned from man hdiutil: /dev/rdisk nodes are character-special devices, but are "raw" in the BSD sense and force block-aligned I/O. They are closer to the physical disk than the buffer cache. /dev/disk nodes, on the other hand, are buffered block-special devices and are used primarily by the kernel's filesystem code. In layman's terms /dev/rdisk goes almost directly to disk and /dev/disk goes via a longer more expensive route

macOS Books

New to macOS Catalina is the Books application, which provides a cleaner interface for handling audiobooks on your Mac.

macOS Notes

Printing

Virtual Network Computing VNC

Virtual Network Computing, or VNC, is more commonly known as Screen Sharing, as this is the name of the application used for VNC on macOS. Your computer listens for VNC connections on port 5900, and you can use the <vnc://user@host.net> syntax to reach a particular user at a particular address.

Icons

You can find icons in some of the following locations:

/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/

You can also find some of the iOS icons in the following places

/System/Library/Extensions/IOSCSIArchitectureModelFamily.kext/Contents/Resources/
/System/Library/Extensions/IOStorageFamily.kext/Contents/Resources/

I've made a script that can convert an image into an icon

Filetype Conversion for Printing

Defaults

defaults is a powerful command. defaults a tool to utilize when exporting and importing settings across machines

You can prohibit the modification of Finder preferences by selecting Preferences... in the menu bar. As to why you'd want to do this, I have no idea, but here you go:

Similarly, you could prohibit the Go to folder action in Finder as well, for whatever reason

If you're tired of .DS_Store files popping up in all of your directories, there's a command to disable them entirely. Before you do however, it's worth nothing that the .DS_Store file is one that contains useful metadata about your file system browsing preferences, such as "Whether to open a folder in 'Application View' or in 'List View'"

TextEdit

Misc

QuickLook

Extending QuickLook Previews

Boolean Operators

Search Operators

author:
date:
created:
modified:
weather:
bitrate:
iso:
wiki:

Searching for messages in Mail

Spotlight by default searches through mail as well, so to find a message, you can retrieve it from Spotlight, without even opening the Mail application.

To learn more about searching the Mail app

The Apple support article that covers how to search for emails in Mail on Mac is not sufficiently comprehensive in my opinion, so I've added more concrete instances of valid search operators in the examples found below.

Type to:recipient@example.com to find all sent mail addressed to a specific recipient at a specific domain. The subject search operator limits the search results to the contents of email subject lines.

Type subject:cookie in the search field to return all emails with the word "cookie" in the subject line. Type subject:"cookie recipe" to find all mail with the phrase "cookie recipe" in the subject line. Type subject:cookie subject:recipe to find all mail with both "cookie" and "recipe" in any order in the subject line.

Spotlight Siri Knowledge

Siri uses natural language processing (NLP) to identify good matches for searches that don't use search operators. Some useful examples are included below:

Disable Spotlight Index of Network Drives

Safari Web Extensions

To convert Firefox web extensions into Safari web extensions, you'll need Xcode 12, and the command-line tools that accompany it

CalDAV Synchronization

You can control which calendars in your Google Calendar to share with iCloud by going to Google Calendar's page about sync settings

URL Schemes

Compressor

The Compressor application is part of the iWork premium set of apps, which includes applications like "Logic Pro" and "Final Cut Pro".


Other Useful Commands

AppleScript

You can trigger AppleScript from a web link using the applescript URL scheme.

For instance, this is the AppleScript to print hello world on a user's computer.

applescript://com.apple.scripteditor?action=new&script=display%20dialog%20%22hello%20world%22

Color Swatches

Screen Capture

I recommend reading Apple's article Take a screenshot on your Mac. I learned something I didn't know. You can hold the ⌥ modifier key while taking a screenshot of a window to prevent the drop shadow from appearing!

Personally, I think the drop shadow is a good touch. If, however, you prefer that drop shadows be disabled by default when taking a screen capture of an application window, you can use the following command.

FaceTime

When you're on a FaceTime audio call, or any phone call for that matter, your desktop will be presented with a small window in the top-right corner. That window can be bothersome, as it floats on top of of whatever applications you're using. I discovered how it can be hidden:

AirDrop

You can add AirDrop to the dock by visiting the following directory.

/System/Library/CoreServices/Finder.app/Contents/Applications

From there, click and drag the AirDrop.app icon to your dock

You can also drag the iCloud.app icon to your dock as well.

Launchpad

You can change the Launchpad icon grid layout, but it requires some defaults write commands, provided below.

Feedback Assistant

The Feedback app is available on all iOS devices, even if they aren't running a beta. devices.

To get to it, open up Safari and navigate to the URL applefeedback://

xattr

xcode-select

sips

Mac has an inbuild tool for editing images called sips, which is an abbreviation for scriptable image processing system. You can learn about it here, but I encourage you to check out man sips and sips --help as well.

Daemons, Agents, and Automation

daemon : A computer program that runs as a background process.

agent : A program that acts on behalf of another program, or on behalf of another user.

user agent : a daemon specific to a logged-in user that only executes while that user is logged in.

process identifier : a number used by most operating system kernels to uniquely identify an active process.

The original daemon is init, which has process identifier #1 because it is the first process started during the booting of the computer system.

macOS has a more robust alternative to cron which allows daemon processes to be triggered on a systematic fashion. Using the launchctl (launch control) command, you can create daemon processes that will automatically run by the system.

These files take the form of plist files and are found in several system directories:

If you write or install personal plist files, they will ideally go in the home directory. A good example file to begin understanding the syntax can be located in /System/Library/LaunchDaemons/ssh.plist, which is the file used to launch the ssh daemon server that listens for incoming ssh connections.

Specifying privacy-sensitive files and folders in a launchd property list might not work as expected and prevent the service from running. Having Program or Program Arguments pointing to an executable in a privacy sensitive location is currently allowed, but may be restricted in a future release.

To comply with the new privacy protections, resources for a launchd service must be stored in locations that aren't privacy sensitive. If necessary, the app can set up resources during its execution rather than using launchd property list keys, making it possible to grant the app access using System Preferences > Security & Privacy > Privacy. The following launchd property list keys are affected: Keep Alive, Path State, Queue Directories, Sockets, Sock Path Name, Standard Error``Path, Standard In Path, Standard Out Path, and Watch Paths.

If you're setting up processes in /Library/LaunchDaemons, which are run as the root user, then make sure to set the file permissions appropriately using chown root:wheel <file> and chmod 644 <file>

launchctl has changed for the better in 10.11.4

Type the command without arguments to get the help. You'll see new domains to search and new commands.

For your processes: if it's not in the system domain it's probably in the domain of that particular user.

However, since you'll be logged into the GUI, you might want to check the domain of the GUI, which is kept separate.

To learn more, read man launchctl or read Apple's article about Creating Launch Daemons and Agents


I wasn't sure where the best place to put this is, but it will link the latest version of GCC installed by Homebrew into /usr/local/bin, while also removing the version number suffixed to the filename by Homebrew's installation formula.

# Choose the directory containing the latest version of GCC
# as indicated by the highest number suffixed to 
# the filepath of the package directory
print -v version /usr/local/opt/gcc@<->(n[-1])
version=${version#*@}
for file in /usr/local/opt/gcc@${version}/bin/*-${version}(*); do
    tail=${file:t}
    ln -sf ${file} /usr/local/bin/${tail%-*}
done

If symbolic links are overkill for you, then check this out. The snippet below will hash the executable corresponding with each file, which zsh will expand each command to when called, and will do this up until the next point that the path is modified by the shell. At that point, these hashed commands will be rehashed, meaning the location specified in the hash command's specified directory will have its executable overwritten and replaced by the executable in the foremost directory in the path containing an executable with a matching name.

print -v version /usr/local/opt/gcc@<->(n[-1])
version=${version#*@}
for file in /usr/local/opt/gcc@${version}/bin/*-${version}(*); {
    tail=${file:t}
    hash ${tail%-*}=${file}
}

iMessage

This isn't written down anywhere, so I'm writing it here: You can send message effects on the macOS Messages application with a keyboard shortcut: ⌘ ⇧ A