четверг, 17 октября 2013 г.

Getting current user's desktop path on Mac OS

The simplest solution is
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES );
NSString* theDesktopPath = [paths objectAtIndex:0];

But in the case of running the application in sudo mode it gives us the root's desktop path. To fix it use
 NSString* home = [[[NSProcessInfo processInfo] environment] objectForKey:@"HOME"];
 NSString *pathToDesktop = [NSString stringWithFormat:@"%@/Desktop", home];