objective-c - 如何以编程方式重新启动 Finder?

标签 objective-c macos finder relaunch

如果我在 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/

相关文章:

cocoa - 以编程方式打开 Mac 帮助菜单

swift - 将包含多个文件路径的 Swift 字符串转换为 AppleScript 列表

macos - 如何在cocoa中访问finder侧边栏共享​​内容

ios - 表格 View 页脚中的按钮没有被调用

iphone - 如何在 iPhone 通话期间播放声音

linux - MAC OS X 别名文件夹/文件不会在 Windows 上保留

xcode - 使自定义文档在 macOS Finder 中显示正确的 "Kind"属性,而不是 "DocumentType"

iphone - 我的 UIView 上已经有渐变,插入新渐变时遇到问题

objective-c - 从带有参数的方法名称创建选择器

perl - Perl如何与正在运行的脚本进行交互?