NSFileManger to move items on desktop

Discussion in 'Mac Programming' started by DavidBlack, Oct 20, 2013.

  1. DavidBlack macrumors 6502a

    DavidBlack

    Joined:
    Jan 27, 2013
    Location:
    Somewhere In Apple's HQ ;)
    #1
    Hi, guys I have this really simple question I have this apple script that basically helps to clean and tidy your desktop. It creates folder in the documents folder and names the folder by month e.g "Desktop October" then moves all the items on your desktop to that folder. My question is how I am going to convert this to objective-c. I have tried the NSFileManger but had no luck, can someone help me?


    Code:
    display alert ("How DesktopTidy Works") message ("DesktopTidy moves all your files to your documents folder and organizes them by month. So you get a clean desktop every day, but your files are never lost and you can get back to them anytime you want!") buttons {"Continue"} default button "Continue"
    if button returned of result is "Continue" then set foldername to ("Desktop " & month of (current date))
    set docsfolder to (path to documents folder) as string
    
    tell application "Finder"
    	if not (exists folder (docsfolder & foldername)) then
    		make new folder at docsfolder with properties {name:foldername}
    	end if
    	move items of (path to desktop folder) to folder (docsfolder & foldername)
    end tell
     
  2. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #2
    NSFileManager is certainly one of the classes you would use. Others include: NSURL, NSDate, NSDateFormatter, NSArray, NSDictionary. Plus whatever kind of UI you'd want for this functionality.

    The algorithm you have in your AppleScript pretty much outlines the algorithm you would use in Objective-C. It's just that the details are a bit different. Under a native solution you don't need to do anything with Finder, for example.
     
  3. DavidBlack thread starter macrumors 6502a

    DavidBlack

    Joined:
    Jan 27, 2013
    Location:
    Somewhere In Apple's HQ ;)
    #3
    Thanks for pointing this out to me. Would I use this to move the files?

    Code:
     [[NSFileManager defaultManager] moveItemAtURL:source toURL:destination error:nil];
    But the problem is that I don't want to move the desktop but rather the files on the desktop. Can you maybe show me how to do this?
     
  4. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #4
    Yes (unless you are dealing with paths as NSStrings, in which case you can use moveItemAtPath:toPath:error: instead). Make sure that the destination path includes the target filename - you are providing the new path/URL for the source item, not the path of its new parent folder.

    You need to get the contents of the desktop and then move each of those items individually. Have a look at NSFileManager's contentsOfDirectoryAtPath:error: method (or the equivalent method for URLs). If you use contentsOfDirectoryAtPath, note that it returns an array of filenames rather than full paths, so you'll need to append each of them to the path for the desktop (using NSString's stringByAppendingPathComponent: or NSURL's URLByAppendingPathComponent:).
     
  5. DavidBlack thread starter macrumors 6502a

    DavidBlack

    Joined:
    Jan 27, 2013
    Location:
    Somewhere In Apple's HQ ;)
    #5
    Thanks for your awesome reply this is what I have so far:

    Code:
      NSString *desktopDirPath = [NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        
        NSArray *filePaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[desktopDirPath stringByAppendingPathComponent:@"/"] error:nil];
        NSLog(@"%@",filePaths);
     
  6. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #6
    You don't need to append a "/" to the desktop path. In fact, you very rarely need to directly check for or add the path separator yourself - stringByAppendingPathComponent will add it automatically if needed, and most of the path related methods work with or without one. So you can simplify that line to:
    Code:
    NSArray *filePaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:desktopDirPath error:nil];
    As a side note, if you're only looking to append a string, you can use stringByAppendingString: instead; stringByAppendingPathComponent is only used for adding path components.

    Once you have the array of paths, you'll probably want to do something like the following to iterate through them and move them (roughly - I haven't tested this). destFolderPath is the path to the destination folder (with or without a trailing '/').

    Code:
    for (NSString *filename in filePaths)
    {
        NSString *sourcePath = [desktopDirPath stringByAppendingPathComponent:filename];
        NSString *destPath = [destFolderPath stringByAppendingPathComponent:filename];
        [[NSFileManager defaultManager] moveItemAtPath:sourcePath toPath:destPath error:nil];
    }
     

Share This Page