Besting a time thief using Applescript

When I want to work on a project there’s always some ceremony. I have to open a few windows, processes and programs. And position them. It doesn’t take that much time. But every day it happens 1-5 times, especially if I have to jump between projects.

This summer I had some spare moments to tinker on a couple side projects. So, why not have a shot at automating away some minutia?

I searched around for a way to learn just enough Applescript to pull this off. I read this Applescript: Beginner’s Tutorial before I started tinkering. I also glanced at the iTerm docs on Applescript. A little Stackoverflow searching was also mixed in. So, while the living room TV droned on I built a functioning script through trial and error.

GIF of one of my projects starting up opening various terminal windows, starting processes and opening an editor.

Tadura! One little script to rule them all.

In the GIF above you can see that I start a .scpt file (Applescript) named after the project that I want to work on. I start the script from the free version of the Alfred launcher, which I’ve set to run .scpt scripts instead of opening them.

It works. :) And this setup will be quite transferrable to many of my other projects, in which each of those projects' folders will house a slightly modified script.

Here’s the script I built, intermingled with enough comments to make sense I hope.

Automate all the things!

# opens an iterm2 terminal
tell application "iTerm"
  # keep a reference to the first terminal prompt and ...
  set firstSession to (current session of current window)
  # ... split it twice and remember the two other sessions.
  tell firstSession
    set thirdSession to (split horizontally with default profile)
    set secondSession to (split horizontally with default profile)
  end tell
  # position iTerm to the right
  set bounds of current window to {960, 26, 1439, 818}
  # run infinite loop until iTerm prompts become
  # ready to receive input
  repeat until (get is at shell prompt of firstSession) is true
    log ("waiting")
  end repeat
  # then start my developer processes in the various terminal prompts.
  tell firstSession
    write text "cd ~/Workspace-netlife/de-bergenske/de-bergenske"
    write text "npm run dev"
  end tell
  tell secondSession
    write text "cd ~/Workspace-netlife/de-bergenske/de-bergenske-local-db"
    write text "docker-compose up"
  end tell
  tell thirdSession
    write text "cd ~/Workspace-netlife/de-bergenske/de-bergenske"
    write text "atom ."
  end tell
end tell
# position the atom editor which was opened in the last terminal session
tell application "Atom"
  set bounds of first window to {0, 23, 960, 821}
end tell