Reading aliases

I want to program an alias on Mac OS X. When I tried, it always read the source file. Any idea how this can be done?

Thanks for your suggestions.

+2
source share
6 answers

Check out BDAlias. This is a nice Cocoa wrapper around an alias.

http://github.com/rentzsch/bdalias

+4
source

If you're at 10.6, then it also provides new methods in NSURL for reading aliases: check NSURL bookmarkDataWithContentsOfURL: to solve them you can use: NSURL URLByResolvingBookmarkData: options: relativeToURL: bookmarkDataIsStale: error:

pre 10.6 Apple " " Cocoa docs NSString

NSString *path = <#Get a suitable path#>;
NSString *resolvedPath = nil;

CFURLRef url = CFURLCreateWithFileSystemPath
                   (kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, NO);
if (url != NULL)
{
    FSRef fsRef;
    if (CFURLGetFSRef(url, &fsRef))
    {
        Boolean targetIsFolder, wasAliased;
        OSErr err = FSResolveAliasFile (&fsRef, true, &targetIsFolder, &wasAliased);
        if ((err == noErr) && wasAliased)
        {
            CFURLRef resolvedUrl = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsRef);
            if (resolvedUrl != NULL)
            {
                resolvedPath = (NSString*)
                        CFURLCopyFileSystemPath(resolvedUrl, kCFURLPOSIXPathStyle);
                CFRelease(resolvedUrl);
            }
        }
    }
    CFRelease(url);
}

if (resolvedPath == nil)
{
    resolvedPath = [[NSString alloc] initWithString:path];
}

, , , Alias ​​ - .

+3

Alias ​​Manager. ( -, FSRef - 64- .)

+1

. , , .

, NSWorkspace.

+1

, ( unix) Apple alias, , .

0

If you want to read alias data, you just need to use posix api to open the file and read it. You can only see this from the terminal using cat.

Kone:test eric$ echo  Hi Mom! > foo.txt
Kone:test eric$ ln -s foo.txt foo2.txt  #to contrast symbolic link and alias
<< Make alias in Finder >>
Kone:test eric$ ls -l
total 192
-rw-r--r--  1 eric  staff      8 Apr 23 11:44 foo.txt
-rw-r--r--@ 1 eric  staff  43252 Apr 23 11:45 foo.txt alias
lrwxr-xr-x  1 eric  staff      7 Apr 23 11:44 foo2.txt -> foo.txt
Kone:test eric$ cat foo.txt
Hi Mom!
Kone:test eric$ cat foo2.txt
Hi Mom!
Kone:test eric$ cat foo.txt\ alias 
bookmark88?????AܧUserserictestfoo.txt...yadda yadda yadda for 40 some k

I do not know if this answers your question. Are you sure you want to open the alias file?

0
source

Source: https://habr.com/ru/post/1793996/


All Articles