OS X terminal command to resolve path of an alias

前端 未结 3 1724
野性不改
野性不改 2021-02-06 08:04

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 al

3条回答
  •  隐瞒了意图╮
    2021-02-06 08:55

    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];
    

提交回复
热议问题