Question

I'm working on a little app that acts as a plugin for a 3rd party app. For it to function, I need to access a saved file from the 3rd party app ("other app" from here). In newer versions, the other app keeps this in ~/Application Support/Test App/Saved Files/.

This is easy to go to, using something like this:

NSString *savedFiles = [NSHomeDirectory() stringByAppendingPathComponent:@"~/Application Support/Test App/Saved Files"];

But I also want to make it compatible with older versions, and they don't use the application support folder. Older versions went in their folder in the user's applications folder, for example this structure: ~/Applications/Test App/Test App.app ~/Applications/Test App/Saved Files/

I can use the same as above to find the location to saved files. But the app won't always be installed here, or the folder name could be different in a different language. This is the problem.

I thought so far, I thought the following are options to get the path:

  1. Ask the user to navigate to the file with a file selector from within my app.
  2. Ask the user where the "Test App.app" is, and get the path via "../Saved Files/" relative to that.
  3. Can the app get the path to a previously run application via the system?
  4. Can the app use spotlight to quickly find the location?
  5. Are there other ways to do this automatically, reliably, and quickly?

1 and 2 are not preferred, as that would require the user to "work" to give the app the path. What do you all think? Are there other ways to automate this? Which would be the fastest?

Was it helpful?

Solution

You can try to ask LaunchServices for the app's location, given its bundle identifier with the LSFindApplicationForInfo call:

CFURLRef urlRef = NULL;
LSFindApplicationForInfo(kLSUnknownCreator, (CFStringRef)@"com.example.some-app", NULL, NULL, &urlRef);
NSLog(@"URL: %@", (__bridge id)urlRef);

This assumes that the bundle identifier is the same between the two variants you mention. If it's really just the name that is the same, you could query by name instead:

CFURLRef urlRef = NULL;
LSFindApplicationForInfo(kLSUnknownCreator, NULL, (CFStringRef)@"Test App.app", NULL, &urlRef);
NSLog(@"URL: %@", (__bridge id)urlRef);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top