gpt4 book ai didi

swift - 确定是否可以安全地杀死 Finder

转载 作者:搜寻专家 更新时间:2023-11-01 05:35:49 26 4
gpt4 key购买 nike

我目前正在开发一个实用程序,需要在对用户的默认设置进行一些更改后重新启动 Finder。

为了安全起见,我想在调用killall Finder(通过NSTask)之前检查Finder是否忙。如果 Finder 正在复制文件或其他忙,我想阻止该操作并稍等片刻。

在 macOS 10.10+ 上的 Swift 2.3 中,有没有办法确定 Finder 是否忙碌或者是否可以安全地终止它?

如果这不可能,是否有更安全的方法让我刷新(重新启动)Finder?

谢谢!

最佳答案

感谢@dfri 的评论,我能够找到一种方法(尽管不完全是链接答案中提供的方法)来做到这一点。

由于观察 Finder 的 NSRunningApplication 对象是不可能的(在我可以删除观察者之前,由于终止而对象被 deinitialized),我最终观察到 NSWorkspaceDidTerminateApplicationNotification 来自 NSWorkspace.sharedWorkspace().notificationCenter

NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: #selector(MyController.applicationWasTerminated(_:)), name: NSWorkspaceDidTerminateApplicationNotification, object: nil)

当我的 Controller 被deinit初始化时,我可以移除这个观察者,选择器看起来像这样:

func applicationWasTerminated(notification: NSNotification?) {
guard let notif = notification else { return }
guard let userInfo = notif.userInfo as? [String : AnyObject] else { return }
guard let identifier = userInfo["NSApplicationBundleIdentifier"] as? String else { return }
if identifier == "com.apple.finder" {
NSWorkspace.sharedWorkspace().launchAppWithBundleIdentifier("com.apple.finder", options: NSWorkspaceLaunchOptions.Default, additionalEventParamDescriptor: nil, launchIdentifier: nil)
}
}

关于swift - 确定是否可以安全地杀死 Finder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40678831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com