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

标签 swift macos swift2 finder

我目前正在开发一个实用程序,需要在对用户的默认设置进行一些更改后重新启动 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/

相关文章:

macos - 获取intellij 14(OSX Yosemite)中Gradle项目的环境变量?

ios - 如何向 ViewController 及其子级添加手势识别器?

ios - 如何完成/销毁不需要的 webview 进程?

java - 我应该为 Mac/Windows Java 应用程序使用什么打包工具?

excel - Excel VBA中mac的 sleep 等效项

ios - 构建 TableView ,同时从完成 block 获取行数

ios - 从 'AUAudioUnitBus?' 转换为无关类型 'UICollectionViewLayoutAttribute' 总是失败 - swift 2 iOS 9

Swift2 UI 测试 - 等待元素出现

Swift & Vapor/Fluent : How to pull values from another table during a post router request

ios - 从 FirebaseDB 获取值(value)