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

DavidBlack

macrumors 6502a
Original poster
Jan 27, 2013
606
239
Somewhere In Apple's HQ ;)
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
 
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.
 
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.

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?
 
Thanks for pointing this out to me. Would I use this to move the files?

Code:
 [[NSFileManager defaultManager] moveItemAtURL:source toURL:destination error:nil];

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.

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?

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:).
 
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:).
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);
 
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);

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];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.