Skip to content

Instantly share code, notes, and snippets.

@clintxs
Last active December 15, 2015 12:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save clintxs/5260225 to your computer and use it in GitHub Desktop.
Save clintxs/5260225 to your computer and use it in GitHub Desktop.
A quick and dirty DTerm-like script for Alfred 2. To install, go into Alfred 2's preferences, click the Features tab, and click Terminal in the sidebar. Set Application to 'Custom', and paste the script in. Now you can just use the terminal command as usual (defaults to '>') and be able to run commands on the frontmost document of any supporting…
-- 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
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 "Terminal"
set docInfo to my getCurrentDocument()
set theDir to item 1 of docInfo
set theDoc to item 2 of docInfo
set theScript to "cd " & quoted form of theDir & "; f=" & quoted form of theDoc & "; " & q
activate
do script theScript
end tell
end alfred_script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment