Reputation: 315
I don't know why, but why the code below does not have the expected behavior?
let workspace = NSWorkspace.shared
if let bundleURL = workspace.urlForApplication(withBundleIdentifier: "com.apple.Safari"){
workspace.open(bundleURL)
let conf = NSWorkspace.OpenConfiguration()
conf.activates = false
conf.hides = true
workspace.openApplication(at: bundleURL, configuration: conf){ (app, err) in
print(app as Any)
print(err as Any)
}
}
When I run the code, the Safari application is opening normally, neither “Hides” is working nor “Activates” false is working.
Obs: App Sandboxed: NO | Deployment Target: 11.3 | Xcode: 12.5.1
Upvotes: 1
Views: 483
Reputation: 270980
You are sort of opening the application twice here:
workspace.open(bundleURL) // first time
let conf = NSWorkspace.OpenConfiguration()
conf.activates = false
conf.hides = true
// second time
workspace.openApplication(at: bundleURL, configuration: conf){ (app, err) in
print(app as Any)
print(err as Any)
}
You didn't use the configuration the first time, so it opens it up in the foreground without hiding. And since it has already launched, opening it the second time doesn't do anything.
Just remove workspace.open(bundleURL)
. It worked for me.
Upvotes: 1