Posted on 05-05-2016 04:27 PM
I am trying to create a script to clean up student computers at the end of the school year, however if there is no file in say movies, then it will error and tell me "not such file or directory" and stop running. I would like the script to continue running even if there is no such file or directory found. Can someone help explain how I can get this script to continue working. Here is my script.
tell application "Terminal" do shell script "rm -r ~/Desktop/" delay 1 --pause for a sec do shell script "rm -r ~/Documents/" delay 1 --pause for a sec do shell script "rm -r ~/Pictures/" delay 1 --pause for a sec do shell script "rm -r ~/Movies/" delay 1 --pause for a sec do shell script "rm -r ~/Music/" delay 1 --pause for a sec do shell script "rm -r ~/Downloads/" delay 1 --pause for a sec tell application "Finder" to set desktop picture to POSIX file "/Library/Desktop Pictures/Solid Colors/Solid Aqua Dark Blue.png"
end tell
Posted on 05-06-2016 08:22 AM
A couple of notes: avoid "~" as that will just call the running users home directory, and if you elevate the script/program, it will perform the action on the elevated account. Another would be to just use a shell script rather than an Apple script that calls shell commands. The 100% safe way to do this whole thing would be to test if a directory is empty; if not then run rm -r, and if it is to just run rmdir. But truthfully since this sounds like a complete purge of an account, just use the "-f" flag to force the removal, and run it from the top of the home directory so that you avoid having to iterate and test each sub folder: rm -rf /Users/StudentA. Just remember to be careful because this will delete EVERYTHING.
I don't know about your current setup or policies/procedures, but it sounds like reimaging the machines would probably be best.
Posted on 05-06-2016 11:10 AM
good point avoid "~", I think I will change this, just to be safe!
Thanks for your input
Posted on 05-06-2016 01:36 PM
hey @TJ78620 in the same boat.. Not near a computer now, but the script I use does a ls on the /Users, grepping out local admins,Shared. Then loops thru the list removing the users. I am sure someone will post a similar script shortly, so hang tight
Larry