Skip to content

Instantly share code, notes, and snippets.

@lsloan
Forked from al3xandru/gist:1156476
Last active September 13, 2017 06:10
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lsloan/1265327 to your computer and use it in GitHub Desktop.
Save lsloan/1265327 to your computer and use it in GitHub Desktop.
Use the selected folder in Finder to open a shell in iTerm and go to its directory.
Gist title: "Open Selected Finder Folder in iTerm"
Summary: Use the selected folder in Finder to open a shell in iTerm and go to its directory.
See also: https://gitlab.com/gnachman/iterm2/issues/3552
on run {input, parameters}
tell application "Finder"
set sel to selection
if (count sel) > 0 then
set myTarget to item 1 of sel
else if (count window) > 0 then
set myTarget to target of window 1
else
set myTarget to path to home folder
end if
my openTerminal(myTarget)
end tell
end run
on openTerminal(location)
set location to location as alias
set the_path to POSIX path of location
repeat until the_path ends with "/"
set the_path to text 1 thru -2 of the_path
end repeat
set cmd to "cd " & quoted form of the_path & " && clear"
tell application "System Events" to set terminalIsRunning to exists application process "iTerm"
tell application "iTerm"
activate
try
set _terminal to current window
tell _terminal
set newTab to (create tab with default profile)
end tell
set _session to current session of _terminal
on error
set _terminal to (create window with default profile)
set _session to current session of _terminal
end try
tell _session
write text cmd
end tell
end tell
end openTerminal
@ManUtopiK
Copy link

I got this error if iTerm is open but without window opened.

Error Number:iTerm got an error: Can’t get current terminal.
-1728

@GuidoS
Copy link

GuidoS commented Jun 22, 2016

I started to get errors about terminal identifier, and have updated my code as follows (iterm 3.0.2):

on run {input, parameters}
    tell application "Finder"
        set sel to selection
        if (count sel) > 0 then
            set myTarget to item 1 of sel
        else if (count window) > 0 then
            set myTarget to target of window 1
        else
            set myTarget to path to home folder
        end if
        my openTerminal(myTarget)
    end tell

end run

on openTerminal(location)
    set location to location as alias
    set the_path to POSIX path of location
    repeat until the_path ends with "/"
        set the_path to text 1 thru -2 of the_path
    end repeat

    set cmd to "cd " & quoted form of the_path & " && clear"

    tell application "System Events" to set terminalIsRunning to exists application process "iTerm"

    tell application "iTerm"
        activate
        try
            set _terminal to current window
            tell _terminal
                set newTab to (create tab with default profile)
            end tell
            set _session to current session of _terminal
        on error
            set _terminal to (create window with default profile)
            set _session to current session of _terminal
        end try
        tell _session
            write text cmd
        end tell
    end tell
end openTerminal

Following this example: https://gitlab.com/gnachman/iterm2/issues/3552

@bihell
Copy link

bihell commented Sep 13, 2017

It works! Thank you.

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