Tuesday, December 2, 2008

Login scripts for individual users on MacOSX

I will demonstrate a simple way to create personalized user login scripts on MacOS Leopard. Using “login items” we can run a script with a .command extension but it is slow to launch at login and leaves an ugly terminal window at login. As an alternative MacOS allows Login Hook to attach a script to the login process but one thing it doesn’t do is allow for a different script for each user. To work around this issue with Login Hook, what you can do is create a login script for each user in their respective folders and the main login script will call the appropriate script for the user when they log in.


One issue with this procedure is that the script launches with root privileges so that may not be ideal. This specific example uses the say command for an audio greeting of the user by their name when they log in. There may be more elegant ways of doing this in Applescript perhaps, maybe someone would like to comment on that.


1. Login as an admin user in MacOSX who has access to sudo. The user created at install time should have this functionality by default


2. Open Terminal by going to Machintosh HD->Applications->Utilities->Terminal


3. Choose a place to store the main login script, I created a Startup folder in /Library and created a startup script called startup.sh

sudo mkdir /Library/Startup

sudo vi /Library/Startup/startup.sh

4. Add the following code which checks for the .startup file in the user's home folder and loads that script if it exists otherwise performs a default command

#!/bin/bash

if [ -e $HOME/.startup ]

then

$HOME/.startup

else if [ $# -gt 0 ]

then

/usr/bin/say welcome $1

fi

fi


5. Change the permissions on the script so it can be executed

sudo chmod u+x /Library/Startup/startup.sh


6. Now create the LoginHook to the script. Type the following command in terminal:

sudo defaults write com.apple.loginwindow LoginHook /Library/Startup/startup.sh


7. create .startup file in each users home directory

sudo vi /Users/username/.startup

chmod u+x /Users/username/.startup

Add the following code or whatever you would like to run at login for that user:

#!/bin/bash

/usr/bin/say welcome username


No comments: