Script Editor error "not such file or directory"

TJ78620
New Contributor

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

3 REPLIES 3

andrew_nicholas
Valued Contributor

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.

TJ78620
New Contributor

good point avoid "~", I think I will change this, just to be safe!
Thanks for your input

Nix4Life
Valued Contributor

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