-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
It works! Thank you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got this error if iTerm is open but without window opened.