Skip to content

Instantly share code, notes, and snippets.

@reyjrar
Created February 8, 2012 13:14
Show Gist options
  • Star 31 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save reyjrar/1769355 to your computer and use it in GitHub Desktop.
Save reyjrar/1769355 to your computer and use it in GitHub Desktop.
AppleScript to Open a New iTerm Window and bring it to the front
(*
* New-iTerm-Window.scpt
*
* Intended for use with QuickSilver
* I mapped option-y to running this script to create
* a new iTerm window on the current workspace
*
* Based on much Googling - very little "original" code here
* Comments/Suggestions to brad.lhotsky@gmail.com
*)
if isAppRunning("iTerm") then
tell application "iTerm"
set myterm to (make new terminal)
tell myterm
set mysession to (make new session at the end of sessions)
tell mysession
exec command "/bin/bash -l"
end tell
end tell
activate
end tell
else
activate application "iTerm"
end if
(* Code from Dweller on
* http://codesnippets.joyent.com/posts/show/1124
*)
on isAppRunning(appName)
tell application "System Events" to (name of processes) contains appName
end isAppRunning
@SwankyLegg
Copy link

Which piece actually brings the window to the front? I'm struggling with something similar.

@aenain
Copy link

aenain commented Jun 6, 2014

@SwankyLegg activate does

@IvanParraga
Copy link

Thanks for the contribution, but I have a strange issue that I don't understand from watching the code. If I execute the script from the Automator, it behaves as expected but if a set keyboard shortcut and iTerm is not opened, it opens not one but two windows. Any idea?

@ChrisBuchholz
Copy link

I have, like you, looked at many examples spread around the internet, and the script I've come up with, taking inspirations for many sources, you included, is the following:

if application "iTerm" is running then
    tell application "iTerm"
        create window with default profile
    end tell
else
    activate application "iTerm"
end if

I have tested it running both via Alfred and Automator. One thing it has above your example and many of the ones on the internet is that, like @IvanParraga mention, if iTerm is not running, two windows are opened. With this script, that doesn't happen.

The only thing missing, really, is that if you already have iTerm open, and have a window on the space your currently on, and you run this script, the window already open will come to the front with the new window. This is a problem all the solution I have seen on the internet shares, and I haven't found a solution yet.

@ohammersmith
Copy link

FWIW @ChrisBuchholz's version requires the iTerm 3 betas.

@lochstar
Copy link

lochstar commented Sep 5, 2015

Thanks @ChrisBuchholz. Works a treat.

@ChrisBuchholz
Copy link

@ohammersmith: Indeed! Though, it's the iTerm 2 betas, right?

@obfischer
Copy link

@ChrisBuchholz you saved my day!

@werty1st
Copy link

I have modified the old version of "open terminal window in current folder"

`on run {input, parameters}
tell application "Finder"
set dir_path to quoted form of (POSIX path of (folder of the front window as alias))
end tell
CD_to(dir_path)
end run

on CD_to(theDir)
if application "iTerm" is running then
tell application "iTerm"
tell current window
create tab with default profile
tell current session
write text "cd " & theDir & ";clear;"
end tell
end tell
end tell
else
tell application "iTerm"
tell current window
tell current session
write text "cd " & theDir & ";clear;"
end tell
end tell
end tell
end if
activate application "iTerm"
end CD_to`

Thias was the old version. i dont remeber the source:
`on run {input, parameters}
tell application "Finder"
set dir_path to quoted form of (POSIX path of (folder of the front window as alias))
end tell
CD_to(dir_path)
end run

on CD_to(theDir)
tell application "iTerm"
tell current window
create tab with default profile
tell current session
write text "cd " & theDir & ";clear;"
end tell

    end tell
end tell

end CD_to`

@avindra
Copy link

avindra commented Feb 14, 2018

@ChrisBuchholz is simple and uncomplicated. Add it to a file, stick #!/usr/bin/osascript at the top, chmod +x it and then call it from your favorite automation utility. in my case, khd.

@chabgood
Copy link

error "iTerm got an error: current tab of current window doesn’t understand the “write” message." number -1708 from current tab of current window

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment