2016-11-18 30 views
2

我目前正在开发一个实用程序,要求在对用户的默认设置进行一些更改后重新启动Finder。确定是否可以安全杀死Finder

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

有没有一种方法可以确定Finder是否忙或是否可以安全地在Swift 2.3中对macOS 10.10+进行处理?

如果这不可行,是否有更安全的方式来刷新(重新启动)Finder?

谢谢!

+0

您确定要强行杀死它,而不是送戒连t后跟一个激活的? – dfri

+0

我不知道这实际上是可能的。我应该使用AppleScript吗?你能举一个例子作为答案吗?这将帮助我 – beeb

+1

看看[这个答案](http://stackoverflow.com/a/10226948/4573247)可以帮助你(obj-C,Cocoa)。另外,使用AppleScript,看看[这个答案](http://stackoverflow.com/a/1462686/4573247)可以帮助你(obj-C,AppleScript)。 – dfri

回答

0

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

由于观察的Finder中NSRunningApplication对象是不可能的(对象是deinit ialized由于终止之前,我可以删除观察者),我结束了从NSWorkspace.sharedWorkspace().notificationCenter

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

观察NSWorkspaceDidTerminateApplicationNotification然后我可以删除当我的控制器deinit ialized,并且选择这个观察者是这样的:

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) 
    } 
} 
相关问题