PDA

View Full Version : User Cleanup at Logout or Shutdown/Restart


ndpete
09-09-2010, 03:44 PM
First off I'm pretty new in the apple scripting area so all the advice in the world is welcome.

Basically What I'm trying to do is have a script run at logout or restart/shutdown that will cleanup a student account.

I started by trying to run an application as a login item for the user but I've found some conflicts with trying to reset the finder preferences. I've tried to quit finder and start it again but i still get an error.

Anyone have some advice on how to modify this script to work at login or how to run one similiar to this at logout or shutdown?

This is what I have so far: I run a script before making a master copy of the plists to a directory then use those to copy them back


#Clear the Desktop and Downloads folders
do shell script "rm -rf /Users/$USER/Desktop/*"
do shell script "rm -rf /Users/$USER/Downloads/*"

#Empty Trash
do shell script "rm -rf /Users/$USER/.Trash/*"

#Reset the Dock
do shell script "cp -af /Users/$USER/Library/CleanupScript/dock.plist /Users/$USER/Library/Preferences/com.apple.dock.plist"
do shell script "cp -af /Users/$USER/Library/CleanupScript/dock.db /Users/$USER/Library/Preferences/com.apple.dock.db"
tell application "Dock" to quit

#Reset Finder Preferences
tell application "Finder" to quit
do shell script "cp -af /Users/$USER/Library/CleanupScript/finder.plist /Users/student/Library/Preferences/com.apple.finder.plist"
do shell script "cp -af /Users/$USER/Library/CleanupScript/sidebar.plist /Users?$USER/Library/Preferences/com.apple.sidebarlists.plist"
tell application "Finder" to open

This was going to run at login but a modified one could be run at logout for the same results I think. In my testing everything worked except the Finder part.

Thanks for the help
-NDPETE

renaultssoftware
09-09-2010, 07:54 PM
Hi and welcome to the forums.

Using this documentation (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html) some people have made applications. Like this (direct download link (http://www.macchampion.com/scenario_files/scenario.dmg)).

I've contacted a few friends regarding Scenario. The main dude at MacChampion told me he would kind of like to work on the the project.

It works, but barely.

tw
09-09-2010, 09:14 PM
You're obviously more familiar with shell scripting, and this is a task that's better handled by a logout hook anyway, so I suggest you try the following. write a shell script as follows (the login window process puts the current user's name in the first parameter):

#! /bin/bash

#Clear folders
rm -rf /Users/$1/Desktop/* /Users/$1/Downloads/* /Users/$1/.Trash/*"

#Reset the Dock
cp -af /Users/$1/Library/CleanupScript/dock.plist /Users/$1/Library/Preferences/com.apple.dock.plist
cp -af /Users/$1/Library/CleanupScript/dock.db /Users/$1/Library/Preferences/com.apple.dock.db

#Reset Finder Preferences
cp -af /Users/$1/Library/CleanupScript/finder.plist /Users/student/Library/Preferences/com.apple.finder.plist
cp -af /Users/$1/Library/CleanupScript/sidebar.plist /Users?$1/Library/Preferences/com.apple.sidebarlists.plist

save it at /path/to/shellscript.sh (whatever path is convenient for you). then do the following commands in terminal from an administrator account (the first makes the script executable, and the second sets up the logout hook):

sudo chmod u+x /path/to/shellscript.sh
sudo defaults write com.apple.loginwindow LogoutHook /path/to/shellscript.sh

see this (http://www.bombich.com/mactips/loginhooks.html) and this (http://support.apple.com/kb/HT2420?viewlocale=en_US). Applescript (much as I love it) is just going to get in your way here.

And renaultssoftware - I get that you are excited about your new project, but please don't toss it out as a solution to everything. :D

renaultssoftware
09-10-2010, 06:41 AM
Well it seems to be a solution to everything! :p

tw
09-10-2010, 10:18 AM
Well it seems to be a solution to everything! :p

I believe you are confusing it with beer. :rolleyes:

tlarkin
09-10-2010, 10:37 AM
I believe you are confusing it with beer. :rolleyes:

quoted for truth!!! :cool::):D


I would also use the log out hook and a shell script, but I would perhaps also look at using MCX. Then you could just ditch this whole log out hook script thing, and set a local policy for the machine exactly how you want it.

ndpete
09-10-2010, 11:21 AM
The Logout hook was what i think i was looking for. I'll have to try that out today and see how it goes

I haven't done much with MCX. Actually probably nothing, i think the closest was cloning parental controls in 10.6 with dscl . -mcxexport and such haha I still have a lot to learn.

Thanks for the help guys I'll test the logout hook and see how it goes.

-ndpete

ndpete
09-15-2010, 10:04 AM
Well the logout hook works like a charm. Thanks for the info!