
Jul 25, '06 07:30:03AM • Contributed by: trangised
I created some shortcuts for CodeWarrior and Xcode, as those are the programs I use most with Terminal. The shortcuts execute a cd to change Terminal's active directory to match that of the open CodeWarrior or Xcode document. I did this by creating the following six functions in my .profile file.
Note that I have created two functions (one to get the path for an application, and a second to implicitly call cd. You could just as easily join those into one function. Here's the code...
function ff { osascript -e 'tell application "Finder"'
-e "if (${1-1} <= (count Finder windows)) then"
-e "get POSIX path of (target of window ${1-1} as alias)"
-e 'else' -e 'get POSIX path of (desktop as alias)'
-e 'end if' -e 'end tell'; }
function cwff { osascript -e 'tell application "CodeWarrior IDE"'
-e "if (${1-1} <= (count documents)) then"
-e "get POSIX path of (location of document ${1-1} as text)"
-e 'else' -e 'get ""' -e 'end if' -e 'end tell'; }
function xcff { osascript -e 'tell application "Xcode"'
-e "if (${1-1} <= (count windows)) then"
-e "get associated file name of window ${1-1}"
-e 'else' -e 'get ""' -e 'end if' -e 'end tell'; }
function cdff { cd "`ff $@`"; }
function cdcw { FILE="`cwff $@`"; if [ "-$FILE" != "-" ] ; \
then cd "${FILE%/*}" ; fi; }
function cdxc { FILE="`xcff $@`"; if [ "-$FILE" != "-" ] ; \
then cd "${FILE%/*}" ; fi; }
It should be pretty straightforward to add support for additional applications -- just open up the AppleScript dictionary and go from there. For instance, Photoshop should work with get POSIX path of (file path of document ${1-1} as alias) if you need it. All these scripts accept an optional parameter for which window/document to get, thus the ${1-1} sprinkled about.
[robg adds: I haven't tested this one. Note that I broke the last two function lines with backslashes. Copying and pasting should work fine, but if it doesn't, just remove the backslash and the line break, and make each one a long single line.]