Converting from Alias to Path, Path to Alias

I’m looking for one of two things:

OPTION #1:
How to covert a shell path to an alias, and then an alias back to a path. Must keep escaped characters in mind (like “/”) and special characters intact in both directions.

OPTION #2:
How to get Finder commands to cooperate with shell-style (slash-delimited) paths. For example “number of items in folder folder_to_check” does not work when “folder_to_check” is a shell path, only seems to accept an alias (colon-delimited path). If there is some on-the-fly coersion that will work with special characters and “/” characters (escaping everything properly), love to see it!

I had written some routines to covert between paths and aliases as needed using ASTIDs, but I ran into a problem when users decide to put slashes in the file names. Not illegal in Mac Finder, but causes havoc with using the TID trick (covert “/” to “:” and the reverse).

Obviously Option 2 is preferred and cleaner. I’m currently using Option 1 with problems with “/” is filenames.

Here’s a sample path that is hell for me using TID replacement:

Hard Disk:Folder:Filename w/Slash.jpg

Also, how do you handle the extra “/Volumes/” that the shell wants, but AppleScript gets confused by as an alias? (i.e. “:Volumes:Hard Disk:Folder:” doesn’t work).

Thanks in advance!

Hi Kevin
…back to a path. Which path?? HFS or POSIX? :wink:

In AppleScript all paths are HFS path (colon separated).
In the shell, all paths are POSIX paths (slash separated).
If there are space characters or other special characters in the POSIX path, the characters must be escaped (with backslash) or the path must be quoted.

Mixed forms are non valid!

Here some examples to coerce one class to another


-- assuming your start volume is named "Macintosh HD" and the current user is named "Calvin Fold"


set a to path to home folder
--> alias "Macintosh HD:Users:Calvin Fold:"
set b to a as Unicode text
--> "Macintosh HD:Users:Calvin Fold:"
set c to POSIX path of a
--> "/Users/Calvin Fold/"
set d to quoted form of POSIX path of a
--> "'/Users/Calvin Fold/'"
set e to POSIX file c
--> file "Macintosh HD:Users:Calvin Fold:" (this is class file URL!!)
set f to POSIX file c as alias
--> alias "Macintosh HD:Users:Calvin Fold:"

Note: Coercing the paths with one of the methods above,
all paths for non-startup disks (“/Volumes/theDisk/path/” for POSIX path or “theDisk:path:” for HFS path) are coerced correctly

Kevin, Is this what you are looking for?

set posix_path to "/Users/<username>/Desktop/"

set mac_reference to posix_path as POSIX file as alias
set posix_reference to POSIX path of mac_reference

What happens in these methods if there is a slash “/” as part of the file name? Will it be escaped to POSIX, but back to a regular charcter when turned back into an alias?

For example:

Hard Disk
–Folder
----Filename w/Slash.jpg

Note the file has a slash and a space in it.

Right now, when the shell “find” command sees it, it returns:

Filename w:Slash.jpg
(note the colon)

I can’t keep users from using slash characters (gave-up that battle ages ago…as long as Apple let’s them type it, it’s gonna happen). As long as I stick to the coersions will they translate back and forth okay without messing-up slashes as well as foreign language characters like ñ or ø or ô etc.?

Calvin, take a look at your other thread. In there I showed how to return mac style paths back from the handler rather then shell so you don’t have to do any conversions later.

This is expected.

http://www.wsanchez.net/papers/USENIX_2000/

James, I’m watching the other thread, but keeping this one handy as a fallback. I got the coersions in this thread to work, just means I still have to either coerce on the spot, or take my lists returned from shell and run them through a handler to convert the list items all to aliases all at once.

Good catch realizing they were related, because they are. My needs evolved as I was writing. I was just being a good programmer and breaking it down into handler-sized routines. Shell scripts have this way of acting like one of those “let’s make this AppleScript a one-liner” contests that gets started around here. :wink:

I have no idea what you’re talking about ::whistles innocently:: :wink: