Oh just in case someone asks: why didn't I write it in zsh to begin with?
I tried, but the osascript part failed.
Hello.
I reworked the entire script so it runs entirely within zsh and uses more modern techniques.
1#!/bin/zsh
2
3# Folder Name Sanitiser
4# Original: jamf nation user - teodle
5# Modified: Richard Purves (franton) - 16th November 2019
6
7# Set the Jamf Management Action app location
8ma="/Library/Application Support/JAMF/bin/Management Action.app/Contents/MacOS/Management Action"
9
10# Find the current console user
11currentuser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )
12
13#logged in user chooses a folder
14folder=`sudo -u $currentuser -H /usr/bin/osascript << EOD
15tell application "System Events"
16activate
17set FolderName to POSIX path of (choose folder with prompt "Please choose a folder to sanitize")
18end tell
19EOD`
20
21# The choice has been made.
22# Use zmv command to remove all spaces, slashes and other illegals and replace them with underscore
23cd "$folder" ; autoload zmv && zmv '(**/)(*)' '$1${2//[^A-Za-z0-9.]/}'
24
25# Notify the user that we're done
26"$ma" -title "JAMF Management Notification" -message "Your Folder has been successfully sanitized"
1. The script now runs entirely within zsh rather than shelling out from bash. That improves response time.
2. We're using "the new hotness" for finding the current console user. This is far more reliable than your code, which can trip up over fast user switching and multiple users.
3. I've recoded the folder osascript code to be more reliable. The original in brackets didn't execute for me, but backticks made it work with the unix heredoc.
4. Since we're in zsh we don't have to shell out. Instead we "cd" to the chosen folder and then execute the zmv command to do all the renaming required.
5. Finally I replaced the final osascript for notification with the Jamf management action binary, as this works more reliably and looks like it ran in Self Service.
Hope that helps!
Thanks I certainly didn't expect someone to rewrite the script for me!!. If I ever make it to JNUC your money's no good at the bar/coffeeshop for at least one or two rounds.
However, my script did actually work if I cd $FOLDER like your script does.
I need to be able to also strip out illegals (if they exist) in the dirname of $FOLDER itself.
If I run this command directly in interactive shell and specify a folder (either directly or call a variable I defined for the shell session, it changes the foldername itself plus drills down recursively.
1/bin/zsh -c "autoload zmv && zmv '(**/)(*)' '$1${2//[^A-Za-z0-9.]/_}'" $FOLDER
So for example, if $FOLDER is "!@##all my files 2/25/19" it will fix that name as well as drill down recursively.
But in a script I have to cd to the chosen folder just like you did in order to get it to work, and then it doesn't change the name of the folder that the user can choose. I want to make this idiot-proof. The instructions are "pick a folder that you want to move inside your OneDrive Folder or other cloud sync folder. Run this self-service app to remove illegal characters in the chosen folder name and all nested subfolder and files names."
I have another script in production now that gets the logged in user (the old way), cds into their OneDrive sync folder and uses that same zmv syntax and it works great--it doesn't require any interaction because the OneDrive Folder is usually at the same path location--the root of the homedir. That script I was able to write in pure zsh because it doesn't use any osascript. The interactive script is for edge cases where someone changed the default OneDrive Sync folder. On setup, it does let you do that if you want.
But your method of getting logged in user is definitely new. My method is the one (I thought) was the latest accepted conventional wisdom from JAMF nation. But there's always a new and better way and I love learning new stuff!
So again thanks.
I ended up abandoning the idea of letting the end user choose a folder and run zmv substitution. The final solution is to run the script on their OneDrive folder only, because zmv is sooo powerful and fast and most characters that are illegal in OneDrive sync folder are just fine anywhere else on a Mac filesystem.