Skip to content

Instantly share code, notes, and snippets.

@reyjrar
Created February 8, 2012 13:14
Show Gist options
  • 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
@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