AppleScript by example, episode 1
I have never written any AppleScript, although I use 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.
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:

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))
Then we edn 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 in the folder we retrieved from Finder before.
Then we run activate
The AppleScript manual says this command “Brings an application to the front, launching it if necessary”
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.