Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locating Mac OS X folders using URLForDirectory instead of FSFindFolder

I have some old code that finds specific folders using the old FSFindFolder routine. Now I have the task of modernizing this code. At first using NSFileManager's URLForDirectory seem like the right choice. Unfortunately I found out that many of the folders you could locate using FSFindFolder are no longer supported by URLForDirectory.

The list of folders you can locate using FSFindFolder is very long and most of them are not useful to my anyway.

Here is a partial list of FSFindFolder constants that I do need to convert and their URLForDirectory equivalents:

FSFindFolder                    URLForDirectory
==============================  ===============
kDesktopFolderType              NSDesktopDirectory
kCachedDataFolderType           NSCachesDirectory
kApplicationSupportFolderType   NSApplicationSupportDirectory
kTemporaryFolderType            located by calling NSTemporaryDirectory()
kCurrentUserFolderType          located by calling NSHomeDirectory()
kSystemFolderType               Not Available
kPreferencesFolderType          Not Available
kTrashFolderType                Not Available
kAudioComponentsFolderType      Not Available
kVolumeRootFolderType           Not Available
kSharedUserDataFolderTypeNo     Not Available

My question: is there a standard way to locate paths to each of the folders marked "Not Available" without using FSFindFolder?

like image 845
Periodic Maintenance Avatar asked Oct 16 '13 13:10

Periodic Maintenance


People also ask

How do I find a folder in Mac terminal?

To do that you use the ls (or list) command. Type ls and press the Return key, and you'll see the folders (and/or files) in the current directory.

How do I open a file path on a Mac?

This is done by double-clicking on your Macintosh hard drive icon from the desktop. Next, go to the View menu and choose Show Path Bar. The Show Path Bar option is grayed out if a Finder window is not open.

How do you copy a file path on a Mac?

Right click (control + click or two finger click on trackpad) on a Folder a context menu with options will popup. Hold the option key then the options will change and you would be able to see the option: Copy “folder_name” as Pathname, click on it, the full path of the file/folder would be copied to the clipboard.


2 Answers

I would recommend that modernizing code will, in most cases, mean shifting away from whatever you were using these folder locations for in the first place. For example, there's not much legitimate use for an equivalent of kSystemFolderType in modern code. You should examine each case where you're using these and ask what the right solution is to solve the high-level problem that that old implementation was solving.

If you really do need paths to some of these folders, your best bet is to look at the path that FSFindFolder() gives, find which NSSearchPathDirectory gets you closest, and then just write your code to get that from URLForDirectory:... and append the difference as a static relative path.

Some specific recommendations:

kPreferencesFolderType: use NSUserDefaults to store preferences; store non-defaults in Application Support

kTrashFolderType: use -[NSWorkspace recycleURLs:completionHandler:] or -performFileOperation:source:destination:files:tag: with NSWorkspaceRecycleOperation; only if necessary, use -[NSFileManager URLForDirectory:...] with NSTrashDirectory

kVolumeRootFolderType: if you're using this with a specific volume ref rather than a domain, use -[NSURL getResourceValue:forKey:error:] with key NSURLVolumeURLKey

kSharedUserDataFolderType: -URLForDirectory:... with NSUserDirectory and then append @"Shared" as a path component

kAudioComponentsFolderType: the Component Manager is deprecated; Apple says there's no exact replacement, but for some purposes Audio Component Services is appropriate.

like image 114
Ken Thomases Avatar answered Sep 27 '22 18:09

Ken Thomases


I'm going off of the legacy documentation for the definitions of the FSFindFolder constants. I will do the best I can to make educated guesses about how they map, so if I am wrong, please clarify and I'll update my answer.

[NSFilemanager URLForDirectory:inDomain:appropriateForURL:create:error:] (documentation)can help you find some of these items. It can find:

NSApplicationDirectory
NSDemoApplicationDirectory
NSDeveloperApplicationDirectory
NSAdminApplicationDirectory
NSLibraryDirectory
NSDeveloperDirectory
NSUserDirectory
NSDocumentationDirectory
NSDocumentDirectory
NSCoreServiceDirectory
NSAutosavedInformationDirectory
NSDesktopDirectory
NSCachesDirectory
NSApplicationSupportDirectory
NSDownloadsDirectory
NSInputMethodsDirectory
NSMoviesDirectory
NSMusicDirectory
NSPicturesDirectory
NSPrinterDescriptionDirectory
NSSharedPublicDirectory
NSPreferencePanesDirectory
NSApplicationScriptsDirectory
NSItemReplacementDirectory
NSAllApplicationsDirectory
NSAllLibrariesDirectory
NSTrashDirectory

In User, Local, Network and System domains.

That should take care of kTrashFolderType.

As far as I can tell, kSystemFolderType will always be the System folder in the root directory (eg /System) so you shouldn't have to search for it. kPreferencesFolderType specifies the Preferences folder in the System Folder. I'm not sure what this means, as there is no "preferences" folder in /System/Library There is however a folder at /Library/Preferences. This too is a static location.

Not sure about kAudioComponentsFolderType, kVolumeRootFolderType, and kSharedUserDataFolderTypeNo off-hand.

like image 39
BergQuester Avatar answered Sep 27 '22 20:09

BergQuester