如何以编程方式重新启动Finder? [英] How does one relaunch Finder programmatically?

查看:116
本文介绍了如何以编程方式重新启动Finder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Finder图标上OPTION + RIGHT CLICK,我会在上下文菜单中获得"重新启动"选项.如果可能的话,我想以编程方式重新启动Finder.我敢肯定,有一种比杀死它并让它重新启动更好的方法.假设我已经具有适当的授权/权限.

If I OPTION + RIGHT CLICK on the Finder icon, I get a "Relaunch" option in the context menu. I would like to programmatically relaunch Finder, if at all possible. I'm sure there is a better way to do it than to just kill it and let it restart. Assume I have the proper authorization / permissions to do so already.

此外,我也想重新启动Spotlight.

Additionally, I would like to restart Spotlight as well.

推荐答案

使用AppleScript发送退出事件,然后将其发送给激活事件:

Send it a quit event using AppleScript, then send it an activate event:

//tell Finder to quit
NSAppleScript *restartFinder = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to quit"];
[restartFinder executeAndReturnError:nil];

添加延迟以确保Finder准备好接收激活事件.在我的机器上,有时需要此延迟,有时则不需要:

add a delay to make sure Finder is ready to receive an activate event. On my machine, sometimes it needs this delay, sometimes it doesn't:

//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];

这篇关于如何以编程方式重新启动Finder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆