2

I'm completely new to Applescript and currently very busy at work. I don't have time to learn it, I will once I find some free time, so I decided to ask you, the experts, for help.

I need an Applescript that will delete all desktop files upon log out. I'm currently running a Mac Lab with 40 iMacs and our students constantly save filed to Desktop, which creates a lot of clutter.

  • 1
    Ok, my friend cracked it and this works (the command lines are separated by commas): echo '#!/bin/bash, rm -rf ~/Desktop/*' > ~/.clean_desktop.sh, chmod +x ~/.clean_desktop.sh, sudo defaults write com.apple.loginwindow LogoutHook ~/.clean_desktop.sh, To uninstall: rm ~/.clean_desktop.sh, sudo defaults delete com.apple.loginwindow LogoutHook – Greg Nov 4 '13 at 6:34
1

Is there any specific reason why you wouldn't want to have your students use a Guest Account on the system instead? That would allow you to provide them restricted access to the system while deleting all their files on logout automatically.

To enable a guest user:

  • Go to System Preferences > Users & Groups
  • Click the lock icon to unlock it if necessary and type an administrator name and password
  • Select the Guest User in the list of accounts from the left sidebar
  • Check the option Allow guests to log in to this computer
  • You can also setup restrictions through Parental Controls by checking the option Enable parental controls and then clicking on Open Parental Controls...

See:
Using the Guest Account in OS X
OS X Mountain Lion: Set up guest users
OS X Lion: Set up a guest account

  • Thanks a lot but there are several reasons why I don't want to use Guest Account. First, you can only use one guest account and I need to set up several accounts for different groups of students (I know it sounds strange but these are the school's requirements) each allowing access to different applications. Secondly, I'm having problems with the dock. Every time I change it in Guest Account (leaving only the apps I want my students to use) it changes when I log out. Standard accounts, on the other hand, save the dock preferences so it looks the same every time you log in. – Greg Nov 4 '13 at 6:31
  • I see that you have adopted a different approach, but I'm leaving this answer here for others who may have a similar question. – M K Nov 4 '13 at 12:17
  • I've also added another solution that is easier to manage as a separate answer. – M K Nov 4 '13 at 12:18
1

You could use a shell script like this instead:

#!/bin/bash

rm -r ~/Desktop/*

Or if you want to move the files to trash:

#!/usr/bin/osascript

tell app "Finder" to move items of desktop to trash

Then run:

chmod +x /path/to/script
sudo defaults write com.apple.loginwindow LogoutHook /path/to/script

Logout hooks were deprecated in 10.4, but I haven't found any way to reliably run a program at logout using launchd.

The logout hook can only be an executable script and not a shell command.

The defaults command modifies /var/root/Library/Preferences/com.apple.loginwindow.plist. Run sudo defaults delete com.apple.loginwindow LogoutHook to delete the LogoutHook key.

1

Your basic requirement is to clear files between login sessions. If you cannot use Guest Accounts, since you would be doing some custom setup for the user accounts anyway, a simple solution that is easy to manage is to use a Startup Item script that runs on login instead of a logout hook (which is deprecated and may not remain a long term solution).

Whenever another user logs in, the files created by the previous user on the desktop would be cleared.

  • Create a script with the name cleardesktop.command (the .command extension makes it run in Terminal by default) containing the line

    rm -rf ~/Desktop/*
    
  • Make it executable by using the command

    chmod +x cleardesktop.command
    
  • Add it to the user account's Startup Items list

    • go to System Preferences > Users & Groups
    • select the user account and go to Login Items
    • click the (+) below the list on the right.
    • select the cleardesktop.command script and click Add
    • check the Hide checkbox next to it so that the Terminal window does not show up on login.
  • Additionally, you can prevent users from removing this Startup Item with the following steps

    • go to System Preferences > Parental Controls
    • then to the Apps tab
    • checking the Limit Applications checkbox
    • in the Allowed Apps: list, uncheck System Preferences - Applications

If you wish to make this solution a bit more robust, you can place the cleardesktop.command file in /usr/bin/ and set restricted permissions with chmod 755 cleardesktop.command from the administrator account.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .