Moving folders/files to trash

Discussion in 'Mac Programming' started by SRossi, Oct 28, 2009.

  1. SRossi macrumors regular

    May 27, 2009
    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:

    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,

  2. chown33 macrumors 604

    Aug 9, 2009
    on the Western Slopes, with E. A. Poe
    I don't know what docs you're reading, but the online docs seem pretty clear:

    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.
  3. kainjow Moderator emeritus


    Jun 15, 2000
    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.
  4. SRossi thread starter macrumors regular

    May 27, 2009
    Glasgow, Scotland

    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,

  5. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    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.

Share This Page