How to create a logon script to delete folder, subfolders and contents when a user logs on ?

I need to create a logon script which will delete a folder, subfolder and contents when a user logs on. I have no experience with scripting so any pointers you can give would be much appreciated.

Thanks

Mac OS X (10.6.6)

Posted on May 5, 2011 2:43 AM

Reply
5 replies

May 5, 2011 4:32 AM in response to STM_College

The principle:


Write a script that does the deletion

Save as an application

Add the application to login items for the user [system prefs>Accounts>Login Items]


Here's an example that deletes a folder ''testfolder' - and all its contents - from the desktop. I should point out that the folder will still be in the trash. (You can add 'empty trash' after the delete to deal with that.)


tell application "Finder"

set theFolder to ((path to desktop as text) & "testfolder")


deletefoldertheFolder

end tell

May 5, 2011 7:02 AM in response to STM_College

Hi,

Thanks for the advice - I'll elaborate a bit further. We have a suite of iMacs with Final Cut Express installed on them - students are having problems when attempting to run it (same problem as post below)

https://discussions.apple.com/thread/2619060?answerId=12549756022#12549756022&messageID=12549756

What I want to do is delete the 'Final Cut Express Data' folder and subfolders when a student logs on (~/Library/Preferences/Final Cut Express Data) - hopefully this will work around the issue FCE has with network accounts.

Thanks

May 5, 2011 8:26 AM in response to STM_College

depending on how you have thing set up, it might be easier to make a LaunchAgent to handle this. do this:

  1. copy the text below into a text editor
  2. save it as a plain text file in /Library/LaunchAgents with the file name "user.startup.folderDeleter.plist" (the name doesn't matter so much, but the 'plist' extension is required)
  3. load the plist into launchd by restarting the machine or by opening terminal and running the command launchctl load /Library/LaunchAgents/user.startup.folderDeleter.plist

This will delete the folder any time any user logs in. You could also expand this to delete the folder periodically (once a day, for instance) if that would be helpful.


Note, this file must be saved as plain text. apps like TextEdit sometimes default to making rich text files which will not work. Either download a programmer's text editor like TextWrangler, or make sure that TextEdit is using plain text (if the window has a formatting toolbar it's using rich text; select "Make Plain Text" from the Format menu).


copy the text below:


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>user.startup.folderDeleter</string>

<key>RunAtLoad</key>

<true/>

<key>ProgramArguments</key>

<array>

<string>osascript</string>

<string>-e</string>

<string>tell application "Finder" to delete folder "Final Cut Express Data" of folder (path to preferences from user domain)</string>

<string>-e</string>

<string>tell application "Finder" to empty trash</string>

</array>

</dict>

</plist>

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to create a logon script to delete folder, subfolders and contents when a user logs on ?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.