Open terminal app in iTerm and if open bring to front

Hi there,

I would like to map a key to open the file manager vifm in iTerm and if the app is already running, then bring the window of that instance of iTerm to the front.

I saw a similar topic, namely Open terminal app, but that yields an error if I merely attempt to exchange iTerm for Terminal.

Is there an elegant, instant and error-proof way of doing this with Keyboard Maestro? Or do I, as was pointed out in the above-mentioned topic, need an AppleScript for that? If the latter, does anyone have a similar script? If not for vifm, then for some other terminal app that I can easily configure to open vifm?

Thanks in advance.

Hey René,

If you haven't read this it's worth a couple of minutes of your time.

Tip: How Do I Get The Best Answer in the Shortest Time?

And don't forget to search the forum – there are several threads on iTerm.


All you can do with Keyboard Maestro in this context is insert text (either by typing or by pasting) and mimic the Return key.

This will usually work just fine if you've set things up properly, but since you're driving the UI an unexpected chuckhole might cause an accident.

Therefore a scripted solution is usually better and more foolproof (when available), because it acts with the application in question rather than on it.

AppleScript terminology is generally different for different applications, so you usually can't mix and match app-specific code – and that's why you couldn't simply trade “iTerm” for “Terminal”.

Here's an AppleScript that works with iTerm 3.2.7 on macOS Sierra 10.12.6.

Run from an Execute an AppleScript Action.

----------------------------------------------------------------

set cmdStr to "vifm"

tell application "iTerm"
   tell front window
      tell current session
         # Attempt to discover if current session is busy.
         if name = "bash" then
            write text cmdStr
         else
            false
         end if
      end tell
   end tell
end tell

----------------------------------------------------------------

-Chris