开发者

Reading Alias files

I want to read alias in Mac OS X programatically. When I tried it always 开发者_StackOverflow社区read original file. Any idea how it can be done?

thanks for your all suggestions.


Check out BDAlias. This is a nice Cocoa wrapper around Alias.

http://github.com/rentzsch/bdalias


If you're on 10.6 then it also provides new methods in NSURL to read aliases: check NSURL bookmarkDataWithContentsOfURL: to resolve them you can use: NSURL URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error:

For pre 10.6 Apple also has a nice samplecode "Resolving aliases" in Cocoa docs for resolving alias from an NSString path:

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

You want to get into the business to load the data manually and interpret that, i do not think that the format of Alias was ever documented.


The way to deal with aliases is through the Alias Manager. (Although it’s carbon-flavoured, the FSRef-based interfaces are still available in 64-bit.)


Note: I am looking for a operation some thing like copy.

You may be interested in the Core Services File Manager's copying functions, then, or NSWorkspace's equivalent operation.


I'm not sure if you are referring to symbolic links (standard unix), or Apple's alias invention, but this looks like it should handle either.


If you want to read the alias data itself you just have to use the posix api to open the file and read from it. You can see this from the terminal just 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 don't know if that answers your question though. Do you really want to open the alias file itself?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜