Chapter 8: MacOS Only Commands

8.1 Finder integration

First open applications/utilities and drag the Terminal.app icon onto the Dock for easy access to the Terminal App from the Dock.

To open Terminal at a folder's path just drag the folder onto the Terminal icon in the Dock.

8.2 Open Stuff

Open any file, directory or program

open

8.2.1 Open a directory

Open the current directory in finder.

open .

Opens the parent directory in the finder.

8.2.2 Open an application

open -a calculator

-a flag tells MacOS to go to the application folder and find this application, then open it...

8.2.3 Open a file

Open a file with a specified application.

open -a textedit lorem_ipsum.txt

8.2.4 Combine commands with open

Combining commands with open can be powerful.

8.2.4.1 Example 1

cut two columns (2,6) from a tab delimited file and open numbers with those columns pasted in

cut -f 2,6 mock_tab_data.txt | open -fa numbers

8.2.4.2 Example 2

open man pages in preview...

man -t bash | open -fa preview

8.3 Clipboard integration

In MacOS there are two important clipboard Commands

One for copying to the clipboard

pbcopy

And one for pasting to the clipboard

pbpaste

N.B. pb stands for pasteboard

8.3.1 Piping results to a clipboard

Piping the results of a command to the clipboard as a way to quickly get results out of terminal

ls -lah | pbcopy

place the contents of a file onto the clipboard without opening the file

pbcopy < lorem_ipsum.txt

8.4 Screencapture

8.4.1 Screencapture whole screen

Take a screenshot of the entire screen and save it to the desktop.

screencapture -S ~/Desktop/screen.jpg

8.4.2 Screencapture a window

To take a picture of a window, and save it to the desktop.

screencapture -iW ~/Desktop/screen.jpg

8.4.3 Screencapture a selection

To take a screenshot of a section of the screen, and save it to the clipboard

screencapture -ic

To take a screenshot of the whole screen, and save it to the clipboard

screencapture -c

Use the -c flag, but don’t assign a file name or path and the image will be saved to the clipboard.

Now that it’s in your clipboard you can just paste it into Preview, Photoshop, Pages, or whatever else you want to use.

8.4.4 Screencapture with a timer

The Grab utility lets you take screenshots on a timer, so you can setup an app or situation on the screen and capture things like alert boxes, menus, button actions, etc. You can also specify a timed screenshot from the Terminal:

screencapture -T 10 timedshot.jpg

The -T flag needs to be followed by whatever amount in seconds you want to delay the screen shot by. In the above example, it’s 10 seconds which is also the default when using Grab.

Make a note that the capitalization of these flags matters, if you use a lowercase -t, you’ll be trying to specify a file type for the screenshot instead, like so:

8.4.5 Screencapture to a file type

screencapture -t tiff sample.tiff

You can select a variety of file types to export to, including .png, .pdf, .tiff, .jpg, and .gif.

8.4.6 Screencapture silent mode

If you plan on scripting out something with the screencapture command, you might not want the shutter sound to fire. To silently take a screen shot just use the -x flag:

screencapture -x quiet.jpg

This is a one time thing so you’ll have to always specify -x, it’s not a permanent change to make the screen shots silent.

8.4.7 Screenshot to application

Another neat trick is sending the screenshot directly to a new Mail.app message:

screencapture -M mailme.jpg

This takes the screenshot, saves it as mailme.jpg, then automatically opens a new Mail message with that screenshot attached to it.

8.4.8 Other screencapture options:

-i (interactive - brings up the crosshairs)

-m (main monitor only)

-C (show cursor)

-t (format (png, pdf, jpg, tiff))

-T (delay in seconds)

-P (open in preview)

-M (send to mail)

-c (send to clipboard)

8.4.9 A complex screen capture example

screencapture -mCP -T 3 -t jpg screen_cap.jpg

main monitor only, show cursor, open in preview, delay 3 seconds, save as jpg

8.5 Text to speech

8.5.1 Say a string

say 'unix is awesome'

8.5.2 Choose the voice

-v (voice option)

say 'unix is awesome' -v vicki

8.5.3 Read a file

say -f file.txt

8.5.4 Output audio as .aiff

say -f file.txt -o audio.aiff

8.5.5 Alert when actions complete

cp -R dir1 dir2; say 'Directory copy done'

8.6 Spotlight

8.6.1 Spotlight: searching metadata

8.6.1.1 mdfind

The metadata for a file contains much more information about the file than we normally have access to from the command line. As an example, let's say that you have an MP3 or a song that you've purchased on iTunes. Well that has a file name. It's probably the same name as the song and we can see that in the file system. We could search for it using find or grep, but it also has a lot more information associated with it. It has the artists name, the album name, the track number, the running time, the bit rate that it is encoded at.

8.6.1.2 Limit the search to a directory

use -onlyin

mdfind -onlyin ~/Library/ "search_word"

8.6.1.3 Exclude results with a term

use a minus sign before a search term to exclude

mdfind -onlyin ~/Library/ "search_word -xxx"

8.6.1.4 Search within a name

use the -name option

mdfind -onlyin ~/Library/ -name "new"

It will find the search term in the name, so "new" returns: new.txt newer.txt new_file.txt

8.6.1.5 Pipe search result to xargs

To remove the null characters from the result set and then pipe the result set into xargs use -0

mdfind -onlyin ~/Library/ -name "new" -0 | xargs -0 open

That will open the found files in the default app.

8.6.2 Spotlight: metadata attributes

8.6.2.1 mdls

mdls allows us to peek at the metadata associated with a file

mdls file.txt

This lists the keys and values for the metadata associated with that file, for instance a key might be: kMDItemFSName.

You you use that key in a search as follows...

mdfind -onlyin ~/Library/ 'kMDItemFSName == "lorem_ipsum"'

remember == is an exact match.

8.6.2.2 Search by time using mdls

mdfind -onlyin ~/Library/ 'kMDItemFSCreationDate >= $time.today'
8.6.2.2.1 options for mdls

$time.now $time.today $time.yesterday $time.week $time.this_month $time.this_year

8.6.2.2.2 provide an argument to these time functions

$time.now(-36000)

yields "now" minus 36000 seconds

$time.today(-2)

yields "today" minus 2 days