How do I run a sudo command at login/logout on macOS
Unless the files are created with sudo
the user can delete files within his own profile.
So, creating a LaunchAgent that cleans the desktop at login would do the trick.
Login in with the account you want to use for guests.
Create a small script with the following code and save it as /usr/local/bin/cleanDesktop.sh
.
#!/bin/bash
rm -rf /Users/$(whoami)/Desktop/*
Make it executable by running chmod +x /usr/local/bin/cleanDesktop.sh
.
Now create a LaunchAgent to run your script.
Save the code below as ~/Library/LaunchAgent/com.local.cleanDesktop.plist
and run launchctl load ~/Library/LaunchAgent/com.local.cleanDesktop.plist
from the command line.
<?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>com.local.cleanDesktop</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/cleanDesktop.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Warning this code deletes stuff and I did not test it. So use with caution.
Also, the user could delete this code. Alternatively you could save the file in /Library/LaunchAgents
. Then it won't be editable for standard users. But it will run for all users.