Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SRossi

macrumors regular
Original poster
May 27, 2009
202
0
Glasgow, Scotland
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:

Code:
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
 
I don't know what docs you're reading, but the online docs seem pretty clear:

http://developer.apple.com/mac/libr...rmFileOperation: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.
 
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.
 
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
 
...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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.