Skip to content

Instantly share code, notes, and snippets.

@chew-z
Created August 31, 2016 12:56
Show Gist options
  • Save chew-z/f0d12e0fdab217b143bbc14015255da2 to your computer and use it in GitHub Desktop.
Save chew-z/f0d12e0fdab217b143bbc14015255da2 to your computer and use it in GitHub Desktop.
Opens folder selected in Finder in iTerm2 - current tab or when busy creates new tab. I am using that as application attached to Finder.
on run {input, parameters}
if input is not in {{}, {""}, ""} then
tell application "Finder"
set my_file to first item of input
set filetype to (kind of (info for my_file))
-- Treats OS X applications as files. To treat them as folders, integrate this SO answer:
-- http://stackoverflow.com/a/6881524/640517
if filetype is "Folder" or filetype is "Volume" then
set dir_path to quoted form of (POSIX path of my_file)
else
set dir_path to quoted form of (POSIX path of (container of my_file as string))
end if
end tell
CD_to(dir_path)
else
-- What could be done when nothing is selected?
end if
end run
on CD_to(theDir)
tell application "iTerm"
set go_dir to "cd " & theDir
activate
try
set currentWindow to the last window
on error
set currentWindow to (create window with default profile)
end try
tell current session of currentWindow
if is at shell prompt then
select
tell application "System Events"
keystroke "u" using {control down}
delay 0.25
end tell
write text go_dir
else
tell currentWindow
set newTab to (create tab with default profile)
tell newTab
select
set go_dir to go_dir & " ; clear"
tell current session of currentWindow
write text go_dir
end tell
end tell
end tell
end if
end tell
end tell
end CD_to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment