AppleScript by example, episode 1

By

Learn AppleScript by example with a script that grabs the front Finder window path and opens it in Terminal, then run it from the shell with osascript.

~~~

In this first episode we write an AppleScript that takes the folder currently open in Finder and opens it in Terminal, already cd’d into it.

I have never written any AppleScript, although I have used a Mac since 2005 I’ve never had the need.

And I’ve always found it quite difficult to grasp, as it’s a very different mental model from what I usually do.

Anyway, today I had to write some, and here’s what I ended up with after some googling, stackoverflowing and chatgpting:

tell application "Finder"
	set currentFinderWindowPath to (POSIX path of (target of front window as alias))
end tell

tell application "Terminal"
	do script "cd " & currentFinderWindowPath
	activate
end tell

I’m going to describe what this does so I’ll remember in the future.

Asking Finder for the current path

First we ask the Finder app to get the absolute path of the currently active window:

POSIX path of (target of front window as alias)

You could also write that as

tell application "Finder" to get the POSIX path of (target of front window as alias)

And this would be a good way to return that in a shell script for example, using osascript -e, to combine that with other scripts:

Terminal showing osascript command returning the POSIX path of Finder's front window

Like I did in another script I wrote:

finderPath=`osascript -e 'tell application "Finder" to get the POSIX path of (target of front window as alias)'`
open -n -b "com.microsoft.VSCode" --args "$finderPath"

Anyway, we assign the result to the variable currentFinderWindowPath using this structure:

set currentFinderWindowPath to (...)

in this way:

set currentFinderWindowPath to (POSIX path of (target of front window as alias))

Notice that if no Finder window is open, there is no front window to ask about, and the script errors out. Open a Finder window first.

Talking to Terminal

Then we end the tell application "Finder" block because we want to “talk” to another app, the Terminal:

tell application "Terminal"
	do script "cd " & currentFinderWindowPath
	activate
end tell

In this case we tell it to run the command cd ... to change the current active folder to the folder we retrieved from Finder before. do script opens a new Terminal window each time, so running the script twice gives you two windows.

Then we run activate

The AppleScript manual says this command “Brings an application to the front, launching it if necessary”

Running the script

The quickest way to try it is Script Editor, the app that ships with macOS. Paste the code and press the run button.

The first time you run it, macOS asks for permission to control Finder and Terminal. Allow both, or the script fails. You can review those permissions later in System Settings, under Privacy & Security, Automation.

Watch out for spaces in paths

There’s a bug in my script: the "cd " & currentFinderWindowPath concatenation breaks when the folder name contains a space. For a folder called My Projects, Terminal receives cd /Users/flavio/My Projects and only changes into /Users/flavio/My.

The fix is quoted form of:

do script "cd " & quoted form of currentFinderWindowPath

quoted form of wraps the string in single quotes, so the shell treats the whole path as one argument.

Tagged: Mac · All topics
~~~

Related posts about mac: