sudo defaults write com.apple.loginwindow LoginHook /bin/loginscript.sh
Any script you put in /bin called loginscript.sh will run every time a user logs in. It runs as root, too, so you can have it do all kinds of cool stuff. Now on to the script (I've also included an uncommented version at the end).Here's the generic, commented script. The first part of the script will delete all of the current printers. We do this because we might need to read a printer with new info. UNIX allows multiple queues with the same name, which can get confusing. The list of printers is stored in /etc/printcap. We'll parse /etc/printcap with sed to create a script to delete the current printers. sed is a UNIX command line tool for substituting text, among other things. We'll use lpadmin -x "Printer Name" to delete the printers. Now all of the printers have been deleted
Adding a printer with lpadmin is easy. The flags I use are:
- -p "Printer name"
- -L "location"
- -E enabled
- -v device name (IP Address in this case)
- -P "Printer Description"
/usr/sbin/lpadmin -p G-Wing-HP -L G104 -E -v \
lpd://192.168.2.2 -P \
"/Library/Printers/PPDs/Contents/Resources/en.lproj/HP\ LaserJet 4050 Series.gz"
Set the default printer like this
lpoptions - d G-Wing-HP
You can add as many as you'd like, just make sure you add the printer before you try to set it as the default. You can also add via ipp instead of lpd. We don't use AppleTalk any more, but you can add AppleTalk printers with a line like this:
lpadmin -p Printer_Display_Name -E -v \
pap://Zone/Printer-Name-With-Dashes/LaserWriter -P /Path/To/PPD
Once the script is running at login, you can update it with Apple Remote Desktop, or by editing the script remotely via ssh. The script resets the queues at every login, so any 'stuck' print jobs are cleared up by a logout/login. New printers can be added and are immediately available after login. You can add multiple printers with the same IP address and different PPDs for custom jobs.
We use this scripting as the basis for a Platypus app that will change printers on demand. If a teacher with a laptop visits a different building, they need only open an app in the Dock and they get the printers for that building. When they return, they can open the app for the original building, and the printers are restored. When imaging, we use a variation on this that sets printers via hostname. It gets a little trickier, but I can post this if anyone is interested.
Here's an uncommented version of the script from above.

