Applescript loops when run from Self Service

xian
New Contributor II

I have an AppleScript that addresses issues with out-of-sync Active Directory passwords. It resets the secure token on an out-of-sync account by disabling and then re-enabling the token. I run it as a Self Service item but the problem is that once the script has finished, it loops back to the beginning and starts to run again. Not sure why it doesn't quit after the token has been re-enabled. If I run it in Script Editor, it doesn't loop, but from Self Service, it loops back to the beginning after the second verification. Thanks for any help with this one.

set the_folder to (path to users folder)
tell application "Finder" set foldernames to name of every folder of the_folder
end tell
set theChosenOne to choose from list foldernames with prompt "Select user needing a password sync"
display dialog "Password for " & theChosenOne default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue" with hidden answer
set thePassword to quoted form of text returned of the result
--disable secure token
do shell script "sysadminctl interactive -secureTokenOff " & theChosenOne & " -password " & thePassword
set theVerification to do shell script "sysadminctl interactive -secureTokenStatus " & theChosenOne & " 2>&1 | awk -F']' '{print $2}'"
display dialog theVerification

set the_folder to (path to users folder)
tell application "Finder" set foldernames to name of every folder of the_folder
end tell
set theChosenOne to choose from list foldernames with prompt "Re-select user needing a password sync"
display dialog "Password for " & theChosenOne default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue" with hidden answer
set thePassword to quoted form of text returned of the result
--re-enable secure token
do shell script "sysadminctl interactive -secureTokenOn " & theChosenOne & " -password " & thePassword
set theVerification to do shell script "sysadminctl interactive -secureTokenStatus " & theChosenOne & " 2>&1 | awk -F']' '{print $2}'"
display dialog theVerification
return

2 REPLIES 2

brunerd
Contributor

Well, you're not running this Applescript "raw", it's either #!/usr/bin/osascript or you are running the above chunk via heredoc or herestring in osascript (also single backticks for inline and triple backticks for code blocks help immensely), so it's hard to say why it's looping given this is just the Applescript guts with none of the shell wrapper that is likely around it.

So I'll just say that writing Applescript is tedious especially when it's doing things shell could do... so I wrote shui a shell wrapper that can handle all manner of user-interaction via Applescript but with the idioms of shell... there's lots of examples in the demo

xian
New Contributor II

Thanks brunerd. shui looks like a great timesaver and has been added to my toolbox. With this problem, It turned out that when I had split it into two policies, I hadn't removed the original script from the first policy after creating the second, so the script was looping because it was actually running twice. A Friday afternoon mistake only realized in the cold light of Monday morning.