Mac OS: How can I launch the iTerm terminal, with a specific profile, from Automator or AppleScript?
Combining lines 11 and 58 of the AppleScript sample code on the iTerm website...
tell application "iTerm"
activate
tell (make new terminal)
launch session "Your Profile Name"
end tell
end tell
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.