【问题标题】:Determine if Finder can be safely killed确定是否可以安全地杀死 Finder
【发布时间】:2017-04-02 09:47:26
【问题描述】:

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

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

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

如果无法做到这一点,是否有更安全的方法来刷新(重新启动)Finder?

谢谢!

【问题讨论】:

  • 您确定要强行杀死它,而不是发送一个退出事件,然后发送一个激活事件?
  • 我不知道这实际上是可能的。我应该为此使用 AppleScript 吗?您可以发布一个示例作为答案吗?这对我有帮助
  • 看看this answer能不能帮到你(obj-C,Cocoa)。或者,使用 AppleScript,看看 this answer 是否可以帮助您(obj-C,AppleScript)。

标签: swift macos swift2 finder


【解决方案1】:

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

由于无法为 Finder 观察 NSRunningApplication 对象(该对象在我移除观察者之前由于终止而被 deinitialized),我最终从 NSWorkspace.sharedWorkspace().notificationCenter 观察 NSWorkspaceDidTerminateApplicationNotification

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

当我的控制器是deinitialized 时,我可以删除这个观察者,并且选择器看起来像这样:

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)
    }
}

【讨论】:

    猜你喜欢
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多