Hey all. I finally got a chance to refine this little Self Service app and it works great. User can now pick a folder to sanitize in preparation for moving to the OneDrive Synch folder. Still have a lot of non-compliant Mac users who need to get important stuff in the Cloud. The script we have now in production doesn't give the user a choice. It automatically runs on their existing OneDrive Folder, which is okay, but I would like users to be able to run it in advance before they even sign into OneDrive for the first time to avoid synch errors right off the bat.
Before this gets into production, however I have two questions.
- How to best prevent a user from selecting any any folder outside their own home directory, where illegal character removal might break something.
- How to make it so this script also fixes illegals in the parent directory?
It may not seem much to ask someone to fix ONE illegal filename manually after the script fixes the other 3,000, but I want to make it bulletproof.
Here's the script as it stands. It runs perfectly as a Self Service Item in our test instance of the latest version of JAMF pro.
#!/bin/bash
##crowdsourced by teodle and jamfnation
USER="$(stat -f%Su /dev/console)"
#logged in user chooses a folder
FOLDER=$(sudo -u $USER -H /usr/bin/osascript << EOD
tell application "System Events"
activate
set FolderName to POSIX path of (choose folder with prompt "Please choose a folder to sanitize")
end tell
EOD)
echo "$FOLDER"
cd "$FOLDER"
#now that a folder has been chosen, we run zmv command AS THE USER to remove, by brute force, all spaces, slashes and other illegals and replace them with underscore
sudo -u $USER -H /bin/zsh -c "autoload zmv && zmv '(**/)(*)' '$1${2//[^A-Za-z0-9.]/_}'"
sudo -u $USER -H /bin/zsh -c "autoload zmv && zmv '(/) ()' '$1$2'"
osascript -e 'display notification "Your Folder has been sanitized" with title "JAMF Management Notification"'
exit 0