NSWorkspaceMBS.launchApplication problem with Apple apps

I want to launch an application with NSWorkspaceMBS with the “NSWorkspaceLaunchAndHide” option but I encounter various problems:
The examples offered on the NSWorkspaceMBS page only work with third-party apps. Apple Apps (all) display this error message:
“Error: Unable to launch the application ‘Address Book.app’ because it was not found.”
How to get past it? Is there a solution ?
I tried:

NSWorkspaceMBS.launchApplicationAtFile(file as folderitem
and NSWorkspaceMBS.launchApplicationAtURL(URL as string,
with dim URL as String = ("file:///Applications/TextEdit.app")

Only the command:

NSWorkspaceMBS.launchApplication(appname as string) as boolean 

works with Apple apps (TextEdit, for example, launches well) but it does not have the “Options” parameter!).
Does anyone have a solution? (or an explanation)

The path is wrong!

correct path is “/System/Applications/TextEdit.app”.

Finder shows /Applications and /System/Applications together.

1 Like

Thank you, @Christian_Schmitz, it works but I need to do 2 routines:
If the user launches an Apple app:
dim file as String = (“file:///” + “/System/Applications/TextEdit.app”) /
If it’s a third-party app:
dim file as String = (“file:///” + “Applications/Firefox.app”)
How do I distinguish an Apple app from a non-Apple app? I will have to test the BundleID of each App…

you could check if the file exists in /Applications and if not, look into /System/Applications.

1 Like

There’s also another method in NSWorkspace to get the path of an application by its bundleID. that would take care of situations where a user had an app in an uncommon location as well.

1 Like

There is no need for the application path as Greg already mentioned.
Based on this discussion you can just use the CFBundleIdentifier:

Public Function LaunchAppFromBundleID(bundID as String) As Boolean
  Declare Function NSClassFromString Lib "Foundation" ( className As CFStringRef ) As Ptr
  Declare Function sharedWorkspace Lib "AppKit" Selector "sharedWorkspace" ( ref As Ptr ) As Ptr
  Declare Function URLForApplicationWithBundleIdentifier Lib "AppKit" Selector "URLForApplicationWithBundleIdentifier:" ( NSWorkspace As Ptr , bundleIdentifier As CFStringRef ) As Ptr
  Declare Function absoluteString Lib "AppKit" Selector "absoluteString" ( NSURLRef As Ptr ) As CFStringRef
  Declare Function launchApp Lib "AppKit" Selector "launchAppWithBundleIdentifier:options:additionalEventParamDescriptor:launchIdentifier:"_
  ( NSWorkspaceRef As Ptr, bundleIdentifier As CFStringRef, options As Ptr, descriptor As Ptr, identifier As Integer ) As Boolean
  Dim URLRef As Ptr = URLForApplicationWithBundleIdentifier( sharedWorkspace( NSClassFromString( "NSWorkspace" ) ), bundID)
  If URLRef <> Nil Then
    Dim f As folderitem = GetFolderItem( absoluteString( URLRef ), folderItem.pathTypeURL )
    If f <> Nil And f.exists Then
      Call launchApp( sharedWorkspace( NSClassFromString( "NSWorkspace" ) ), "com.apple.TextEdit", Nil, Nil, 0 )
      Return True
  Return False  
End Function

and call it via

Call LaunchAppFromBundleID("com.apple.TextEdit")

No need for a Plugin here.