4
votes

I have been playing around with NSWorkspace.

  • [[NSWorkspace sharedWorkspace] launchApplication:@"Photos"] works

  • [[NSWorkspace sharedWorkspace] launchApplication:@"Contacts"] does not work

  • [[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Contacts.app"] works

(Mac OS Sierra)

So, I was wondering if there is any safe way to find the "real" application path. Maybe the Contacts app has different names in different localization of the System ? Why is just "Photos" working fine ?

I would imagine that there should be a way, from an Apple documented application identifier to get the application path, but I found nothing.

2

2 Answers

4
votes

Remember that launchApplication: returns a BOOL if it succeeded or not.

To be really certain of the app you are launching, you can also try looking it up via NSWorkspace's URLForApplicationWithBundleIdentifier: API, into which you can plug Contact's application ID of com.apple.AddressBook.

With the result, you can call the explicit location of the app via:

- (NSRunningApplication *)launchApplicationAtURL:(NSURL *)url options:(NSWorkspaceLaunchOptions)options configuration:(NSDictionary<NSString *,id> *)configuration error:(NSError * _Nullable *)error;

0
votes

The best way to open an app is from file url path. I have did the same for my application like below:

NSString *appPathIs = Url.path;

I take url path in one string and then open it with as you have indicated

[[NSWorkspace sharedWorkspace] launchApplication:appPathIs];

I have tried every way to achieve the same but sometime they work and sometime not. If you want to try another one then please consider this:

NSString *appPathIs = [[NSWorkspace sharedWorkspace] fullPathForApplication:appName];
NSString *identifier = [[NSBundle bundleWithPath:appPathIs] bundleIdentifier];
NSString *path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:identifier];

[[NSWorkspace sharedWorkspace] launchApplication:appPathIs];