i_a*_*orf 5 macos finder objective-c relaunch
如果我OPTION + RIGHT CLICK在Finder图标上,我会在上下文菜单中看到" 重新启动 "选项.如果可能的话,我想以编程方式重新启动Finder.我确信有一个更好的方法来做它而不是杀死它并让它重新启动.假设我已经拥有适当的授权/权限.
另外,我也想重新启动Spotlight.
使用AppleScript向它发送一个退出事件,然后发送一个激活事件:
//tell Finder to quit
NSAppleScript *restartFinder = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to quit"];
[restartFinder executeAndReturnError:nil];
Run Code Online (Sandbox Code Playgroud)
编辑:添加延迟以确保Finder准备好接收激活事件.在我的机器上,有时它需要这种延迟,有时它不会:
//delay 1 second
restartFinder = [[NSAppleScript alloc] initWithSource:@"delay 1"];
[restartFinder executeAndReturnError:nil];
Run Code Online (Sandbox Code Playgroud)
(...结束编辑)
//tell Finder to activate
restartFinder = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to activate"];
[restartFinder executeAndReturnError:nil];
Run Code Online (Sandbox Code Playgroud)