Create a gist now

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Show comment Hide comment
@ManUtopiK

ManUtopiK Jan 29, 2015

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

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

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

This comment has been minimized.

Show comment Hide comment
@GuidoS

GuidoS 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

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

This comment has been minimized.

Show comment Hide comment
@bihell

bihell Sep 13, 2017

It works! Thank you.

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