Open iTerm Tab with a Directory in Current Session I am working in

I hope I am not alone in using iTerm here so someone can help me with this. I would really appreciate the help.

I want to open iTerm's new tab and go to a directory that I will pass in my script. I found this online. I tried using it in KM like so :

But that didn't work and do anything. None of the scripts there worked in fact. Also I think it won't do what I want either because it might open a new iTerm window and that I don't want.

I want to run it in my profile that I always keep open. I use this to open and close my iTerm

And that is where I want the new tab with the path opened. How can I do this?

Thank you for any help.

Like this?

iTerm Tab with cd.kmmacros (2.3 KB)


Note for other users who don’t want to read the whole thread:

If you are experiencing problems with the script, for example, a new iTerm window doesn’t come to the foreground:

  1. Open iTerm Preferences > Keys
  2. Make sure “Hotkey window hides when focus is lost” is unchecked – regardless of whether “Show/hide iTerm2 with a system-wide hotkey” is enabled!
  3. Relaunch iTerm.

If you want to leave the “Hotkey window hides…” enabled, you’ll have to modify the script. See posts below.


Update (2016-10-19 19:34 Z):

Better script:

It checks now if there is an open window. If yes, it opens a new tab, if not, it opens a window and writes to the window (without creating a new tab).

1 Like

Hey Tom. this is lovely. Thank you.

I am curious though, why doesn’t this work?

quoted form is mainly for variables.

Write it simply like this:

tell current session to write text "cd ~/Desktop"

I added a better script to my post above.

1 Like

This is great. Thank you.

I am trying to figure out now, how to wrap it all in an if statement that will check if iTerm is currently active. If it is, it will do this whole action, if not, it will do the whole action + trigger a keystroke to open iTerm for me. I can do the keystroke part (with sending a key code).

How do I do the if cause though to see if the app is currently active. I found something like this but couldn’t make it work. :frowning:

The script (the new version) activates iTerm if it’s in the background, and it launches it if it isn’t running. Just download the macro again, I have updated it with the new script.

I have and it does activate it, but it doesn’t pull up and show my iTerm window. For it to do it, it will need to press the hotkey.

I tried putting the hotkey there :

tell application "Keyboard Maestro Engine" to set thePath to getvariable "itermPath"

if application "iTerm" is running then
   tell application "iTerm"
      activate
      if exists window 1 then
         tell current window
            create tab with default profile
            tell current session to write text "cd" & space & quoted form of thePath
         end tell
      else
         create window with default profile
         tell current window
            tell current session to write text "cd" & space & quoted form of thePath
            tell application "System Events" to key code 13 using {option down, control down, shift down, command down}
         end tell
      end if
   end tell
end if

But that doesn’t work. Nor was putting the tell application in the first if clause worked for some reason but putting this key code alone as applescript in new macro worked and run the key code properly.

Use my script, it will work. You have made some changes that prevent the script from doing what you want it to do.

(Your if application "iTerm" is running then […] is the culprit.)

Yeah as I said, I have tried your updated script and it does activate iTerm but it does not activate iTerm itself. I am running iTerm in full screen by pressing cmd + return so it is “full screen”. Just checked now and your script works well when it is not in full script but when it is, it is not working.

Ah, you said nothing about full screen until now. (I’m not an iTerm user, so I don’t know what the hotkeys are doing.)

It should I think be easily solved with an if clause that will check if iTerm is active. If it is, do the action that you have sent, and if not, do the same action + hotkey. I just am not really sure about that if clause.

Try to compile the script in Script Editor. It will show you some info, what is going wrong.

To test-run it from Script Editor replace the first line with

set thePath to "/Users/<your name>/<folder>"

Without ~ !

(Or make sure the variable iTermPath is present in KM.)

It works for me in Full Screen mode if I do this:

  1. Add the line
tell application "System Events" to key code 13 using {option down, control down, shift down, command down}

to the very end of the script. (The script = the script as contained in the macro.)

  1. Remove the activate from line 3

Yeah, but you need if clause because if I add that line of code in the end, yes it will work well when iTerm is not active. But if I am already in iTerm and activate the macro, it will instead hide iTerm from full screen. That’s why you need the if clause.

Add this at the end:

tell application "iTerm"
  if not frontmost then tell application "System Events" to key code 13 using {option down, control down, shift down, command down}
end tell

The whole script:

tell application "Keyboard Maestro Engine" to set thePath to getvariable "itermPath"
tell application "iTerm"
  activate
  if exists window 1 then
    tell current window
      create tab with default profile
      tell current session to write text "cd" & space & quoted form of thePath
    end tell
  else
    create window with default profile
    tell current window
      tell current session to write text "cd" & space & quoted form of thePath
    end tell
  end if
  if not frontmost then tell application "System Events" to key code 13 using {option down, control down, shift down, command down}
end tell
1 Like

Yep, it definitly stays in Full Screen now when the macro is triggered.

Thank you a lot Tom.

This doesn’t though trigger the hotkey when iTerm is not active. :frowning:

It just activates iTerm and brings focus to it but doesn’t trigger the hotkey.

Don’t really know why.

Heck, I’ll never again answer to any of your topics :wink: