Keyboard shortcut to launch profile in New Window

958 views
Skip to first unread message

Han Sooloo

unread,
Dec 22, 2016, 10:45:35 AM12/22/16
to iterm2-discuss
Typical workflow:

  1. Open profiles window (Cmd-O)
  2. Type name of profile, highlight with cursor
  3. Want to open profile in New Window using keyboard


How can I accomplish Step-3 above?


Steve King

unread,
Dec 22, 2016, 11:20:35 AM12/22/16
to iterm2-...@googlegroups.com

How about a shell script, with a little applescript thrown in for spice? Assuming you use bash, put this in your .bashrc:

function win() {
    if [ -z "$1" ]; then
        osascript -e "tell application \"iTerm\" to create window with default profile"
    else
        osascript -e "tell application \"iTerm\" to create window with profile \"$1\""
    fi
}

At the shell prompt you can type just win to get a new window with the default profile, or win xyzzy to open a window with a profile named “xyzzy”.

--
Steve King
st...@narbat.com

Han Sooloo

unread,
Dec 22, 2016, 11:44:58 AM12/22/16
to iterm2-discuss
That's a good suggestion, assuming I am on my local machine terminal session, where 'win' would result in AppleScript execution.

The goal is to be able to launch a profile from "any" other terminal session, using ONLY the keyboard.

e.g., I am connected to my DigitalOcean VM, where I am doing some log tracing.  At that time, I realize, I need to connect to another server.

I envision something like pressing Cmt-O to bring up the profiles window; and then, Cmd-Opt-N, while the desired profile is selected, so that it opens up in a new window.

Steve King

unread,
Dec 22, 2016, 3:05:20 PM12/22/16
to iterm2-...@googlegroups.com
On 2016-12-22 11:44, Han Sooloo wrote:
e.g., I am connected to my DigitalOcean VM, where I am doing some log tracing.  At that time, I realize, I need to connect to another server.

I envision something like pressing Cmt-O to bring up the profiles window; and then, Cmd-Opt-N, while the desired profile is selected, so that it opens up in a new window.

That's trickier, and I don't see a good way to do it purely within iTerm. I'm kind of surprised that I can't find a built-in way to open a specific profile without the mouse. Here's how I'd do it, which may or may not be applicable to you.

Option 1: I have an iTerm hotkey window defined. I pretty much reserve that window for quick single-shot commands, stuff that runs and exits immediately, not something like an editor session that's going to persist for a while. So, a local command prompt is always just a keystroke away. I use other windows for my ssh sessions. If I was in an ssh session and I needed to connect to a new server I'd pop open the hotkey window and type "win whatever" to open a new window.

Option 2: Another way is by using a hotkey manager like Butler. (There are others that would probably do just as well, like Quicksilver and Alfred, but Butler is the one I use.) I'd set up either hotkeys or keywords that essentially do the same thing as the win script. Hit the hotkey, type the server name into Butler, and let Butler run the script.

Option 3: Automator. This would be the first option, but I couldn't *quite* get it to work. Create a new "Service" workflow.  The workflow should be "Ask for Text" -> "Run AppleScript", the AppleScript being the line from the win script.  Set the workflow to receive "no input" in either "any application".  Save it as "Open iTerm Profile". Then, open System Prefs.  Go to Keyboard -> Shortcuts -> App Shortcuts. Create a new shortcut for All Applications, Menu Title "Open iTerm Profile".  Assign it a convenient keyboard shortcut.  Now just hit the shortcut key.  You'll get a popup dialog asking for a profile name. Hit return and a new iTerm window is opened.  Great, so what's wrong with it?  The problem I ran into is that when the dialog pops up *it does not get mouse focus*, so you have to mouse over and click it before you can type. That kind of defeats the point.  (See http://blog.fosketts.net/2010/08/09/assign-keyboard-shortcut-applescript-automator-service/ for more about running Automator workflows as shortcuts.)

Option 4: Automator again. I couldn't get this one to work at all. It worked when I ran it inside Automator, but not when I ran it as a service. You can already open the iTerm profiles window and select a profile with keystrokes.  All you need is to have something click "New Window" for you.  I tried to record an Automator workflow of just me clicking that button, and assigning it a shortcut as above.  Then in iTerm I hit ⌘O, typed the profile name, and pressed my shortcut key. I expected Automator to click the button for me, but it just gave me an unhelpful error along the lines of "An error occurred." Maybe if it gave me a little more to go on I could figure it out.  (Edit: On closer examination of the logs it looks like this might work if you go to Sys Prefs, Security & Privacy, Accessibility and give iTerm permission to control your computer.)

Good luck, I hope something works out for you.

--
Steve King
st...@narbat.com

Han Sooloo

unread,
Dec 22, 2016, 3:53:49 PM12/22/16
to iterm2-discuss
Thanks for the really creative alternatives!

I'll try these to see which one works best.

I'll also open an issue to track an enhancement request.

Han Sooloo

unread,
Dec 22, 2016, 4:01:29 PM12/22/16
to iterm2-discuss
I feel really silly .... apparently one can do "Shift-Enter" to open selected profile in a New Window !!!
Reply all
Reply to author
Forward
0 new messages