Re: NSWorkspaceRecycleOperation and auto-renaming items moved to Trash
Re: NSWorkspaceRecycleOperation and auto-renaming items moved to Trash
- Subject: Re: NSWorkspaceRecycleOperation and auto-renaming items moved to Trash
- From: Kurt Revis <email@hidden>
- Date: Fri, 22 Feb 2002 17:19:40 -0800
On Friday, February 22, 2002, at 05:00 PM, John Blackburn wrote:
Is NSWorkspaceRecycleOperation supposed to automatically rename items
if necessary when they're moved to the Trash? I'm seeing it fail when
attempting to move an item to the Trash if another item of the same
name is already in the Trash. The documentation doesn't say it does,
but it seems a handy and appropriate feature.
You'd think it would do this, but it doesn't. Also, this method will
not cause the dock's Trash icon to get updated (so it will still appear
empty if it was empty to start with). I'm pretty sure I've filed a bug
on this.
If it's not supposed to automatically rename duplicate items, what's
the best way to get the Trash's path in Cocoa so I do it myself?
You don't want to move files directly to the Trash either--you will
still have problems getting the dock icon updated. The best way is to
send an AppleEvent to the Finder to tell it to move the files to the
trash itself, so you get the renaming and dock updating for free.
Here's my code to do this, which is based on some Apple sample code I
found somewhere:
#import <Carbon/Carbon.h>
- (BOOL)moveFilesToTrash:(NSArray *)filePaths;
{
// Send an AppleEvent to the Finder to move the files to the trash.
// This is a workaround for bugs in -[NSWorkspace
performFileOperation:NSWorkspaceRecycleOperation ...].
// Wheee.
OSErr err;
AppleEvent event, reply;
AEAddressDesc finderAddress;
AEDescList targetListDesc;
OSType finderCreator = 'MACS';
unsigned int filePathCount, filePathIndex;
FSRef fsRef;
AliasHandle aliasHandle;
// Set up locals
AECreateDesc(typeNull, NULL, 0, &event);
AECreateDesc(typeNull, NULL, 0, &finderAddress);
AECreateDesc(typeNull, NULL, 0, &reply);
AECreateDesc(typeNull, NULL, 0, &targetListDesc);
// Create an event targeting the Finder
err = AECreateDesc(typeApplSignature, (Ptr)&finderCreator,
sizeof(finderCreator), &finderAddress);
if (err != noErr) goto bail;
err = AECreateAppleEvent(kAECoreSuite, kAEDelete, &finderAddress,
kAutoGenerateReturnID, kAnyTransactionID, &event);
if (err != noErr) goto bail;
err = AECreateList(NULL, 0, false, &targetListDesc);
if (err != noErr) goto bail;
filePathCount = [filePaths count];
for (filePathIndex = 0; filePathIndex < filePathCount;
filePathIndex++) {
NSString *filePath;
filePath = [filePaths objectAtIndex:filePathIndex];
// Create the descriptor of the file to delete
// (This needs to be an alias--if you use
AECreateDesc(typeFSRef,...) it won't work.)
err = FSPathMakeRef((const unsigned char *)[filePath
fileSystemRepresentation], &fsRef, NULL);
if (err != noErr) goto bail;
err = FSNewAliasMinimal(&fsRef, &aliasHandle);
if (err != noErr) goto bail;
// Then add the alias to the descriptor list
HLock((Handle)aliasHandle);
err = AEPutPtr(&targetListDesc, 0, typeAlias, *aliasHandle,
GetHandleSize((Handle)aliasHandle));
HUnlock((Handle)aliasHandle);
DisposeHandle((Handle)aliasHandle);
if (err != noErr) goto bail;
}
// Add the file descriptor list to the apple event
err = AEPutParamDesc(&event, keyDirectObject, &targetListDesc);
if (err != noErr) goto bail;
// Send the event to the Finder
err = AESend(&event, &reply, kAENoReply, kAENormalPriority,
kAEDefaultTimeout, NULL, NULL);
// Clean up and leave
bail:
AEDisposeDesc(&targetListDesc);
AEDisposeDesc(&event);
AEDisposeDesc(&finderAddress);
AEDisposeDesc(&reply);
return (err == noErr);
}
Hope this helps.
--
Kurt Revis
email@hidden
_______________________________________________
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.