Can't append Spotlight Comments after -writeToFile:atomically
Can't append Spotlight Comments after -writeToFile:atomically
- Subject: Can't append Spotlight Comments after -writeToFile:atomically
- From: Jerry Krinock <email@hidden>
- Date: Fri, 23 Feb 2007 14:45:40 -0800
- Thread-topic: Can't append Spotlight Comments after -writeToFile:atomically
I write data to a new file using -[NSData writeToFile:atomically]] and then,
following tips from the list archives, append some Spotlight/Finder comments
to the new file, using an NSAppleScript.
Although the AppleScript executes without error, the Spotlight Comments do
not actually get written unless I set a timer to write the comments 1
millisecond after writing the file.
Additional Clues:
- Does not matter if atomically: is YES or NO
- Sleeping main thread for 1.0 seconds does not help
- Without the timer, if I read the Spotlight Comments (using another
AppleScript) immediately after writing them, I see the desired
comments that I have written, even though these comments did not
out make it to the hard disk.
I presume that all the 1 millisecond timer does is cause the AppleScript to
execute after the current run loop (the one writing the file) has completed.
Can anyone explain this behavior? I hate to release a design which is based
solely on experiments.
Thanks,
Jerry Krinock
FUNCTIONS TO SET, GET, APPEND SPOTLIGHT COMMENTS:
enum SSErr SSSetPathComment(NSString* path, NSString* comment) {
enum SSErr err = kNoError ;
if (path) {
// Create the AppleScript
NSString *scriptSource = [NSString stringWithFormat:
@"on run\r tell application \"Finder\"\r set theFile to (\"%@\")
as alias\r set comment of theFile to \"%@\"\r end tell\r end run\r",
[path hfsPath],
comment] ;
NSAppleScript *appleScript = [[NSAppleScript alloc]
initWithSource:scriptSource];
// Run the AppleScript
NSDictionary *scriptError = nil ;
NSAppleEventDescriptor* result = [appleScript
executeAndReturnError:&scriptError] ;
if(!result || scriptError) {
SSLog(0, "SSSetPathComment got AppleScript error:\n%@
result:\n%@", [scriptError description], result) ;
err = kAppleScriptFailed ;
}
[appleScript release];
}
return err ;
}
enum SSErr SSGetPathComment(NSString* path, NSString** comment) {
enum SSErr err = kNoError ;
NSDictionary *scriptError = nil;
// Create the AppleScript
NSString *scriptSource = [NSString stringWithFormat:
@"on run\r tell application \"Finder\"\r set theFile to (\"%@\") as
alias\r get comment of theFile\r end tell\r end run\r",
[path hfsPath]] ;
NSAppleScript *appleScript = [[NSAppleScript alloc]
initWithSource:scriptSource];
// Run the AppleScript
NSAppleEventDescriptor* result = [appleScript
executeAndReturnError:&scriptError] ;
if (result) {
*comment = [result stringValue] ;
}
else if ([[scriptError objectForKey:NSAppleScriptErrorNumber] intValue]
!= fnfErr) {
SSLog(0, "SSGetPathComment got script error %@", [scriptError
description]) ;
err = kAppleScriptFailed ;
*comment = nil ;
}
else {
// File was not found
*comment = nil ;
}
[appleScript release];
return err ;
}
enum SSErr SSAppendPathComment(NSString* file, NSString* comment) {
enum SSErr err = kNoError ;
NSString* oldComment ;
err = SSGetPathComment(file, &oldComment) ;
if (err <= kNoError) {
NSString* delimiter = ([oldComment length] > 0) ? @"\n" : @"" ;
NSString* newComment = [[NSString alloc] initWithFormat:@"%@%@%@",
oldComment, delimiter, comment] ;
err = SSSetPathComment(file, newComment) ;
}
return err ;
}
_______________________________________________
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