Macos – Mac OS: How to launch the iTerm terminal, with a specific profile, from Automator or AppleScript

applescriptautomatormacos

I'm trying to assign a global keyboard shortcut that will launch a new window of iTerm, with a specific profile. (I managed to do this to launch a new Chrome window with Automator and AppleScript, but this is proving more difficult)

This would be equivalent to activating iTerm, and in the top menu select Profiles -> "my profile", with "alt" or "option" pressed, so it'll open in a new window, not a new tab in the current window.

Any ideas how to do this with either Automator or AppleScript?

In case it's relevant, I have Mac OS Mountain Lion

(Sorry if this is an absolute noob question, I just moved from Windows to Mac, and I'm trying to optimize the things I do all the time)

Thank you!

Best Answer

The previous answer no longer works on the latest version of iTerm2 (3), since terminal has been discontinued. The new approach is to use create window with profile.

However, this doesn't work as expected: while if iTerm is running, it will open the new window using the appropriate profile. But if iTerm is not running, it will open a window using the default profile, and then it will open the second window with the supplied other profile. I came up with the following script to address this:

-- this script will start/activate iTerm (close the default window if the app had been newly started), then open a new session with a desired profile

on is_running(appName)
    tell application "System Events" to (name of processes) contains appName
end is_running

set iTermRunning to is_running("iTerm2")

tell application "iTerm"
    activate
    if not (iTermRunning) then
        delay 0.5
        close the current window
    end if
    create window with profile "xxxxxx"
end tell

Of course, it would have been really easy if iTerm supported command line parameters. Hopefully it will at some point.