5

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.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222

4 Answers4

5

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];
Rob
  • 1,288
  • 13
  • 22
  • You've got a memory leak. Plus, there's no need to activate Finder. It will restart automatically. – Dave DeLong Sep 22 '09 at 21:23
  • matt... the first script is 'quit', the second is 'activate'. The sample code is leaky for sure. Finder does not reactivate without being told in at least Snow Leopard. Not positive about prior OS, but I'm pretty sure a "truly" quit Finder stays quit. – Rob Sep 22 '09 at 21:33
  • @Barry touché, but i think it will always feel wrong to see an alloc/init and no release... – Dave DeLong Sep 22 '09 at 22:20
  • You may need to put in a delay (or maybe an AppleScript try block) between the calls. If the Finder isn't done quitting yet, it probably can't be activated. – Rob Sep 22 '09 at 22:24
  • So, it doesn't seem like "tell application \"Finder\" to activate" is working for me. – i_am_jorf Sep 23 '09 at 17:41
  • Yeah, telling it to activate doesn't work on 10.5 or 10.6... how does one start Finder again? – i_am_jorf Sep 23 '09 at 18:00
  • I had to switch "activate" to "launch" and do it twice(?) but I got it working. If you have further advice, let me know. – i_am_jorf Sep 23 '09 at 19:40
  • 1
    I added some extra code above. What's happening is that the Finder cannot accept an activate event if it's still in the process of quitting cleanly. A 1 second delay always works perfectly on my setup. (With no delay, sometimes it would still activate.) – Rob Sep 23 '09 at 21:28
3

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.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 1
    Yeah, Explorer on Windows is similar, but randomly killing Explorer can lead to bad things; much better to shut it down cleanly. – i_am_jorf Sep 22 '09 at 22:11
-1

'Relaunch' almost certainly just sends a kill signal to the Finder.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
-1

Killing Finder with killall Finder works since the system will automatically relaunch it.

[[NSTask launchedTaskWithLaunchPath:@"/usr/bin/killall" 
    arguments:[NSArray arrayWithObjects:@"Finder", nil]] waitUntilExit];
Reimund
  • 2,346
  • 1
  • 21
  • 25