I'm writing a shell script which will rsync files from remote machines, some linux, some macs, to a central backup server. The macs have folders on the root level containing aliases of all files/folders which need to be backed up. What is a terminal command I can use to resolve the path to the files/folders the aliases point to? (I'll need to pass these paths to rsync)
3 Answers
I had this problem and so I've implemented a command-line tool. It's open source at https://github.com/rptb1/aliasPath
The key thing is that it will work even if the alias is broken, unlike any AppleScript solution I've found. You can therefore use it to write scripts to fix aliases when lots of files change volume. That's why I wrote it.
The source code is very short, but here's a summary of the key part, for anyone else needing to solve this problem in code, or who wants to look up the relevant protocols.
NSString *aliasPath = [NSString stringWithUTF8String:posixPathToAlias];
NSURL *aliasURL = [NSURL fileURLWithPath:aliasPath];
NSError *error;
NSData *bookmarkData = [NSURL bookmarkDataWithContentsOfURL:aliasURL error:&error];
NSDictionary *values = [NSURL resourceValuesForKeys:@[NSURLPathKey]
fromBookmarkData:bookmarkData];
NSString *path = [values objectForKey:NSURLPathKey];
const char *s = [path UTF8String];

- 460
- 4
- 11
I found the following script which does what I needed:
#!/bin/sh
if [ $# -eq 0 ]; then
echo ""
echo "Usage: $0 alias"
echo " where alias is an alias file."
echo " Returns the file path to the original file referenced by a"
echo " Mac OS X GUI alias. Use it to execute commands on the"
echo " referenced file. For example, if aliasd is an alias of"
echo " a directory, entering"
echo ' % cd `apath aliasd`'
echo " at the command line prompt would change the working directory"
echo " to the original directory."
echo ""
fi
if [ -f "$1" -a ! -L "$1" ]; then
# Redirect stderr to dev null to suppress OSA environment errors
exec 6>&2 # Link file descriptor 6 with stderr so we can restore stderr later
exec 2>/dev/null # stderr replaced by /dev/null
path=$(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
)
exec 2>&6 6>&- # Restore stderr and close file descriptor #6.
echo "$path"
fi

- 10,961
- 11
- 65
- 108
-
This is fine as long as the original item can be accessed at the time the script is run. It doesn't work if the alias is broken or the volume can't be mounted. I've added link to a tool that can work in those circumstances in another answer http://stackoverflow.com/a/17570232/425078 – rptb1 Jul 10 '13 at 12:07
-
why all the copying and restoring existing fd 2? why not just say `path=$(osascript 2>/dev/null << EOF ...)` – dubiousjim Aug 31 '15 at 18:39
-
@dubiousjim that would be cleaner. I didn't write this script; I found it online. That being said, I'll test your suggestion and update the answer – Josh Sep 01 '15 at 20:41
-
@Josh, if the script is not your own work, then you should at least include a link to where you found it. – May 08 '16 at 21:47
I've found this tool.
A tiny bit of compiled code, a function in your .bash_profile
, and voila. Transparent handling of aliases, just use "cd". Several times faster than using Applescript, too.
-
The usefull part of your link (this tool): `getTrueName.c` isn't available anymore from there. – dan Apr 30 '15 at 07:31
-
1@danielAzuelos thanks for letting me know! I tweeted at the author, the link is fixed. – Groxx Apr 30 '15 at 19:51
-
The links is broken again, but the relevant script `getTrueName.c` almost surely is the one given in [this answer](https://unix.stackexchange.com/a/409613) which refers back to [this source](https://raw.githubusercontent.com/GalCohen/dotfiles/master/getTrueName.c) – Karl der Kaefer Mar 13 '23 at 10:41