如果我在 Finder 图标上 OPTION + RIGHT CLICK
,我会在上下文菜单中看到“重新启动”选项。如果可能的话,我想以编程方式重新启动 Finder。我确信有比杀死它并让它重新启动更好的方法。假设我已经有适当的授权/许可来这样做。
此外,我还想重新启动 Spotlight。
最佳答案
使用 AppleScript 向它发送一个退出事件,然后向它发送一个激活事件:
//tell Finder to quit
NSAppleScript *restartFinder = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to quit"];
[restartFinder executeAndReturnError:nil];
编辑:添加延迟以确保 Finder 已准备好接收激活事件。在我的机器上,有时需要这种延迟,有时不需要:
//delay 1 second
restartFinder = [[NSAppleScript alloc] initWithSource:@"delay 1"];
[restartFinder executeAndReturnError:nil];
(...结束编辑)
//tell Finder to activate
restartFinder = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to activate"];
[restartFinder executeAndReturnError:nil];
关于objective-c - 如何以编程方式重新启动 Finder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1462425/