Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Given an alias created by the Finder, from the Unix prompt (e.g., in Terminal) how can I get the target of the alias?

In my Finder windows I have numerous aliases that point mostly to other folders. When in the Terminal, these aliases present as regular files. How can I get the target of an alias so that I can perform some operation, e.g., "cd", on the target?

MacBook Pro (17-inch Early 2011), OS X Mavericks (10.9.2)

Posted on Nov 28, 2014 11:43 AM

Reply
13 replies

Nov 28, 2014 12:10 PM in response to Ken Nellis

A Finder alias is unlike the Unix variety, the Finder alias is actually a binary file which contains information concerning the file. One of the pieces of information is of course the location of the original but there is no way to resolve that location outside of the Finder.


Actually there is no Apple supplied way, there are programs out there that will do this but as the file format can change between versions of OS X and Finder there is no guarantee that what works at on point will continue to work.


Depending on what you are doing you might be abel to get the original file using a small Applescript run from within your shell script.


regards


<edit>


this is one of the sites that have a binary http://sebastien.kirche.free.fr/python_stuff/MacOS-aliases.txt and other useful information. Again as this stuff canned does change not sure how accurate it currently is.

Nov 28, 2014 12:25 PM in response to Frank Caggiano

Thanx, Frank. We're on the same page. Indeed, the "strings" command shows me that the target is in there, but how to extract it properly? If AppleScript can do it for me, then the question becomes what is the AppleScript command to do such? From the command prompt the "osascript" command could invoke the AppleScript to return the target back to the shell, which would work for me. Any ideas on the AppleScript to do such?

Nov 28, 2014 12:52 PM in response to Ken Nellis

Something like this should work for you


#!/bin/sh


aPath=$(osascript << EOF

tell application "Finder"

set theItem to (POSIX file "${1}") as alias

if the kind of theItem is "alias" then

get the posix path of ((original item of theItem) as text)

end if

end tell

EOF

)


echo $aPath

Takes the alias file path as its argument


sets aPath to the original file


regards

Given an alias created by the Finder, from the Unix prompt (e.g., in Terminal) how can I get the target of the alias?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.