Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting desktop path for current user on macOS

I use

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

It works well. But when I launch the application with sudo it gives the root's desktop path. Is there any way to return current user's desktop always (even if the app is started with sudo)?

like image 963
amplifier Avatar asked Jul 05 '13 14:07

amplifier


People also ask

How do I find the Desktop path on a Mac?

On your Mac, click the Finder icon in the Dock to open a Finder window. Choose View > Show Path Bar, or press the Option key to show the path bar momentarily. The location and nested folders that contain your file or folder are displayed near the bottom of the Finder window.

What is desktop path?

Often when opening the command prompt window, you automatically be placed in the (username) directory. Therefore, you only need to type cd desktop to get into the desktop. If you're in any other directory, you would need to type cd \docu~1\(username)\desktop to get into the desktop.

How paths work in MacOS?

PATH is a system-level variable that holds a list of directories. When you enter a command in the terminal, it's shorthand for a program with the same name. The system looks in each of the PATH directories for the program corresponding to the command. When it finds a matching program, it runs it.


1 Answers

I use the statement below.

[NSURL fileURLWithPath:[NSHomeDirectory()stringByAppendingPathComponent:@"Desktop"]]
like image 59
larod Avatar answered Nov 15 '22 11:11

larod