I am having issues in Leopard where I cannot shutdown without forcing a shutdown. I cannot find the solution; I feel like I have tried everything. So, I’m looking at writing a script to “fix” this. Here is what I have tried, but it just hangs and I have to force quit the script:
on run
display dialog "Please enter your password:" default answer ""
set userpassword to text returned of result
if userpassword is "mypass1234" then
do shell script "sudo su; shutdown -h now" password userpassword with administrator privileges
else
display dialog "Sorry, wrong password." giving up after 3
end if
end run
property userpassword : {}
on run
display dialog "Please enter your password:" default answer ""
set userpassword to text returned of result
if userpassword is "mypass1234" then
do shell script "sudo shutdown -h now" password userpassword with administrator privileges
else
display dialog "Sorry, wrong password." giving up after 3
end if
end run
It defines the userpassword variable to be a property. Properties are saved across runs of the script.
Since the entered password is saved in this variable, it will cause the password to be saved into the script. But nothing in this script actually reads the value of the variable before it is overwritten, so it is useless. You can delete it.
If the default answer of the dialog used this variable instead of “”, then the password used in the previous run of the script would be pre-populated in the dialog. Thus, you would not have to actually type the password, just hit enter to accept the previously used value. But I am not sure this makes any sense in this password protection scenario.
Add with hidden answer to the parameters of display dialog. It will use bullets, though not asterisks.
I’m not sure why he putted it there. But if you change the script to the below, the password you’ll have entered automatically save. If you want to use this function, it’s best to save your script as an application, because when you recompile the script, the saved information will get lost.
Script:
property userpassword : missing value
on run
if userpassword is missing value then set userpassword to text returned of (display dialog "Please enter your password:" default answer "" with hidden answer)
if userpassword is "mypass1234" then
do shell script "sudo shutdown -h now" password userpassword with administrator privileges
else
display dialog "Sorry, wrong password." giving up after 3
set userpassword to missing value
end if
end run
This should do it:
display dialog "Password:" default answer "" with hidden answer