POSIX path of a folder

I am trying to get the POSIX path of a folder. I have tried numerous variations without luck yet. This version at least can pull some info from the folder to its results, but I am not sure how to use that in a way that I can use.


tell application "Finder"
	activate
	if exists (front window) then --In case there are no windows open
		set theWindow to front window
		set thePath to POSIX path of (theWindow)
	end if
	display dialog "The path to this folder is:" default answer thePath
end tell

I get this error in SD4 when I run the script

thanks,
Levon

Model: Dual 2.3GHz G5
AppleScript: 2.1.1/Script Debugger 4
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

It looks like you’re trying to get the POSIX path of a window. :wink:

The POSIX path property expects an alias, file specification, or text. The window class in Finder has a target property that returns “the container at which this file viewer is targeted” (as a Finder reference). You can coerce this value to an alias and then get the POSIX path.

Also, note that window could be any kind of window (Get Info, preferences, etc. The various classes are listed in Finder’s dictionary.). You should specify Finder window instead.

tell application "Finder"
	if exists (front Finder window) then
		set thePath to POSIX path of ((target of front window) as alias)
	else
		set thePath to "" -- or whatever, if needed
	end if
end tell

display dialog thePath

That works great! I changed the dialog so that the user can select the path and copy it to another app, like Mail. In my environment, users work on a number of large servers and need to be able to quickly tell others the location a file or folder is that they are working on.


tell application "Finder"
	if exists (front Finder window) then
		set thePath to POSIX path of ((target of front window) as alias)
	else
		set thePath to "" -- or whatever, if needed
	end if
	display dialog "The path to this folder is:" default answer thePath
end tell

I dropped it right in to the Finder toolbar, making it convenient regardless of the location. Just click!

http://i101.photobucket.com/albums/m76/levonspradlin/work/finderWindow.jpg

thanks,
Levon

Model: Dual 2.3GHz G5
AppleScript: 2.1.1/Script Debugger 4
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

Here’s a thought. What if the reverse could be done? Take a POSIX path and go to that path via script. Does this sound plausible?

Levon

Model: Dual 2.3GHz G5
AppleScript: 2.1.1/Script Debugger 4
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

something like this

set prefFolder to quoted form of POSIX path of (path to preferences folder)
set finderPlistPOSIX to do shell script "mdfind -onlyin " & prefFolder & " com.apple.finder.plist"
display dialog "The POSIX path to com.apple.finder.plist is:" default answer finderPlistPOSIX
set finderPlistHFS to POSIX file finderPlistPOSIX as Unicode text
display dialog "The HSF path to com.apple.finder.plist is:" default answer finderPlistHFS

I guess that is sort of the idea, but maybe this will give you more of an idea of what I am think of:


display dialog "Where would you like to go?" default answer "/Macintosh HD/"
set userInput to text returned of result
tell application "Finder"
	activate
	make new Finder window at userInput
end tell

The first script could get the POSIX path and the second could go to the location. Another person could just paste into a ‘goTo’ script and have a new window created at that location. Is this idea out in the ether, or maybe practical?

Levon

Model: Dual 2.3GHz G5
AppleScript: 2.1.1/Script Debugger 4
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

in Finder type ⇧⌘G :wink:

or what’s about this

display dialog "Where would you like to go?" default answer "/"
set userInput to text returned of result
try
	set userInput to POSIX file userInput as alias
on error
	display dialog "Path doesn't exist" buttons {"Cancel"} default button 1
end try

tell application "Finder"
	activate
	set newWindow to make new Finder window
	set target of newWindow to userInput
end tell

Stefan,
Thanks again for your help. This is exactly what I was after. I have no idea why I forgot about the quick command for go to… Oh well. I am going to post the final version of both files in Code Exchange. I think it is a nice quick way to exchange addresses in a production environment like I am in. My group is excited about the idea.
http://bbs.applescript.net/viewtopic.php?pid=87636

thanks everyone,
Levon

Model: Dual 2.3GHz G5
AppleScript: 2.1.1/Script Debugger 4
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)