Re: Creating an alias
Re: Creating an alias
- Subject: Re: Creating an alias
- From: Greg Robbins <email@hidden>
- Date: Tue, 4 Mar 2003 13:28:07 -0800
Here's how to make and send the Apple event directly to the Finder, without extra dependencies.
- (void)makeFinderAliasForItem:(FSRef*)aliasTarget atFolder:(FSRef*)aliasLocation withName:(NSString*)name
{
// make new alias file to (file "aliasTarget") at (folder "aliasLocation")
// with properties { name: name }
OSType finderSignature = 'MACS';
OSErr err;
AliasHandle hAliasTarget = NULL;
AliasHandle hAliasLocation = NULL;
NSAppleEventDescriptor *targetDesc = [NSAppleEventDescriptor descriptorWithDescriptorType:typeApplSignature
bytes:&finderSignature
length:sizeof(OSType)];
NSAppleEventDescriptor *appleEvent = [NSAppleEventDescriptor appleEventWithEventClass:kAECoreSuite
eventID:kAECreateElement
targetDescriptor:targetDesc
returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID];
AppleEvent replyEvent = { typeNull, 0 };
require(appleEvent, Bail);
err = FSNewAlias(nil, aliasTarget, &hAliasTarget);
require_noerr(err, Bail);
HLock((Handle) hAliasTarget);
err = FSNewAlias(nil, aliasLocation, &hAliasLocation);
require_noerr(err, Bail);
HLock((Handle) hAliasLocation);
NSAppleEventDescriptor *aliasTargetDesc = [NSAppleEventDescriptor descriptorWithDescriptorType:typeAlias
bytes:*hAliasTarget
length:(**hAliasTarget).aliasSize];
NSAppleEventDescriptor *aliasLocationDesc = [NSAppleEventDescriptor descriptorWithDescriptorType:typeAlias
bytes:*hAliasLocation
length:(**hAliasLocation).aliasSize];
NSAppleEventDescriptor *aliasObjClass = [NSAppleEventDescriptor descriptorWithTypeCode:typeAlias];
[appleEvent setParamDescriptor:aliasObjClass forKeyword:keyAEObjectClass];
[appleEvent setParamDescriptor:aliasTargetDesc forKeyword:keyASPrepositionTo];
[appleEvent setParamDescriptor:aliasLocationDesc forKeyword:keyAEInsertHere];
if ([name length])
{
const char* utf8name = [name UTF8String];
NSAppleEventDescriptor *nameDesc = [NSAppleEventDescriptor descriptorWithDescriptorType:typeUTF8Text
bytes:utf8name
length:strlen(utf8name)];
NSAppleEventDescriptor *recordDesc = [NSAppleEventDescriptor recordDescriptor];
[recordDesc setParamDescriptor:nameDesc forKeyword:keyAEName];
[appleEvent setParamDescriptor:recordDesc forKeyword:keyAEPropData];
}
/* wanna see the event? log it this way:
Handle hResult = nil;
AEPrintDescToHandle([appleEvent aeDesc], &hResult);
NSLog([NSString stringWithCString:*hResult length:GetHandleSize(hResult)]);
DisposeHandle(hResult);
*/
err = AESend([appleEvent aeDesc], &replyEvent, kAEWaitReply,
kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
Bail:
if (hAliasTarget) DisposeHandle((Handle) hAliasTarget);
if (hAliasLocation) DisposeHandle((Handle) hAliasLocation);
}
Greg Robbins
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.