Finder/Terminal tools

When I’m working at my Mac, some things are most effectively done using the Finder’s GUI and some are best done through the command line in the Terminal.1 I switch between the two with the help of a handful of simple automations. I know I’ve written about one of them before, but I don’t think I’ve written about the others. Certainly not as a collection. So here are four little tools I use to go back and forth between the Finder and the Terminal.

open .

The open command is one of Apple’s (originally NeXT’s) great contributions to the integration of the command line and the GUI (the others are pbcopy and pbpaste). It can be used to open documents, but I use it most often this way:

open .

This command, when invoked in the Terminal, activates the Finder and opens a window to the current working directory (that’s the dot). It’s smart enough to know if there’s already a Finder window showing that directory; if so, it just brings that window to the front instead of creating a new one.

Terminal Here

The converse of open . is a Keyboard Maestro macro I wrote called Terminal Here. When the Finder is active, invoking Terminal Here via the ⌃⌥⌘T keyboard shortcut opens a new Terminal window and sets its working directory to the one shown in the frontmost Finder window. Because it was written by me, not Apple, it isn’t as smart as open . in that it will open a new Terminal window even there’s already one open and set to that directory. In the many years I’ve used this macro, I haven’t found that lack of intelligence a problem, so I haven’t put any effort into making it smarter.

You can download the macro or just build it yourself. Here’s what it looks like in the Keyboard Maestro editor:

KM Terminal Here

It’s just a single step that runs this AppleScript:

applescript:
1:  tell application "Finder"
2:    tell front Finder window
3:      set myPath to POSIX path of (target as alias)
4:    end tell
5:  end tell
6:  
7:  tell application "Terminal"
8:    activate
9:    do script "cd " & quote & myPath & quote
10:  end tell

The first stanza, Lines 1–5, gets the path to the directory shown in the front Finder window. The second stanza, Lines 7–10, activates Terminal and cd’s into that directory. Pretty simple, and you can easily change the second stanza to work with iTerm if that’s your terminal emulator of choice.

Update 16 Sep 2024 7:27 PM
Leon Cowle pointed out on Mastodon that if you have the path bar visible in your Finder windows (as I do—you can see it in a screenshot further down in the post), you can right-click (or control-click or two-finger click) on the icon or folder name down there and a context menu will appear with Open in Terminal as one of the items. Choosing it will do what Terminal Here does. This is part of macOS—going back to OS X, I think—so you don’t need a utility like Keyboard Maestro to open a Terminal window this way. Also, you can do the same thing with any of the folders shown in the path bar.

I won’t be using this myself, as I find it easier to type ⌃⌥⌘T than to invoke a context menu, but it’s always nice to know things like this. Thanks, Leon!

;dir

Let’s say I’m working in the Terminal and I want to change to a new directory. That’s what the cd command is for, of course, but typing the path to the new directory can be tedious—even when taking advantage of tab completion. If you have a Finder window open to the directory you want to change to, Apple has a cute way around the tedium: drag the proxy icon from the Finder window’s title bar into the Terminal after typing cd and the path will be inserted at the caret.

What’s the proxy icon? It’s the little picture of a folder that appears next to the window’s title.

Finder title bar with proxy icon

This icon used to be a permanent part of the Mac’s title bars but was stupidly changed in Big Sur to be a peekaboo feature—it would normally be hidden but would appear if you put the mouse pointer over the title and waited a bit. The following year, Apple showed that it sometimes does listen to reason by adding an Accessibility preference that lets you make proxy icons permanently visible.

Anyway, while I like the idea of dragging proxy icons, it isn’t especially efficient. So I created a TextExpander snippet, triggered by typing ;dir, that would insert the path of the frontmost Finder window. When I switched from TextExpander to Typinator, I migrated it over. Both TextExpander and Typinator (and other utilities like them) have an option for running an AppleScript when an abbreviation is typed. Here’s the AppleScript that’s run when I type ;dir:

applescript:
tell application "Finder" to get quoted form of POSIX path of (target of front Finder window as alias)

It’s kind of long because of AppleScript’s “this of that of the other” syntax.

Update 16 Sep 2024 7:27 PM
As a follow-on to the update in the Terminal Here section, the context menu that pops up when you right-click on an element in the path bar has an item named Copy folder name as Pathname, where folder name is the name of the current folder. This will put the full path onto the clipboard, so you can paste it anywhere. I prefer using ;dir because it’s faster and keeps me in the Terminal, but it’s nice to know other options.

sel

The previous three automations are really simple, and I use them quite often. This last one was the most difficult to build, and I rarely use it. But when I do use it, it’s very handy. It’s a shell script named sel that’s meant to be run at the end of a pipeline. It expects a list of files to be passed into it via standard input and then opens a Finder window in the current working directory and selects those files. For example,

ls *.png | sel

will activate the Finder, open a Finder window in the Terminal’s working directory, and select all the files that end with .png.

The idea behind sel is that I want to use the Finder to do something with a bunch of files, but it’s easier to select the files with a shell command than it is by using the GUI. Suppose, for example, I have a directory filled with photos. The name of each JPEG file starts with the date on which the photo was taken in yyyymmdd format. If I want to select all photos taken on a certain date (perhaps because I’m going to attach them to an email or copy them to another folder) I could drag or shift-click my way through them, but it’s faster and more accurate to run

ls 20240915*.jpg | sel

and immediately see the results:

Finder window with several files selected

(If you’re wondering why all these JPEG files are zero bytes long, it’s because I made them using the touch command specifically for this example.)

I probably wouldn’t use sel if I had only ten files to select, but definitely would if I had dozens or hundreds.

By the way, I’m not thrilled with the sel name. I’d rather it were select, but there’s already a built-in bash command named select. For a while, I called my script fselect but found that I was forgetting the initial f. Time will tell if this shorter name sticks with me.

Oh, you want to see the code for sel? Here:

bash:
 1:  #!/bin/zsh
 2:  
 3:  # Open Finder window to the current directory and select all the files
 4:  # in that directory whose names are passed to this script via stdin.
 5:  
 6:  # Open a Finder window to the current directory. 
 7:  open .
 8:  
 9:  # Construct the AppleScript in three parts.
10:  # 1. Initialize variables and start telling theFolder.
11:  applescript='tell application "Finder"
12:    set theFolder to target of front window as alias
13:    set theFiles to {}
14:    tell folder theFolder
15:    '
16:  
17:  # 2. Add all the files from stdin to theFiles list
18:  while read f; do
19:    applescript+="    set end of theFiles to file \"$f\"
20:    "
21:  done
22:  
23:  # 3. Stop telling theFolder and select theFiles. Return nothing.
24:  applescript+='  end tell
25:    select theFiles
26:    return
27:  end tell'
28:  
29:  # Run the AppleScript.
30:  echo "$applescript" | osascript -

After opening a Finder window in Line 7, it constructs an AppleScript that does the selecting of files. The exact form of the AppleScript depends on the filenames passed into sel. For the example above, the AppleScript will be

applescript:
 1:  tell application "Finder"
 2:    set theFolder to target of front window as alias
 3:    set theFiles to {}
 4:    tell folder theFolder
 5:        set end of theFiles to file "20240915-001.jpg"
 6:        set end of theFiles to file "20240915-002.jpg"
 7:        set end of theFiles to file "20240915-003.jpg"
 8:        set end of theFiles to file "20240915-004.jpg"
 9:        set end of theFiles to file "20240915-005.jpg"
10:        set end of theFiles to file "20240915-006.jpg"
11:        set end of theFiles to file "20240915-007.jpg"
12:        set end of theFiles to file "20240915-008.jpg"
13:        set end of theFiles to file "20240915-009.jpg"
14:        set end of theFiles to file "20240915-010.jpg"
15:      end tell
16:    select theFiles
17:    return
18:  end tell

The loop in Lines 18–21 of the shell script creates all the lines in the tell folder section of the AppleScript.

After the AppleScript is built and saved in the applescript variable, Line 30 then runs it by passing it to the osascript command.

I should mention a few things:

  1. sel has to be run from the directory with the files you want to select. Being able to go up or down a folder hierarchy is beyond my coding skills. This limitation has never got in my way.
  2. sel can take a few seconds to run if it’s selecting hundreds of files. The Finder window will appear immediately, but it will take a little while before the files are selected. AppleScript isn’t especially fast.
  3. I’ve put zsh in the shebang line of sel because zsh is the Mac’s default shell, but the script runs the same under bash. If you want to translate the script to tcsh, you’re on your own.

  1. Or iTerm or whatever terminal emulator you choose. I used iTerm for several years, but moved back to the Terminal some months ago.