[英]How does one relaunch Finder programmatically?
如果我在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];
Finder由系統保持活動狀態,因此您只需將其殺死,它就會自動重新啟動。 我使用killall Finder
完成此任務。
幾乎可以肯定,“重新啟動”只是向Finder發送一個終止信號。
使用killall Finder
殺死killall Finder
原因是系統會自動重新啟動它。
[[NSTask launchedTaskWithLaunchPath:@"/usr/bin/killall"
arguments:[NSArray arrayWithObjects:@"Finder", nil]] waitUntilExit];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.