Launching an app with openApplicationAtURL

As written, the following script displays a dialog stating “The File Can’t be Found”. I want to eliminate the commented-out code and to have openApplicationAtURL return some value indicating whether the app was launched. Is this possible? Thanks!

use framework "AppKit"
use framework "Foundation"
use scripting additions

set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set theApp to current application's |NSURL|'s fileURLWithPath:"/Does/Not/Exist.app"
-- set fileExists to theApp's checkResourceIsReachableAndReturnError:(missing value)
-- if fileExists is false then
-- 	display alert "An error has occurred" message "The app could not be found"
-- 	return
-- end if
set startOptions to current application's NSWorkspaceOpenConfiguration's configuration()
theWorkspace's openApplicationAtURL:theApp configuration:startOptions completionHandler:(missing value)

BTW, I’ve considered the launchApplication and launchApplicationAtURL methods. They return a value indicating if the app was launched as I want, but they are deprecated.

There is a primitive API to open an URL in NSWorkspace, open(_:), this works also with applications. Of course the other API is preferred, but unfortunately AppleScript doesn’t support completion blocks.

use framework "AppKit"
use framework "Foundation"
use scripting additions

set theWorkspace to current application's NSWorkspace's sharedWorkspace()
set theApp to current application's |NSURL|'s fileURLWithPath:"/Does/Not/Exist.app"
set success to theWorkspace's openURL:theApp
2 Likes

Thanks Stefan. I’ll use your suggestion, which works well.