How to duplicate an iTerm tab using AppleScript

The terminal is my number one programming tool. And, as such, I need it to be as much ‘out of my way’ as possible, so that using the terminal doesn’t make me waste time. And wasting time is exactly what it forces me to do when I have to open a new tab in the same directory as an already existing tab. Whether you do this

  • by browsing the disk until finding the folder, then creating a new tab, writing cd and dragging the path of the directory; or
  • by browsing the file hierarchy with Quicksilver and then triggering a Quicksilver action to open the folder in iTerm;

or whatever other way you do this, it’s definitely a time waste.

Adding a new keyboard shortcut to iTerm 2

Many applications have a ⌘ D shortcut that allows you to duplicate whatever document the application works with. Why doesn’t a terminal emulator have that shortcut to duplicate a terminal tab? iTerm 2 certainly doesn’t have one, but it does have a preference panel where you can add new keyboard shortcuts:

To add a new global shortcut, just click on the + button. This will open the shortcut addition dialog, where you’ll enter the keyboard shortcut (⌘ D), the action to trigger (Send text), and the command to be sent to the terminal (duplicate-iterm-tab) when the shortcut is triggered:

Notice that the command duplicate-iterm-tab is to be executed when sent to the terminal, so a line feed must be entered at the end. Since pressing ↩ in the dialog will apply changes, to enter a new line you must press ⌥ ↩ instead.

Finally, click OK to create the new shortcut and then you’ll see the new shortcut in the Keys preference panel:

An AppleScript that duplicates the iTerm tab in use

The following AppleScript code should be written to the Duplicate iTerm tab.scpt file, and will get the working directory of the current iTerm tab and create a new tab, changing to that very same directory, and setting the same tab title (thus duplicating the tab you’ve selected before pressing ⌘ D):

-- Duplicate iTerm tab.scpt
tell application "iTerm"
	tell the current terminal
		tell the current session
			set the_name to get name
			set tty_name to do shell script "basename " & (get tty)
			set working_dir to do shell script "pwd-tty " & tty_name
		end tell

		launch session "Default Session"
		tell the last session
			set name to the_name
			write text "cd '" & working_dir & "'"
		end tell
	end tell
end tell

Necessary files for the shortcut to work

For the keyboard shortcut to work from iTerm 2, the Duplicate iTerm tab.scpt file must be placed in some directory of your choice (I’ve chosen ~/shell-config/osx-automation/Duplicate iTerm tab.scpt), and the following files are needed alongside:

  • duplicate-iterm-tab, the shell script that will be run by iTerm 2 when the keyboard shortcut is triggered, and that will execute the AppleScript
  • pwd-tty, a shell script used by the AppleScript to find out the current working directory of the TTY whose name is passed as the first command line argument

Both shell scripts must be in the PATH. I’ll keep them in ~/shell-config/bin, since files placed in there are by default in the PATH (according to how I’ve set up my Bash configuration). If you don’t want to add them to the PATH, you can just change the AppleScript code, writing the absolute path to each script instead.

The file duplicate-iterm-tab has the following content:

#!/bin/sh
#
# Runs the 'Duplicate iTerm tab' AppleScript, enabling iTerm 2 to run this AppleScript when a
# given keyboard shortcut is triggered.
osascript '~/shell-config/osx-automation/Duplicate iTerm tab.scpt'

Please note that you must change the path of the AppleScript to whatever directory you’ve placed it into.

The file pwd-tty has the following content:

#!/bin/sh
#
# Finds out the current working directory of the TTY whose name is passed as the first command
# line argument.
#
# Based on http://superuser.com/questions/220679/how-to-get-tty-shell-working-directory

TTY=$1
PID=`ps -f | grep $TTY | head -n 1 | awk '{ print $2; }'`
lsof -a -d cwd -F n -p $PID | egrep -v 'p[0-9]+' | awk '{ sub(/^n/, ""); print; }'

To finish setting all up, you should give both shell scripts execution permissions with chmod +x.

Useful links

3 Responses to How to duplicate an iTerm tab using AppleScript

  1. This was exactly what I needed, thanks for taking the time to share it.

  2. Ben Tisdall says:

    Thanks for the excellent article, one of those I’ve been hating myself for not automating!

    One suggestion – don’t bother with a separate applescript file, just do as I did in the top-level script and put it in a heredoc:

    osascript <<EOF
    — Duplicate iTerm tab.scpt
    tell application "iTerm"
    — etc …
    EOF

  3. martindodo says:

    Excellent article! Very useful almost 3 years after its publication! 🙂

    I had to add a simple quote at the end of the last line of pwd-tty to get it work:

    ‘lsof -a -d cwd -F n -p $PID | egrep -v ‘p[0-9]+’ | awk ‘{ sub(/^n/, “”); print; }’

Leave a reply to John Stoltenborg (@tlots) Cancel reply