I have a script that I am trying to get to run at login. The script is meant to clear the contents of the Desktop, Documents, and Downloads folders.
I'm using a launch agent to trigger the script to run at load. When I run the script manually, everything functions as expected. All directories are cleared, just as they should be.
However, when that same script is triggered by a Launch Agent, stderr reports "no matches found: /Users/jdoe/Desktop/*" and stdout reports "Checking /Users/jdoe/Desktop". None of the other directories are even checked.
#!/bin/zsh
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Removes files from the specified directories
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Define Variables
userName="jdoe"
directories=("Desktop" "Documents" "Downloads")
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Define functions
removeFiles() {
local folder="/Users/$userName/$dir"
for file in "$folder"/*
do
echo "Removing $file"
/bin/rm -rf "$file"
done
}
clearFiles() {
date
for dir in "${directories[@]}"
do
echo "Checking folder $dir"
removeFiles
done
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# main
clearFiles
exit 0
Here is the Launch Agent which is saved at /Users/jdoe/Library/LaunchAgents/
<?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>domain.label</string>
<key>Program</key>
<string>/bin/zsh</string>
<key>ProgramArguments</key>
<array>
<string>/PATH/TO/script.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/PATH/TO/domain.label.filename.stderr</string>
<key>StandardOutPath</key>
<string>/PATH/TO/domain.label.filename.stdout</string>
</dict>
</plist>
I've also tried variations of the plist. For example:
<?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>domain.label</string>
<key>ProgramArguments</key>
<array>
<string>/PATH/TO/script.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/PATH/TO/domain.label.filename.stderr</string>
<key>StandardOutPath</key>
<string>/PATH/TO/domain.label.filename.stdout</string>
</dict>
</plist>
When the script is created the folowing function sets it's permissions:
setScriptPermissions() {
chown jdoe ${script}
chmod 755 ${script}
}
When the launch agent is created:
setAgentPermissions() {
chown jdoe "$plist"
chmod 644 "$plist"
}
Had to create a pppc profile that granted access to each folder to zsh.