Programmatically Get Path to Application Support Folder

Programmatically get path to Application Support folder

This is outdated, for current best practice use FileManager.default.urls(for:in:) as in the comment by @andyvn22 below.

the Best practice is to use NSSearchPathForDirectoriesInDomains with NSApplicationSupportDirectory as "long winded" as it may be.

Example:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *applicationSupportDirectory = [paths firstObject];
NSLog(@"applicationSupportDirectory: '%@'", applicationSupportDirectory);

NSLog output:

applicationSupportDirectory: '/Volumes/User/me/Library/Application Support'

What is the cross-platform way of obtaining the path to the local application data directory?

You could probably say something like (contradict me if I am wrong, or if this a bad approach)

private String workingDirectory;
//here, we assign the name of the OS, according to Java, to a variable...
private String OS = (System.getProperty("os.name")).toUpperCase();
//to determine what the workingDirectory is.
//if it is some version of Windows
if (OS.contains("WIN"))
{
//it is simply the location of the "AppData" folder
workingDirectory = System.getenv("AppData");
}
//Otherwise, we assume Linux or Mac
else
{
//in either case, we would start in the user's home directory
workingDirectory = System.getProperty("user.home");
//if we are on a Mac, we are not done, we look for "Application Support"
workingDirectory += "/Library/Application Support";
}
//we are now free to set the workingDirectory to the subdirectory that is our
//folder.

Note that, in this code, I am taking full advantage that Java treats '/' the same as '\\' when dealing with directories. Windows uses '\\' as pathSeparator, but it is happy with '/', too. (At least Windows 7 is.) It is also case-insensitive on it's environment variables; we could have just as easily said workingDirectory = System.getenv("APPDATA"); and it would have worked just as well.

How to sent path Control to choose folder prompted value?

The data types don't match.

  • choose folder returns an AppleScript alias specifier.
  • The parameter of setURL is supposed to be a Cocoa NSURL instance.

To make NSURL from alias specifier first get the POSIX path string of the alias.

set PRJpath to POSIX path (choose folder)

Then create the url

pathPanel's setURL:(current application's NSURL's fileURLWithPath:PRJpath)

How do I read from /Library/Application Support/ folder?

/Library/Application Support is not in your bundle. The paths you get using [[NSBundle mainBundle] pathsForResourcesOfType:…] are only useful for accessing files inside your application itself (images, sounds, etc that you included when you built the app).

You want to use [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:error] to get a list of files in a directory outside your application.

Matt Gallagher has a great example of a fault-tolerant method of locating the path to your application support directory at Cocoa With Love. I would recommend using it over hardcoding the /Library/Application Support path.

NSError *error = nil;
NSArray *text = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fileLocation error:&error];
if (!text) {
NSLog( @"Error reading contents of application support folder at %@.\n%@", applicationSupportFolder, [error userInfo] );
}

Xcode 6 iPhone Simulator Application Support location

The simulator directory has been moved with Xcode 6 beta to...

~/Library/Developer/CoreSimulator

Browsing the directory to your app's Documents folder is a bit more arduous, e.g.,

~/Library/Developer/CoreSimulator/Devices/4D2D127A-7103-41B2-872B-2DB891B978A2/data/Containers/Data/Application/0323215C-2B91-47F7-BE81-EB24B4DA7339/Documents/MyApp.sqlite

iPhone Application Support Folder Include Files BEFORE Building

You can add these files in the Project and access these files at runtime Xcode will copy them in the Copy Bundle Resource phase. This normally copies into the root of the bundle. To deal with directories see @CocoaFu's answer to this SO question.

Then in the code

NSBundle* bundle = [NSBundle mainBundle] will give you the main bundle

From this you look in directories using pathForResource:ofType:inDirectory: e.g.

NSString* path = [bundle pathForResource:@"file.xml" 
ofType:nil
inDirectory:@"a"];

The methods are given in NSBundle class reference also see the Bundle Programming guide

Hope this solves your issue. If not please comment

Application's data folder in Mac

/Users/USERNAME/Library/Application Support/

Edit:

This answer has drawn a lot of upvotes despite its minimalistic nature. Therefore, I want to point out the things mentioned in the comments here to make them more visible:

  • There are several other folders being used for application data / configuration, as mentioned in this answer.
  • If writing an application, don't hardcode that path. Instead, use macOS' API to retrieve it. This question has several answers for both ObjectiveC and Swift.

Application support directory not found

It's recommended to use the FileManager related API to get the directory because you can create it on-the-fly if it's missing.

do {
let applicationSupportDirectory = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let demoPlistURL = applicationSupportDirectory.appendingPathComponent("demo.plist")
} catch {
print(error)
}

The result demoPlistURL is an URL rather than a String.



Related Topics



Leave a reply



Submit