Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Dec 6, 2013

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

@aenain

This comment has been minimized.

Copy link

commented Jun 6, 2014

@SwankyLegg activate does

@IvanParraga

This comment has been minimized.

Copy link

commented Sep 7, 2014

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

This comment has been minimized.

Copy link

commented Jul 22, 2015

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

This comment has been minimized.

Copy link

commented Aug 12, 2015

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

@lochstar

This comment has been minimized.

Copy link

commented Sep 5, 2015

Thanks @ChrisBuchholz. Works a treat.

@ChrisBuchholz

This comment has been minimized.

Copy link

commented Nov 7, 2015

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

@obfischer

This comment has been minimized.

Copy link

commented Jun 7, 2016

@ChrisBuchholz you saved my day!

@werty1st

This comment has been minimized.

Copy link

commented Jul 13, 2016

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jan 25, 2019

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
You can’t perform that action at this time.