Moving folders/files to trash

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

  1. macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #1
    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
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    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:

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

    kainjow

    Joined:
    Jun 15, 2000
    #3
    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. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #4

    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
     
  5. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #5
    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