我不知道为什么,但是下面的代码为什么没有预期的行为呢?
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)
}
}当我运行代码时,Safari应用程序通常是打开的,“Hides”和“Activates”false都不起作用。
Obs:应用程序沙箱:不需要部署目标: 11.3 \ Xcode: 12.5.1
发布于 2021-09-01 00:45:07
在这里,您需要打开应用程序两次:
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)
}您没有在第一次使用配置,所以它在前台打开它而不隐藏。既然它已经启动了,第二次打开它就什么也做不了了。
只需删除workspace.open(bundleURL)即可。对我起作用了。
https://stackoverflow.com/questions/69006440
复制相似问题