Skip to content

August 27, 2009

3

How to Create an Alias Programmatically

by Dan

First, a disclaimer. Apple will warn you not to do this. The only supported way of creating an alias is to use the Finder. If you must do it programmatically, you will be told to use AppleScript. But if AppleScript won’t work for you, and a simple Cocoa method is what you want, read on.

Mozy’s Mac client doesn’t create aliases, but our customers do. We want to make sure our software backs them up correctly. So we added some unit tests to our build process that create aliases and check to see that Mozy handles them correctly.

We first used AppleScript, but ran quickly into two issues:

  1. Our build server runs as the root user, which doesn’t have a UI context. AppleScript doesn’t work without a UI context.

  2. Even running as a normal user, AppleScript cannot access the system temporary files location (/tmp) which is where we wanted to create our aliases.

That’s when the fun began.

I spent quite a bit of time failing to find the right bit of magic to create an alias that functioned properly in Finder. It turns out that an alias is a data structure inside another data structure stored in the resource fork of an empty file. Those structures need to have the correct record types for everything to work.

Having gone to the trouble of figuring this out, I thought I’d share. This code creates an alias for a folder, but it should serve as a good template if you need to create another type.

- (void)makeAliasToFolder:(NSString *)destFolder inFolder:(NSString *)parentFolder withName:(NSString *)name
{
    // Create a resource file for the alias.
    FSRef parentRef;
    CFURLGetFSRef((CFURLRef)[NSURL fileURLWithPath:parentFolder], &parentRef);
    HFSUniStr255 aliasName;
    FSGetHFSUniStrFromString((CFStringRef)name, &aliasName);
    FSRef aliasRef;
    FSCreateResFile(&parentRef, aliasName.length, aliasName.unicode, 0, NULL, &aliasRef, NULL);

    // Construct alias data to write to resource fork.
    FSRef targetRef;
    CFURLGetFSRef((CFURLRef)[NSURL fileURLWithPath:destFolder], &targetRef);
    AliasHandle aliasHandle = NULL;
    FSNewAlias(NULL, &targetRef, &aliasHandle);

    // Add the alias data to the resource fork and close it.
    ResFileRefNum fileReference = FSOpenResFile(&aliasRef, fsRdWrPerm);
    UseResFile(fileReference);
    AddResource((Handle)aliasHandle, 'alis', 0, NULL);
    CloseResFile(fileReference);

    // Update finder info.
    FSCatalogInfo catalogInfo;
    FSGetCatalogInfo(&aliasRef, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL);
    FileInfo *theFileInfo = (FileInfo*)(&catalogInfo.finderInfo);
    theFileInfo->finderFlags |= kIsAlias; // Set the alias bit.
    theFileInfo->finderFlags &= ~kHasBeenInited; // Clear the inited bit to tell Finder to recheck the file.
    theFileInfo->fileType = kContainerFolderAliasType;
    FSSetCatalogInfo(&aliasRef, kFSCatInfoFinderInfo, &catalogInfo);
}

I consider this code to be in the public domain. Please feel free to copy and paste. And let me know if you find any problems or have suggestions.

If you need a complete solution, Nathan Day wrote a nice set of classes called NDAlias. We didn’t want to import 9 classes for just a handful of unit tests.

I later found some of Apple’s sample code from 1999 demonstrating a similar approach. I think our Objective-C example is much easier to use.

3 Comments
  1. Aug 28 2009
  2. greg
    Sep 2 2009

    You may also use the source code of osxutils or the corresponding command line tool to make Finder aliases. See:

    http://codesnippets.joyent.com/posts/show/1745

  3. Sep 4 2009

    Amazing how I can search for something for hours and not find it. Thanks for the link. The source code for osxutils is:

    http://osxutils.cvs.sourceforge.net/viewvc/osxutils/osxutils/src/mkalias.c?view=markup

    The code is more complete than what I’ve got shown above, but is also much longer.

Comments are closed.