Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X terminal command to resolve path of an alias

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)

like image 939
Josh Avatar asked Jul 23 '09 23:07

Josh


1 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];
like image 126
rptb1 Avatar answered Oct 24 '22 09:10

rptb1