Adrian Payne | Frontend Dev Blog

Adrian Payne
Senior Dev @ Costa Digital

Who am I?

Hello, I'm Adrian Payne and I spend most of my time being a Senior Developer at Costa Digital, Hamburg. When I'm not doing that, I'm either singing or stand-up paddling or both!


What I write about


Why the domain?

I'm a frontend developer and I'm from Ireland. I put two and two together. Literally :)


Recent Posts


Sign up for an awesome Lootcrate!

Automating tasks with AppleScript

Ever had a project where you've needed to run multiple services in multiple terminals? Typing all those gulp, grunt, or npm commands can get tedious. Let's learn how to automate it with AppleScript!

It's 9.30am and your computer has decided to crash *sigh*. Now you need to re-open all those terminal windows and get everything running again.

AppleScript is a scripting language that makes it possible direct control of scriptable applications and of many parts of the Mac OS. [source]

Maybe you need to start a taskrunner build and web server server in one terminal, a database service in another, then the backend in yet another. Oh and don't forget about that extra dependency service for that new feature! No thanks. Let's learn how we can launch multiple terminal windows and commands using AppleScript.

Let's make a script!

Create a new file in your flavour editor of the month (or straight through the terminal if your hip like that). Save the file with the extension .scpt. I went with project.scpt.

Next, paste this into the new file. I'll explain it below.

tell application "Terminal"
    set terminal to window 1

    do script "cd ~/Projects; gulp build" in terminal
end tell

Above we're asking to speak to the Terminal application and to set a variable named terminal equal to our open window so we can reference this later. We've also written one do script line which will switch directory and then run the gulp watch command in our active window.

But we wanted multiple tabs! To open a new tab in the terminal we need to use the following line in our script. It's pretty long but does what we need.

tell application "System Events" to tell process "Terminal" to keystroke "t" using command down

After this line we can write another do script and that will get executed in our new tab. Rince and repeat for multiple tabs! Here's an example with 2 tabs.

tell application "Terminal"
    set terminal to window 1

    # Task for Tab 1
    do script "cd ~/Projects; gulp build" in terminal

    # Open a new tab.
    tell application "System Events" to tell process "Terminal" to keystroke "t" using command down

    # Task for Tab 2
    do script "echo This is tab 2!" in terminal
end tell

The code above is specific to the vanilla osx Terminal application. If you're using iTerm, I've put the code for the same result here on github under iTerm.scpt.

Running an AppleScript file

There are at least 2 ways that I know of to run an Applescript. I'll explain both below.

Running through the Terminal

To run an AppleScript file (.scpt) from the terminal, we simple need to open a terminal window and run the script with osascript followed by our file name.

 osascript project.scpt

Running as an Application

Launch the Script Editor from Applications and open your AppleScript file via File > Open. Next use File > Export and save the file as an Application using the File Format dropdown. You can now run the script using your exported file via the desktop or even put it in your Dock.

Run and chill with increased productivity! ;)


Posted in osx, apple, command line on by

Share this post:
This site is run on Digital Ocean. Sign up for yours!