Further to my previous foray into the world of Applescript, I’ve modified my server management script to now prompt me for a sudo password. Previously I would have to tab between each Terminal window and enter my sudo password, but now I enter it once and a dynamic command is generated that looks like this:

echo <password> | sudo -S clear && sudo aptitude update && sudo aptitude dist-upgrade && sudo aptitude clean'"

I don’t like that my sudo password is displayed on the screen. I could get around this by manually editing /etc/sudoers to allow for password-less aptitude. Alternatively, perhaps I could encrypt my password inside the Applescript and send it, pre-encrypted, to sudo. They’re options I guess.

You’ll notice that the first thing I do is clear the screen, but when there’s a second or so lag it means my password is bare for all to see. I’ll consider that when I run the script.

Below is an Applescript snippet which shows you how to open a dialog box and take some simple text input:

set my_password to display dialog "Please enter your password:" ¬
	with title "Password" ¬
	with icon caution ¬
	default answer "" ¬
	buttons {"Cancel", "OK"} default button 2 ¬
	giving up after 295 ¬
	with hidden answer
if length of (text returned of my_password) is not 0 then
	display dialog "Running the application!" buttons ["OK"] default button 1
else
	display dialog "You didn't enter a sudo password!" buttons ["OK"] default button 1
end if

Having spent a bit of time with Ruby lately, I don’t like the syntax of Applescript very much, though it gets the job done.

Posted in Code, Tips and Tricks at December 9th, 2008. Trackback URI: trackback
Tags: , , , , , ,

2 Responses to “Prompting for a password with Applescript”

  1. December 9th, 2008 at 9:41 pm #Martin Michel

    If you do not like the syntax of AppleScript, then you might be interested in appscript, which allows to write AppleScript code in Python, Ruby or even Obj-C:

    http://appscript.sourceforge.net/rb-appscript/index.html

    I use it for many of my private automation projects and it works like a charm.

    Best regards from Germany and keep on scripting the Mac :-)

    Martin

  2. December 9th, 2008 at 10:17 pm #mlambie

    Thanks for the suggestion Martin. I knew that with Leopard’s release Ruby was made a legitimate choice for scripting on the Mac but I haven’t investigated it much outside Rails.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>