> Can your repeat script always bring the user back to "What's the password" if the password is incorrect OR blank?
The problem is that AppleScript doesn't know if the password is incorrect. It knows if there was a password or not, but until it's passed to the shell script there's no way for AppleScript to know if what the user provided was correct.
That's not to say it can't be done, but it's a lot more complicated and you need to play some shell shenanigans, so let's dig in.
First up, you need to suppress the output of diskutil since we really don't care to hear what it has to say. You can do that by adding the following to the end of the diskutil command:
> /dev/null 2>&1
it looks weird, but the ' > /dev/null' tells the shell to send the normal output of the command to /dev/null (basically a black hole), and the ' 2>&1' tells it to do the same with any error messages.
so now the diskutil will run silently - whether or not it manages to mount the disk. So now we need some way to check whether it worked or not. For that we append another command to the shell script, namely:
echo $?
This command outputs the exit code of the previous command. Now, diskutil will return 0 if there was no error (e.g. the disk was mounted successfully), or will return some number if there was a problem (the disk couldn't be mounted, the password was wrong, etc.), so all we need to do is check this error code (which is why we're free to ignore the output of the diskutil command itself).
So now we get something like:
set cmdResult to do shell script "diskutil apfs unlockVolume FD6DF5D6-0EAA-3429-9D86-8E06AFCC075B -passphrase " & thepw & " >/dev/null 2>&1 ; echo $?"
whenever this is run, cmdResult will be either 0 or some other value, and we can test that. Here's one example:
-- start with nothing
set preamble to ""
-- assume failure
set cmdResult to 1
-- start the loop
repeat until cmdResult is 0
-- ask the user for the password
set thePW to text returned of (display dialog (preamble & "What's the password?") default answer "" with hidden answer)
-- if it's empty...
if thePW is "" then
-- set the preamble to tell the user they have to give us _something_
set preamble to "Password cannot be blank. Please enter a password." & return & return
else
-- if we get here the user entered a password, so let's pass that to diskutil and see what happens
set cmdResult to do shell script "diskutil apfs unlockVolume FD6DF5D6-0EAA-3429-9D86-8E06AFCC075B -passphrase " & thePW & " >/dev/null 2>&1; echo $?"
-- assume the password was incorrect, so give the user some feedback
-- this line won't matter if the password is correct since the script will exit
set preamble to "Incorrect password. Please try again:" & return & return
end if
end repeat
Note how I used the preamble to provide feedback to the user as to what's wrong.
Note that instead of the ' > /dev/null 2>&1; echo $?' method, you could wrap the do shell script in a try/end try block as noted earlier and use that to catch the diskutil error. Multiple ways of approaching this, but in general since we're using a shell command, I prefer to leverage the shell mechanism for checking error results.