cweave_

christa

Posted on March 25, 2022

Automating iTerm2

TLDR

Working with localhost and multiple repos can be tedious - Apple has a way to automate opening multiple terminal windows and tabs, navigating to the directories that you need, and starting up those services. This is a tutorial to show you how to set up that process.

  1. TLDR
  2. Why Automate?
  3. came in for the save!
  4. Start with the Basics
  5. Creating Multiple Sessions
  6. Adding to the Script
  7. Create a New Tab
  8. Miscellaneous Settings
  9. Wrapping Up

Why Automate?

For me, automation is just about speeding up the process. An example of my work flow looks like:

  • Opening up iTerm
  • Navigating to my frontend directory
    • start the frontend server
  • Split the terminal vertically, navigate the the frontend directory
    • start webpack
  • Open a new tab, navigate to the reporting directory
    • start the reporting service
  • Open a new tab, navigate to the engine directory
    • start the engine service (this is a Docker service, with the help of aliases, has been reduced to smaller commands)

It may not seem like a whole lot, but it becomes a pain whenever I have to restart my computer and have to restart each of those services needed to run the application. When I discovered that it could be automated, it changed everything.

Applescript came in for the save!

Start with the Basics

Let's start with a simple script to get the hang of the syntax.

  • Create a file hello_world.scpt with the content below:
#!/usr/bin/osascript
tell application "iTerm2"
  tell current session of current tab of current window
    write text "echo 'hello world!'"
  end tell
end tell
Enter fullscreen mode Exit fullscreen mode
  • From the directory you created hello_world.scpt, Run the script like this: osascript hello_world.scpt. You should see:

Echoing hello world in iTerm with Applescript automation

Why does it appear twice? Echoing added the command to the terminal history. If you hit up in the terminal, echo 'hello world! will appear as the last command.

If you hit up again, you'll get your first command, osascript hello_world.scpt.

Creating Multiple Sessions

Now for the workflow automation.

  • Create a new script file called create_sessions.scpt with the content below:
#!/usr/bin/osascript
tell application "iTerm2"
  set serverSession to current session of current window

  tell serverSession
    set webpackSession to (split vertically with default profile)
  end tell
end tell
Enter fullscreen mode Exit fullscreen mode
  • From the directory you created create_sessions.scpt, Run the script like this: osascript create_sessions.scpt. You should see the terminal window spilt vertically.

New terminal session and splitting the window vertically

What is happening?

Let's break it down:

  • tell application "iTerm2": which application we're sending commands to. In this case, iTerm2 (this can also be iTerm).
  • set serverSession to current session of current window: is creating a new variable name for the current active window. In this case, serverSession is being assigned to the current window. I used serverSession since this will be the window that I used to run my server.
    • set <NAME> to current session of current window
  • set webpackSession to (split vertically with default profile): sets a new variable webpackSession, and splits the window vertically (vertically can be swapped for horizontally to match your needs).
  • end tell: "closing tag" for the function

Adding to the Script

#!/usr/bin/osascript
tell application "iTerm2"
  set serverSession to current session of current window

  tell serverSession
    write text "cdkwhub"
    set webpackSession to (split vertically with default profile)
  end tell

  tell webpackSession
    write text "cdkwhub"
  end tell
end tell
Enter fullscreen mode Exit fullscreen mode

tell <SESSION> to write text "<COMMANDS>"

Here's where the automation fun happens. With each write text, we can pass in the commands that we need. For me, cdkwhub is an alias that cd's into my codebase directory.

In the next tell block, we reference the new tab that was created and write a command to that window.

Writing commands to each tab

Create a New Tab

Continuing to add to the script,

  tell current window
    create tab with default profile
    set tab2 to current session
  end tell

  tell tab2
    write text "put a command here"
  end tell
Enter fullscreen mode Exit fullscreen mode
  • tell current window: with the vertical tab selected
    • create tab with default profile: iTerm will create a new tab
  • set tab2 to current session: creates a new variable called tab2 and sets it to the new session
  • tell tab2: in the new tab, it writes text

Opening split tab and new tab

Miscellaneous Settings

Adding Tab Names

One thing you can do to make your tabs more organized is to set a tab to them. To set a name, use set name to "name". You can do that like so:

...
  tell serverSession
    set name to "SERVER" # Add this line 
    write text "cdkwhub"
    set webpackSession to (split vertically with default profile)
  end tell
...
Enter fullscreen mode Exit fullscreen mode

Inside of each tell statement you can set a name.

Open the Terminal in Fullscreen

Another thing you can automate with this script is having it open your window in fullscreen. You can do that by adding the following to the top of the script:

...
tell application "iTerm2"
  # Open window in full screen
  tell application "System Events"
    keystroke "f" using {control down, command down}
  end tell
...
Enter fullscreen mode Exit fullscreen mode

Add as a .zsh alias

You can really put your automation in overdrive by creating an alias to run your new script. Using an alias can simplify your command into one single word. An example of my use looks like this:

export SCRATCH_DIR=~/codebase/scratch
alias cdscratch='cd $SCRATCH_DIR';
alias startrepos='cdscratch; osascript create_sessions.scpt'
Enter fullscreen mode Exit fullscreen mode

Creating zsh aliases

  • Create a variable to the directory that has my script
  • Create an alias that navigates into that directory and finally, the whole thing
  • Create an alias that uses the cding alias, and then add the osascript command followed by the name of the file

When you open a terminal window, entering startrepos followed by enter will fire off the automation script.

Wrapping Up

Hopefully this helps you save some time. You can also use the Script Editor application to write the script and export it as an executable application, as well as other miscellaneous things. You can also use it to automate other aspects of your day to day processes.

Helpful Likes

💖 💪 🙅 🚩
cweave_
christa

Posted on March 25, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Automating iTerm2
terminal Automating iTerm2

March 25, 2022