password generator

applescript can be immensely frustrating at times — trying to get the syntax and structure to work just right is often a torturous process for us novices. but sometimes an applescript solution turns out to be astonishingly easy …

this script is the result of simple paranoia : there are a lot of online services offering to generate randomised passwords for you. now, it seems rational to expect at least some of these services may be malignant — they generate a password for you, then follow your digital trail to where that password is used. again, this may simply be paranoia and have absolutely no bearing at all on reality — or maybe not.

this is a simple script to generate passwords, on your own machine, of whatever length you like *

* yes, whatever length you like, but you may find the script bogs down a bit when generating passwords of more than 50,000 characters — apologies.

a good password is a random collection of upper case letters, lower case letters and digits (even better passwords also include punctuation, but many, many sites do not allow punctuated passwords) — so the first thing we do is create a bunch of lists (see below for an explanation of why we don’t use just one list) :

set mgList1 to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
set mgList2 to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
set mgList3 to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
set mgList4 to {mgList1, mgList2, mgList3}

that last list is a list of lists — things will become clearer in a moment.

next we ask the user to specify the length of the password. this uses the handy display dialog command which is standard issue with applescript :

set mgAnswer to display dialog "How long should this password be?" with title "password generator" default answer "8"
set x to text returned of mgAnswer

… and will generate a dialog something like this :
screen grab of password generator dialog
usually the result of a dialog is only button returned — but because we used the default answer property, we can also retrieve the text returned. in this case, the text returned is captured by the variable ‘x’.

the next part is the creation of the password :

set mgPassword to ""
repeat x times
  set mgThisList to some item of mgList4
  set mgPassword to mgPassword & some item of mgThisList
end repeat

first we create an empty variable (mgPassword) then run that variable through a repeat loop ‘x’ number of times — each time adding another randomly chosen letter or digit. this is achieved through the “some item of…” trick. first we randomly choose one of the three lists, then we randomly choose one of the items from that list.

finally, we present the password back to the user :

display dialog "Here you go …" with title "password generator" default answer mgPassword buttons "bonza" default button 1

screen grab of dialog showing generated password

wrap all that in an on run statement and you’ll have a neat little script that looks like this :
screen grab of password generator complete script
save that out as an application somewhere then just double-click to run.

or, if you’re really lazy, you can download the completed app here : password generator

so, why not use just one list?
you could combine all those lists at the start into one like this :

set mgList1 to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", 
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", 
"Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", 
"2", "3", "4", "5", "6", "7", "8", "9", "0"}

… but your passwords will be less likely to contain digits. with three lists you have a 1 in 3 chance that a digit will be chosen on each pass through the repeat loop. with only one list that chance is reduced to 1 in 6.2.

yeah, sure, but why not just do it like this? …

set mgList1 to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", 
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", 
"Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", 
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", 
"2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", "5", 
"6", "7", "8", "9", "0"}

because i didn’t bloody think of that when i first wrote the script — now shut up!
… but also because, with the three list version, you have the opportunity to extend the script to ensure that, for example, you don’t get three capitals in a row (so there).

now for your homework : how would you alter the script so that it generates five passwords at a time :
screen grab of dialog with five generated passwords

hoping you all have yourselves a grunting great christmas.

if you find this script handy, feel free to spread a little christmas cheer by hitting the donate button — thanks

macgrunt icon

Advertisements