PDA

View Full Version : Moving folders/files to trash




SRossi
Oct 28, 2009, 07:11 PM
Hey all again,

Yet again looking for some wisdom, I am creating a smallish app that allows users to enter a path to a file say "~/Users/Docs" and on click of a button moves the folder and all its items to the trash.

Right now I have:

NSInteger tag;
NSString *string = [textField stringValue];
NSArray *files = [NSArray arrayWithObject:string]; // Unsure what to put here

[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:string destination:@"" files:files tag:&tag];

What I do not know is what I should put in the files part of the performFileOperation: It says in the documentation that this should be an array of items that you are working with, no path just an array of the items, but I do not know how to add just the folder to the array, is that even possible?

Any other hints/comments are as always thanked.

Thanks in advance,

Stephen



chown33
Oct 28, 2009, 09:48 PM
I don't know what docs you're reading, but the online docs seem pretty clear:

http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWorkspace/performFileOperation:source:destination:files:tag:

files
An array of NSString objects specifying the names of the files and directories to be manipulated. Each string must not contain any path information other than the name of the file or directory. In other words, all of the files and directories must be located in the source directory and not in one if its subdirectories.


If your goal is to move a single item "someDir/someItem" to the trash, I would make someDir be the source: parameter, and "someItem" be in an NSArray containing a single item.

There are NSArray convenience methods to create NSArray objects with various items. See the NSArray class reference docs and look for +arrayWithObject: . Google keywords: nsarray class reference.

kainjow
Oct 28, 2009, 11:40 PM
Snow Leopard added a new method called recycleURLs:completionHandler: which you could look into using if you don't need backwards compatibility. It is more UI-based though as the docs say it might pop up a window like the Finder, but it's far more direct in its use.

SRossi
Oct 29, 2009, 09:06 AM
If your goal is to move a single item "someDir/someItem" to the trash, I would make someDir be the source: parameter, and "someItem" be in an NSArray containing a single item.

There are NSArray convenience methods to create NSArray objects with various items. See the NSArray class reference docs and look for +arrayWithObject: . Google keywords: nsarray class reference.


That was the same docs as I looked at but I am trying to delete a directory not just certain items in the directory. What the user will do in the end is drag and drop a folder into the app and that will add the folder to be moved to the trash.

And kainjow it needs 10.5 support I had originally used that but i was asked for it to have 10.5 support.

Thanks so far,

Stephen

xStep
Oct 29, 2009, 03:13 PM
...I am trying to delete a directory not just certain items in the directory.

The document states that files are: An array of NSString objects specifying the names of the files and directories to be manipulated.

So it seems to me that you have to strip the directory you are trying to delete from the source path and add it to the files array. You would also use the shorter path.