Globally launching items at login


A common need in a managed environment is to run certain scripts every time someone logs in, or to open certain items (apps, folders, documents). Apple has provided a method for each user to specify items to be opened at login, but it is not entirely obvious how to specify certain items to be opened or executed for all users of a given computer.
Fortunately, there is a simple way to do this.

As it turns out, the loginwindow.plist file, located at ~/Library/Preferences/loginwindow.plist, works the way you'd wish all preference files worked. This file contains the list of items to open at login. If you take this file, copy it to /Library/Preferences/ and make sure it is readable by everyone (chmod o+r /Library/Preferences/loginwindow.plist), the items you've specified to open at login will now be opened for every user of that machine. What's even better is that if a user specifies items to be opened at login (using the "My Account" (Jaguar) or "Accounts" (Panther) preference pane), the items defined in /Library/Preferences/loginwindow.plist AND the items defined for the specific user at ~/Library/Preferences/loginwindow.plist will be opened. So you can define items to be opened for every user of a machine without interfering with the ability for a user to define their own items.

This technique can be further refined. I have defined a single item to be opened by every user of the machines I manage. It's an AppleScript application I call "LoginLauncher". This application looks in a folder I've defined (/Library/FA/LoginItems/) and opens everything in it. It knows how to run AppleScripts, execute shell and Perl scripts, and open anything else the same way the Finder would. The advantage of this method is that you do not have to keep editing /Library/Preferences/loginwindow.plist - instead, simply add or remove items from /Library/FA/LoginItems/ to control what is open or executed at start up.

Here's the AppleScript for LoginLauncher:
set scriptPath to "/Library/FA/LoginItems" --change this to the path for your Login Items
set theFolder to POSIX file scriptPath
set folderPath to theFolder as text
tell application "Finder"
    repeat with aFile in folder folderPath
        copy kind of item (aFile as textto theKind
        if theKind is "script text" or theKind is "compiled script" or theKind is "Script" then
            --run an AppleScript
            run script file (aFile as text)
        else if (aFile as textcontains ".sh" or (aFile as textcontains ".pl" then
            --run a shell or Perl script
            copy POSIX path of file (aFile as textto scriptPath
            do shell script (ASCII character 34) & scriptPath & (ASCII character 34)
        else
            --just tell the Finder to open it
            open item (aFile as text)
        end if
    end repeat
end tell

Posted: Sat - November 15, 2003 at 08:26 PM      


©