Function never called!

Hi,
I use this routine to launch an app and bring it to the foreground. It seems to work except that the “LaunchingAppForeground” function (AddressOf LaunchingAppForeground) is never called!
How to solve this problem ?

#If TargetMacOS
  Const appBundleID As String = "com.apple.Safari" //for example
  
  Declare Function NSClassFromString Lib "Foundation" (name As cfstringref) As ptr
  
  // Get the shared instance of the NSWorkspace class
  Declare Function getSharedWorkspace Lib "Foundation" Selector "sharedWorkspace" (cls As ptr) As Ptr
  
  Var workspace As ptr = getSharedWorkspace(NSClassFromString("NSWorkspace"))
  
  // Get the path to the application with our bundle identifier
  Declare Function URLForApplicationWithBundleIdentifier Lib "Foundation" Selector "URLForApplicationWithBundleIdentifier:" ( obj As ptr , bundleIdentifier As CFStringRef ) As Ptr
  
  Var url As ptr = URLForApplicationWithBundleIdentifier(workspace, appBundleID)
  
  // Make a default open configuration
  Declare Function configuration Lib "Foundation" Selector "configuration" (cls As ptr) As Ptr
  
  Var config As ptr = configuration(NSClassFromString("NSWorkspaceOpenConfiguration"))
  
  // Tell the application to open
  Declare Sub openApplicationAtURL_configuration_completionHandler Lib "Foundation" Selector "openApplicationAtURL:configuration:completionHandler:" ( obj As ptr , applicationURL As Ptr , configuration As Ptr , completionHandler As Ptr )
  
  Var handler As New ObjCBlock(AddressOf LaunchingAppForeground)
  
  openApplicationAtURL_configuration_completionHandler(workspace, url, config, handler.Handle)
#EndIf

Thanks.

So… the problem has to do with how this function works. The description says this:

Launches the app at the specified URL and asynchronously reports back on the app’s status.

Reporting back asynchronously means that the callback doesn’t happen on the main thread, which is where Xojo needs everything to be, otherwise it’ll crash.

Now, the fact that you’re not reporting crashes probably points to another issue…

What’s the signature of LaunchingAppForeground?

Thank you @Greg_O, but what do you call the “signature”?
“Launching App Foreground” is a method that I placed in a module (I tested outside the module and it is identical). Is the routine compatible with API 2?

Oh yes, I forgot to specify that there are two parameters…

@

In that case, consider yourself lucky that your app isn’t outright crashing when that callback is made.

There are two solutions I can think of:

  1. Create a timer and periodically check to see if the app actually launched up to a certain timeout. NSWorkspace provides other functions that you can declare against to find that out.
  2. Use a plugin or dylib which encapsulates this method and calls the callback on the main thread.

Other than that you’re left with mostly dangerous hacks.

Option 3: use the Folderitem.Launch method on the executable.

He’s looking for a way to know if it actually launched.

Just to add a shortcut here after reading Greg’s recommendation #2, for users with MBS it is available with the NSWorkspaceMBS plugin: NSWorkspaceMBS.launchApplicationAtURL

1 Like

You know, he never actually says that - he just happens to be using a function that reports back.