Mac OS: How can I launch the iTerm terminal, with a specific profile, from Automator or AppleScript?

4

1

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!

Daniel Magliola

Posted 2012-07-31T17:20:33.767

Reputation: 1 049

If you're new to OS X, why not give Terminal a try first? It's pretty good. – Daniel Beck – 2012-07-31T18:18:31.067

Answers

2

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.

lucianf

Posted 2012-07-31T17:20:33.767

Reputation: 136

4

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

Daniel Beck

Posted 2012-07-31T17:20:33.767

Reputation: 98 421

I'd love to see another answer that launches an new iTerm process, not a window in the existing app. – Sparr – 2014-09-17T22:23:30.307

@Sparr: You're usually running no user application multiple times on OS X. Some behave very weirdly (or break), others actively prevent it. Is there a real reason you're looking for this? – Daniel Beck – 2014-09-18T09:18:34.547

@DanielBeck because I want the OS to behave as if it's another application, the same way it does with Fluid web apps. – Sparr – 2014-09-18T16:42:20.183

Awesome! That's EXACTLY what I needed. Thank you!! – Daniel Magliola – 2012-07-31T19:14:32.477

0

Based on answers above and others on osascript command:

From BASH command line by wrapping the AppleScript in osascript

osascript -e "tell application \"iTerm\"
    create window with profile \"my-cool-profile\"
end tell"

or BASH function that take a profile name as an argument:

open-with () {
  osascript -e "tell application \"iTerm\"
    create window with profile \"$1\"
  end tell"
}

As an open-with BASH script like open-with my-cool-profile

#! /usr/bin/env bash

PROFILE="${1-Default}"

osascript -e $"tell application \"iTerm\"
  create window with profile \"$PROFILE\"
end tell"

And as an open-run BASH script that can run a program/command for you on open:

#! /usr/bin/env bash

PROFILE="${1-Default}"
CMD="${2-echo "I, \$(whoami), am here at \$PWD"}"

osascript -e "tell application \"iTerm\"
  set newWindow to (create window with profile \"$PROFILE\")
  tell current session of newWindow
    write text \"$CMD\"
  end tell
end tell"

Example: Open in a specific profile and run ssh command

open-run my-ssh-profile 'ssh mike@mycoolserver.com'

Example: Open htop in it's own window, auto-close window when user exits htop

open-run my-htop-profile 'htop && exit'

Example: like above on a remote server via ssh

open-run my-htop-profile 'ssh -t mike@mycoolserver.com bash -c htop && exit'

The escaping and quoting can get pretty crazy but it does what I need

Mike

Posted 2012-07-31T17:20:33.767

Reputation: 171