OS X terminal command to resolve an alias path

I am writing a shell script that will rsync files from remote computers, some linux, some macs, to a central backup server. Macs have folders at the root level containing the aliases of all the files / folders that you want to copy. What is a terminal command that I can use to resolve the path to files / folders that aliases point to? (I need to pass these paths to rsync)

+6
shell terminal path macos
source share
3 answers

I found the following script that does what I need:

#!/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 
+3
source share

I had this problem, so I applied the command line tool. It is open source located at https://github.com/rptb1/aliasPath

The main thing is that it will work even if this alias is broken, unlike any AppleScript solution I found. Therefore, you can use it to write scripts to fix aliases when many files change volume. That is why I wrote it.

The source code is very short, but here is a summary of the key part for everyone who needs to solve this problem in the code, or who wants to look for the appropriate 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]; 
+7
source share

I found this tool .

Small bit of compiled code, function in .bash_profile and voila. Transparent handling of aliases, just use "cd". Several times faster than using Applescript.

+3
source share

All Articles