How can I program finder programmatically?

If I am OPTION + RIGHT CLICKon the Finder icon, I get the “Restart” option in the context menu. I would like to programmatically restart Finder, if at all possible. I am sure there is a better way to do this than simply kill it and restart it. Suppose I have the appropriate permissions / permissions for this.

In addition, I would also like to restart Spotlight.

+5
source share
4 answers

Send the quit event using AppleScript, then send it an activation event:

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

EDIT: , , Finder . , :

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

, , . killall Finder .

+3

'Relaunch' Kill Finder.

-1

Killing Finder killall Finderworks with , as the system will automatically restart it.

[[NSTask launchedTaskWithLaunchPath:@"/usr/bin/killall" 
    arguments:[NSArray arrayWithObjects:@"Finder", nil]] waitUntilExit];
-1
source

All Articles