Re: Writing Spotlight comments
Re: Writing Spotlight comments
- Subject: Re: Writing Spotlight comments
- From: Pierre Bernard <email@hidden>
- Date: Mon, 12 Feb 2007 16:01:15 +0100
Thanks a bunch John!
Pierre
On 12 Feb 2007, at 14:43, John Pannell wrote:
Hi Pierre-
I have been using AppleScript to write to the Finder's "Spotlight
Comments" field of files on disk. The AppleScript itself looks
like this:
on SetSpotlightComment(pathToFile, newComment)
tell application "Finder"
set p to pathToFile as POSIX file
set comment of p to newComment
end tell
end SetSpotlightComment
The script must be stored somewhere as an NSAppleScript object to
call, the url below is the location on disk (I kept it in my app
bundle)...
_commentScript = [[NSAppleScript alloc] initWithContentsOfURL:url
error:&errors];
The setup to run the script in Cocoa is a mess, and I cobbled it
together from things I found on the web:
// write spotlight comment
NSDictionary *errors = [NSDictionary dictionary];
NSAppleEventDescriptor *firstParameter =
[NSAppleEventDescriptor descriptorWithString:_storagePath];
NSAppleEventDescriptor *secondParameter =
[NSAppleEventDescriptor descriptorWithString:[NSString
stringWithFormat:@"%@, %@, %@", [_spider rootString], _sourceURL,
_parentWebPage]];
// create and populate the list of parameters
// note that the array starts at index 1
NSAppleEventDescriptor *parameters = [NSAppleEventDescriptor
listDescriptor];
[parameters insertDescriptor:firstParameter atIndex:1];
[parameters insertDescriptor:secondParameter atIndex:2];
// create the AppleEvent target
ProcessSerialNumber psn = { 0, kCurrentProcess };
NSAppleEventDescriptor *target = [NSAppleEventDescriptor
descriptorWithDescriptorType:typeProcessSerialNumber bytes:&psn
length:sizeof(ProcessSerialNumber)];
// create an NSAppleEventDescriptor with the method name
// note that the name must be lowercase (even if it is
uppercase in AppleScript)
NSAppleEventDescriptor *handler = [NSAppleEventDescriptor
descriptorWithString:[@"setSpotlightComment" lowercaseString]];
// last but not least, create the event for an AppleScript
subroutine
// set the method name and the list of parameters
NSAppleEventDescriptor *event = [NSAppleEventDescriptor
appleEventWithEventClass:kASAppleScriptSuite
eventID:kASSubroutineEvent targetDescriptor:target
returnID:kAutoGenerateReturnID transactionID:kAnyTransactionID];
[event setParamDescriptor:handler forKeyword:keyASSubroutineName];
[event setParamDescriptor:parameters forKeyword:keyDirectObject];
// at last, call the event in AppleScript
if(![[_spider commentScript] executeAppleEvent:event
error:&errors])
[[_spider session] logComment:@"Could not set Finder
Spotlight comment" forResourceID:[self resourceID]];
Hope this helps!
John
On Feb 12, 2007, at 5:07 AM, Pierre Bernard wrote:
Is there an API for writing Spotlight comments (aka. Finder
comments) for files?
- - -
Houdah Software s. à r. l.
http://www.houdah.com
- Quality Mac OS X software
- Premium WebObjects consulting
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden