Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Switch Terminal dir to frontmost window of certain apps UNIX
I spend a lot of time in Terminal, and a lot of time in GUI applications. There have been a few hints about using the Finder with Terminal, so here's mine to add to the collection.

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.]
    •    
  • Currently 3.60 / 5
  You rated: 5 / 5 (5 votes cast)
 
[8,780 views]  

Switch Terminal dir to frontmost window of certain apps | 6 comments | Create New Account
Click here to return to the 'Switch Terminal dir to frontmost window of certain apps' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Switch Terminal dir to frontmost window of certain apps
Authored by: ojohns on Jul 25, '06 10:53:54AM

This is nifty. But I had to modify it a bit to get it to work directly from the command line.
Copy this and then paste it into a Terminal window:

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 cdff { cd "`ff $@`"; };

Note the space before each -e. After pasting this into Terminal and pressing Return, then type cdff and press return. The Terminal should cd to whatever is the current Finder window.

---
ODJ



[ Reply to This | # ]
Switch Terminal dir to frontmost window of certain apps
Authored by: xSmurf on Jul 25, '06 10:55:22AM

Very neat idea! Why didn't I think of this before! Thanks a bunch!

Note: I've had to add '\' at the end of every line, except the last, of the first function (ff) to make it work.

---
MacBook Pro 2.16Ghz / 2Gb / 100Gb 7200rpm / CD/DVD±RW
PM G4 DP 800 / 1.25gb / 120Gb+80Gb / CD/DVD±RW/RAM/DL
- The only APP Smurf



[ Reply to This | # ]
Switch Terminal dir to frontmost window of certain apps
Authored by: xSmurf on Jul 25, '06 12:51:59PM

Sry for the double post about the backslashes, I was still editing when it got posted.

---
MacBook Pro 2.16Ghz / 2Gb / 100Gb 7200rpm / CD/DVD±RW
PM G4 DP 800 / 1.25gb / 120Gb+80Gb / CD/DVD±RW/RAM/DL
- The only APP Smurf



[ Reply to This | # ]
Switch Terminal dir to frontmost window of certain apps
Authored by: ojohns on Jul 25, '06 11:03:33AM

The editor ate the leading spaces of my previous post. This is a corrected version.

This is nifty. But I had to modify it a bit to get it to work directly from the command line.
Copy this and then paste it into a Terminal window:

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 cdff { cd "`ff $@`"; };

After pasting this into Terminal and pressing Return, then type cdff and press return. The Terminal should cd to whatever is the current Finder window.



---
ODJ



[ Reply to This | # ]
Switch Terminal dir to frontmost window of certain apps
Authored by: timcrawf on Jul 26, '06 09:18:53AM

how do I get Terminal to remember this?



[ Reply to This | # ]
a simpler way?
Authored by: kholburn on Jul 27, '06 02:50:57PM

type "cd " in the terminal window, click on the tiny icon in the title bar of the finder window and drag it to the terminal. The text path of the window is placed in the terminal window. Hit return in the terminal window.



[ Reply to This | # ]