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

Determine if Finder can be safely killed

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

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

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

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

谢谢!

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

由于无法观察 Finder 的 NSRunningApplication 对象(在我可以移除观察者之前,由于终止而对象被 deinit 初始化),我最终观察 NSWorkspaceDidTerminateApplicationNotification来自 NSWorkspace.sharedWorkspace().notificationCenter

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

当我的控制器 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)
    }
}