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)
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With