How do I remove the Finder from the Dock automatically at login? This is related to this question here, except that, in addition to removing the Finder, I want to do it automatically. There are several solutions on the web that suggest using this AppleScript:

tell application "System Events"
    tell UI element "Finder" of list 1 of process "Dock"
        perform action "AXShowMenu"
        click menu item "Remove from Dock" of menu 1
    end tell
end tell


Unfortunately, saving this as an application and having it run at login doesn't work; interestingly, running it within Script Editor does work, but this doesn't solve the problem of removing the Finder automatically.
The solution is to create a LaunchAgent (man launchctl for more info) that runs the above script via the command-line. Essentially, you want to store the script above in a file, say 'killfinder.scpt' and then run the command osascript /path/to/killfinder.scpt. 

We want to do this automatically at login, so we use a LaunchAgent. Since I want to use this because I use Pathfinder and my wife doesn't, I'm going to put it in ~/Library/LaunchAgents, but if you want this to be system wide, put it in /Library/LaunchAgents.

You will have three files:


A UNIX script
An AppleScript (optional)
A LaunchAgent XML plist


Script File


Put this in an appropriate path. For example, I stored it in ~/dev/scripts/killfinder.bash.
Create the file and add this content:

#!/bin/bash
osascript /path/to/AppleScriptFile.scpt

Make sure the permissions are correct (i.e. chmod ug+x killfinder.bash)


AppleScript File

You can call osascript directly with AppleScript code as an argument. I leave that as an exercise to the reader; in that case, you can creating this file. But if you want to do it this way:
1. Put this in an appropriate path.
2. Create the file and add this content:

    tell application "System Events"
        tell UI element "Finder" of list 1 of process "Dock"
            perform action "AXShowMenu"
            click menu item "Remove from Dock" of menu 1
        end tell
    end tell



Make sure the permissions are correct (i.e. chmod ugo+r Finder\ Remove.scpt)


LaunchAgent XML plist


Put this in ~/Library/LaunchAgents or /Library/LaunchAgents depending if you just want this behavior for your personal account or system-wide.
Create a file called com.&lt;whateveryouwant&gt;.KillFinder.plist with the following content:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
   &lt;key&gt;Label&lt;/key&gt;
   &lt;string&gt;com.&lt;whateveryouwant&gt;.KillFinder&lt;/string&gt;
   &lt;key&gt;Program&lt;/key&gt;
   &lt;string&gt;/path/to/unixscript&lt;/string&gt;
   &lt;key&gt;RunAtLoad&lt;/key&gt;
   &lt;true/&gt;
&lt;/dict&gt;
&lt;/plist&gt;


It's important to make sure the 'string' value after the Label key is correct as well as the path to the unix script.


Now all you have to do is exit and login and it should work!