2

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

thanks for your all suggestions.

Girish Kolari
  • 2,515
  • 2
  • 24
  • 34
  • 1
    Are you sure you're not looking at a symbolic link? Alias files and symbolic links are two different things; an alias file is a regular file containing alias data, whereas a symbolic link is a separate kind of file. Last I checked, the POSIX APIs only dereference symbolic links; if you point them to an alias file, you'll get the alias data. – Peter Hosey Apr 09 '10 at 21:57
  • In the Mac OS UI there is the point "Create Alias" in the context menu of files. I guess he means this. (This does NOT create a symlink :() – ZeissS Apr 23 '10 at 21:23

6 Answers6

4

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

http://github.com/rentzsch/bdalias

Ken Aspeslagh
  • 11,484
  • 2
  • 36
  • 42
3

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.

mfazekas
  • 5,589
  • 1
  • 34
  • 25
1

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.)

Jens Ayton
  • 14,532
  • 3
  • 33
  • 47
  • thanks for your valuable input. is it not possible to read Alias with some simple file reading? I tried 'cat' in terminal which file to ope the alias file but 'ls' could read the content. Note: I am looking for a operation some thing like copy. – Girish Kolari Apr 09 '10 at 10:51
1

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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • wehen I read the Mac OS X alias file - it is reading data from original file that alias refer not the content of Alias file. – Girish Kolari Apr 19 '10 at 10:01
0

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.

ergosys
  • 47,835
  • 5
  • 49
  • 70
0

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?

Ukko
  • 2,236
  • 2
  • 21
  • 33