Get POSIX path of original of an alias-to-a-folder?

I’m writing an application in which I want to get the POSIX path of the original of an alias to a folder. My application will be storing some aliases inside itself, with each alias having a name like “drive_e” and acting as an alias to a folder somewhere else in OS X (like ~/Documents or “~/Anything Else”).

Everything I’ve tried (based on various posts here and elsewhere) has failed. Can anyone suggest a technique for this?

Thanks for any help.

Hello.

An alias has an original item property, which you will find in Finders scripting dictionary.

Thank you - I should have explained that I knew about that property, but I can’t figure out how to use it. Both of these methods produce an error (the alias is an alias to a folder):

set myAlias to "Users:edward:Desktop:vDosWP.app:Contents:Resources:vDos.app:drive_c:"
tell application "Finder"
	set myPath to original item of myAlias
end tell
set myPosix to "/Users/edward/Desktop/vDosWP.app/Contents/Resources/vDos.app/drive_c/"
tell application "Finder"
	set myPath to original item of myPosix
end tell

The same error occurs if I omit the colon (“:”) from the end of the Finder path or the “/” from the end of the POSIX path.

When I click Cmd-I on the alias itself, it tells me the correct POSIX path to the original item.

I am clearly doing something wrong, and I would be grateful for any help.

EDIT: I see what the problem is: the alias I am testing is in fact a symbolic link, which looks like an alias in the Finder, but is in fact not an alias at all. The answer is to use the unix command “readlink”. Apologies for wasting bandwidth on this one!

Hi,

the Finder treats symbolic links as alias files, but there are some issues with your code


set myAlias to "Users:edward:Desktop:vDosWP.app:Contents:Resources:vDos.app:drive_c:"
tell application "Finder"
	set myPath to original item of myAlias
end tell

  1. A colon separated HFS path must start with a disk name (even specifying the startup disk)
  2. The code tries to get the original item of a literal string instead of a Finder object

try this, replace nameOfStartupDisk with name of the startup disk


set myAlias to "nameOfStartupDisk:Users:edward:Desktop:vDosWP.app:Contents:Resources:vDos.app:drive_c:"
tell application "Finder"
	set myPath to original item of item myAlias
end tell

or, much more portable


set myAlias to (path to desktop as text) & "vDosWP.app:Contents:Resources:vDos.app:drive_c:"
tell application "Finder"
	set myPath to original item of item myAlias
end tell


The second example has a third issue: The Finder does not recognize POSIX paths as string paths at all.

Stefan,

Thank you! I should have remembered that the full path was needed. AppleScript did not give me an error when I got the POSIX path from the HFS path, so I lazily assumed that the HFS path was correct. It wasn’t, as you showed me.

Thank you again!

Additionally, if you want to get the POSIX path of a Finder object use this


set myAlias to (path to desktop as text) & "vDosWP.app:Contents:Resources:vDos.app:drive_c:"
tell application "Finder"
	set myPath to original item of item myAlias
end tell
set myPOSIXPath to POSIX path of (myPath as text)

No, it’s an alias. When you create a symbolic link in UFS, a similar alias file is created in HFS. UFS doesn’t support alias files and HFS doesn’t support symbolic or hard links. So when the VFS knows a file system specific file is created, it will create for each specific file system a link.

Hello.

If somebody should ever have to resolve symbolic links that are relative, then the solution is here: (the first part of the combined filename would be the posix path of the file that contains the symbolic link).

Yes, I finally figured that out in outline, but not in any intelligent way. What I notice, though, is that traditional HFS aliases seem to have a file size of 5MB in the Finder, while a symbolic link (in the directory created by the application that I’m working with) has perhaps 25 bytes. Am I missing something obvious? They certainly look different!

The alias’s size was discussed some months ago.

Aliases contain a lot of infos borrowed from the original file.
Not really useful when both are on the same disk.
It’s interesting when the original is on a network, in the cloud or on an external HD.
In these cases, the embedded infos give the ability to display a document’s preview even if it’s on a device which is not mounted.
My own choice is to use symbolic links for every file whose original is not on the internal HD.
For others I use aliases.

Yvan KOENIG (VALLAURIS, France) lundi 10 mars 2014 17:46:02