The end game is to make this a self service item but I can't even get it to work right when run locally.
If I run the zsh zmv command in the interactive shell, it works perfectly. Whatever folder I apply the command to it fixes all illegal characters. But in the script below, zmv fails and it fails silently. No characters are substituted in any folder/file names.
Also, if I change the script to first cd to $FOLDER, the zmv command does it's job. But it doesn't fix illegals in the name of $FOLDER--just all child files and folders...I want it to work the way it does in the interactive shell.
1 #!/bin/bash
2 USER="$(stat -f%Su /dev/console)"
3 #logged in user chooses a folder
4 FOLDER=$(sudo -u $USER -H /usr/bin/osascript << EOD
5 tell application "System Events"
6 activate
7 set FolderName to POSIX path of (choose folder with prompt "Please choose a folder to sanitize")
8 end tell
9 EOD)
10 #now that a folder has been chosen, we run zmv command to remove, by brute force, all spaces, slashes and other illegals and replace them with underscore
11 /bin/zsh -c "autoload zmv && zmv '(**/)(*)' '$1${2//[^A-Za-z0-9.]/_}'" $FOLDER
12 osascript -e 'display notification "Your Folder has been sanitized" with title "JAMF Management Notification"'
13 exit 0
14