making password prompts

ok is there anyway that when a prompt comes up when you type it only shows the "*"s. Anyway in applescript to do that?

Here you go…

-- enter password, hides answer
display dialog "ENTER PASSWORD:" default answer "" buttons {"OK"} default button 1 with hidden answer
copy the result as list to {their_answer, button_pressed}

Note the “hidden answer” only works on OS X v1.04 or later (AppleScript v1.10).

Ok so this is my simple password protection script for folders. The problem is the part where if the user hits cancel the folder is supposed to close. The folder does not close when the user hits cancel though…

on opening folder This_Folder
	repeat
		tell application "Finder"
			set dialogresult to display dialog "Restricted Folder. Please enter the password to access folder:" buttons {"Ok", "Cancel"} default button 1 default answer "" with hidden answer
			copy the result as list to {PWText, button_choice}
			set button_choice to the button returned of dialogresult
			if button_choice is equal to "Ok" then
				set PWText to the text returned of dialogresult
				if not PWText = "password" then
					display dialog "Access Denied" buttons {"Canel"} default button 1
					
				else
					display dialog "Access Granted" buttons {"Ok"} default button 1
					exit repeat
				end if
			else if button_choice is equal to "Cancel" then
				tell application "Finder"
					close folder This_Folder
				end tell
			end if
		end tell
	end repeat
end opening folder

I believe the script is stopping as soon as Cancel is returned.

Try this:

display dialog "Cancel stops script... no beep is heard!" buttons {"Ok", "Cancel"} default button 2

beep

You might want to try naming the button something else, like “Stop”.

With most dialogs (except choose from list and [sometimes] display alert) choosing a button labeled “Cancel” (or otherwise signified as having such a behavior) will generate error number -128 (In English, “User canceled”); This stops the script unless you use a try block to do your own error handling. This gives you the benefit of not having to error handle everything the user could cancel out of.

You could either use a different word, or use small try block:

on opening folder This_Folder
	repeat
		tell application "Finder"
			try
				display dialog "Restricted Folder. Please enter the password to access folder:" default answer "" with hidden answer
				set PWText to text returned of result
			on error
				close folder This_Folder
				exit repeat
			end try
			
			if not PWText = "password" then
				display dialog "Access Denied" buttons {"Cancel"} default button 1
			else
				display dialog "Access Granted" buttons {"Ok"} default button 1
				exit repeat
			end if
		end tell
	end repeat
end opening folder

A couple of thoughts.

display dialog "Restricted Folder. Please enter the password to access folder:" default answer "" with hidden answer
set {PWText, theButton} to {text returned, button returned} of result
--> sets those variables and returns a list

display dialog "Restricted Folder. Please enter the password to access folder:" default answer "" with hidden answer
set {text returned:PWText, button returned:theButton} to result
--> sets those variables and still returns a record

Alternatively, if you don’t need to keep the button for very long.

display dialog "Restricted Folder. Please enter the password to access folder:" default answer "" with hidden answer
set {text returned:PWText} to result

if button returned of result is "OK" then
	-- OK
else
	-- whatever
end if

. . . Or you could actually protect the folder and:

  • Use a disk image with a password (high encryption).
  • Not leave your account logged in when you’re not there.

Otherwise, you may as well not bother.