Skip to content

Instantly share code, notes, and snippets.

@mortonfox
Last active June 22, 2016 21:19
Show Gist options
  • Select an option

  • Save mortonfox/9afe3767bdde6494d15ca90e5c40c2e5 to your computer and use it in GitHub Desktop.

Select an option

Save mortonfox/9afe3767bdde6494d15ca90e5c40c2e5 to your computer and use it in GitHub Desktop.
on run argv
if class of argv is list then
try
set dir to first item of argv
on error
-- If no command-line arguments, do not change directory.
set dir to ""
end try
else
-- For testing in Apple Script Editor.
set dir to "/Applications"
end if
tell application "iTerm"
set gotsession to false
if it is not running then
-- If iTerm is not running, the activate command creates a new terminal and session, so we do not need to create a session.
-- If iTerm is running, the activate command only brings it to the foreground and we still need to create a session.
set gotsession to true
end if
activate
try
set current_term to current terminal
set tmp to current session of current_term
on error
-- if iTerm is in a state where it has no open windows, create a window.
set current_term to make new terminal
end try
-- For some reason, make new terminal does not set current terminal. So we need to use our variable here.
tell current_term
if not gotsession then
-- If we did not start iTerm, then create a new session here. Otherwise, we can reuse the initial default session.
launch session "Default"
end if
write current session text "clear; cd \"" & dir & "\""
end tell
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment