Suppress Applescript Warning-Error

I have the following script that works fine except under 1 condition, that is where the user does not enter a password and clicks "OK" and I get the warning/error below the script.


Any way of getting the script to suppress the warning/error and have the "Password Incorrect…" come up as it would normally do with the wrong password since no password is the wrong password?


If the warning comes up, I need to force quit the script !!

I tried removing: default answer "" withhidden answer but didn't help

This is for an encrypted thumb drive if that matters.


Script:


set thepw to text returned of (display dialog "What's the password?" default answer "" withhidden answer)

 

do shell script "diskutil apfs unlockVolume FD6DF5D6-0EAA-3429-9D86-8E06AFCC075B -passphrase " & thepw



Warning/Error:




OS 15.5


Thanks, as always


iMac (2017 – 2020)

Posted on Jun 16, 2025 3:40 PM

Reply
Question marked as Top-ranking reply

Posted on Jun 17, 2025 11:20 AM

> 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.

8 replies
Question marked as Top-ranking reply

Jun 17, 2025 11:20 AM in response to mrokloricred37

> 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.

Jun 16, 2025 3:47 PM in response to mrokloricred37

> Any way of getting the script to suppress the warning/error


This is pretty easy. there are a couple of ways of going about it.


The first is to check the result of the display dialog command:


set thepw to text returned of (display dialog "What's the password?" default answer "" with hidden answer)
if thepw is not "" then
    do shell script "diskutil..."
end if


This will prevent the diskutil command from running without a password.


If you want to loop until the user enters a valid password, then try something like:


set thepw to ""
repeat until thepw is not ""
    set thepw to text returned of (display dialog "What's the password?" default answer "" with hidden answer)
end repeat
do shell script "diskutil ...."


in this way the user will repeatedly be prompted for the password and the script will not continue until they enter something, or click Cancel

Jun 16, 2025 3:52 PM in response to mrokloricred37

If you’re just trying to make sure they don’t continue without entering anything, you can try the following.


set thepw to “”

repeat while thepw is equal to “”

set thepw to text returned of (display dialog "What's the password?" default answer "" withhidden answer)

end repeat


Alternatively:


try

set thepw to text returned of (display dialog "What's the password?" default answer "" withhidden answer)

 

do shell script "diskutil apfs unlockVolume FD6DF5D6-0EAA-3429-9D86-8E06AFCC075B -passphrase " & thepw

on error

display dialog “Incorrect password, try again”

end try

Jun 16, 2025 6:02 PM in response to stevejobsfan0123

To be clearer, as it stands now an incorrect password brings up the warning below.

OK ends it, but edit allows editing of script.


I "just" want the user to enter the correct password and it would proceed. If not it should bring up "What's the password" again or bring up something like "wrong password" and user would get another chance to enter the correct password or cancel out of it.


Not be allowed to edit script.


Thanks again

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Suppress Applescript Warning-Error

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.