5

Does anybody know the Applescript command to show or hide a iTerm2 terminal window? I use iTerm2 in a guake style command window and currently using one window and many tabs.

My use case: I managed to open a new iTerm2 tab from Forklift (see here) but I still need to activate iTerm2 manually with my show/hide shortcut.

9

Here are several options (none of them have been tested, so test and see which one works for you):

Option 1:

tell application "iTerm2" to activate
tell application "System Events" to tell process "iTerm2" to keystroke "h" using command down

Note: Keystroke may be replaced with m if you want iTerm2 to be minimized to the dock with the Genie effect.

Option 2:

tell application "Finder"
set visible of process "iTerm2" to false
end tell 

Option 3:

tell application "System Events" to tell process "iTerm2" to set visible to false

Note: The process name may just be iTerm instead of iTerm2. I opened iTerm on my Mac and then Activity Monitor, and the Process Name that it shows is iTerm. Please change accordingly if it does not work for you.

Links and Resources

Here is a great WikiBooks link that describes exactly what you need.

Please let me know which option works for you so I can edit the question and only include the option that works.

Edit:

If you make the iTerm window the frontmost application (Be careful you don't want to make the AppleScript window the frontmost application), try this script:

tell application "System Events"
    set frontProcess to first process whose frontmost is true
    set visible of frontProcess to false
end tell
  • tested both. Both do not work. iTerm2 is activated but the terminal window only shows after pressing the shortcut key. Thanks for the try. – therealmarv Aug 6 '13 at 18:54
  • What version of OS X are you running? – sameetandpotatoes Aug 6 '13 at 18:56
4
tell application "System Events" to tell (process 1 where frontmost is true)
    if name is "iTerm" then
        set visible to false
        return
    end if
end tell
tell application "iTerm"
    reopen -- open a default window if there are no open windows
    activate -- make frontmost
end tell

If others were looking for a way to show or hide iTerm in general, you can set a shortcut for it in the preferences or use Alfred:

1

I tried every of this solution. None if them worked as expected. They all opened a completely new terminal or did not worked. The only solution which worked for me in the full screen iTerm Hotkey Window was to simulate keypresses.

Be sure to disable Hotkey toggles a dedicated window with profile in settings or you need to simulate even that hotkey press which caused the major pain for my problem.

enter image description here

Here is the file /Applications/ForkLift.app/Contents/Resources/OpenInTerminal.applescript which:

  • opens my fullscreen terminal app, opens a new session window (my session is named Hot, change it to Default or your desired session type from profiles)
  • waits for 1 second (otherwise the new session is not recognized)
  • changes the directory and clears screen

tell application "iTerm"
    activate

    try
        tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
        delay 1
        set _session to current session of current terminal
    on error
        set _term to (make new terminal)
        tell _term
            launch session "Hot"
            set _session to current session
        end tell
    end try

    tell _session
        write text "cd _forklift_path_placeholder_;clear;"
    end tell
end tell
1

tell application "iTerm2" to activate set miniaturized of window 1 to true end tell http://macscripter.net/viewtopic.php?id=12501

the wording of your question makes me think you want individual windows out of the way. The hide function on mac will hide everything. I needed a window away from view. the url above was helpful.

your solution may not be hiding a single window but rather minimizing a single window.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .