Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one relaunch Finder programmatically?

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.

Additionally, I would like to restart Spotlight as well.

like image 703
i_am_jorf Avatar asked Sep 22 '09 20:09

i_am_jorf


2 Answers

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

EDIT: 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];

(...end EDIT)

//tell Finder to activate
restartFinder = [[NSAppleScript alloc] initWithSource:@"tell application \"Finder\" to activate"];
[restartFinder executeAndReturnError:nil];
like image 152
Rob Avatar answered Sep 27 '22 18:09

Rob


Finder is kept alive by the system, so you can just kill it and it will automatically relaunch. I use killall Finder to accomplish this.

like image 42
Dave DeLong Avatar answered Sep 27 '22 18:09

Dave DeLong