Skip to content

Instantly share code, notes, and snippets.

@itsthejb
Last active August 29, 2015 13:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsthejb/8900078 to your computer and use it in GitHub Desktop.
Save itsthejb/8900078 to your computer and use it in GitHub Desktop.
Tweak of https://gist.github.com/clintxs/5260225 for iTerm2 support
-- This script automatically `cd`s to the directory of the current document.
-- You can use $f to represent the file for the current document.
-- This is nowhere near perfect, just a quick and dirty script.
-- NOTE: you need to have "Enable access for assistive devices" checked...
-- in the Accessibility preference pane.
-- Example: select a file in Finder, and type: echo The filename is $f
on getCurrentDocument()
set theDir to ""
set theDoc to ""
set theSelection to {}
try
tell application "System Events"
set appName to name of first process whose frontmost is true
if appName is "Finder" then
tell application "Finder"
set theDir to POSIX path of ((target of window 1) as text)
if selection is not {} then
set theSelection to (POSIX path of (selection as text))
if theSelection is theDir then
set theSelection to {}
end if
end if
end tell
else
tell process appName
set theValue to value of attribute "AXDocument" of window 1
set theDoc to do shell script "php -r \"echo urldecode(" & quoted form of theValue & ");\""
end tell
end if
end tell
if theSelection is {} then
set escapedDoc to POSIX path of POSIX file theDoc
else
set escapedDoc to theSelection
end if
set theDir to do shell script "dirname " & quoted form of escapedDoc
set theDoc to do shell script "basename " & quoted form of escapedDoc
end try
{theDir, theDoc}
end getCurrentDocument
on alfred_script(q)
tell application "iTerm"
set docInfo to my getCurrentDocument()
set theDir to item 1 of docInfo
set theDoc to item 2 of docInfo
set theScript to "if [[ `pwd` != " & quoted form of theDir & " ]]; then pushd " & quoted form of theDir & "; fi; f=" & quoted form of theDoc & "; " & q
try
set _session to current session of current terminal
on error
set _term to (make new terminal)
tell _term
launch session "Default"
set _session to current session
end tell
end try
tell _session
write text theScript
end tell
activate
end tell
end alfred_script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment