Search code examples
cocoansworkspace

Launching apps programmatically


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.


Solution

  • 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;