Random password generator and log

Pretty basic script but have found really useful.

The script requests some general information such as the user name you want to use as a login, how many characters your passowrd needs, the application you need the password for (eg Safari or Dreamweaver) and if it’s for a website, you can specify the site address.

It then generates a random alpha-numeric password and logs all the details in an HTML file for future reference, adding to it each time you create a new password. The site address in the HTML becomes a link so you can click it to go straight to the site. The HTML file can edited in any text / HTML editor (eg if you want to change a password).

Have included a CSS file for formatting but this is not essential and again can be edited to suit in any text / HTML editor.

NB Not been using Applescript for very long so realise there must be a number of ways of simplifing the script and would welcome any advise / comments. Cheers

(*
Script used to create a random password and log in a file for future reference.
User input includes:
User name to be used
max number of characters (must be even number)
The normal application the password would be assosiated with
the website this would be assosiated with
any additional information

The script then creates a completely random alpha-numeric password and places all information in the password file for future reference. 
*)


-- Lists from which the passwords are generated - can be amended e.g. remove all uppercase characters to ensure all passwords only contain lowercase characters and numbers

set alpha_list 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", "?", "!"}
set numeric_list to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
set alpha_result to {}
set numeric_result to {}
set application_log to {}
set website_log to {}
set result_list to {}

-- get user information

display dialog "User name" default answer ""
set user_name to text returned of result as string

display dialog "Max number of characters in password" & return & "N.B. MUST BE AN EVEN NUMBER" default answer 8
set the_number to text returned of result

display dialog "Normal application" default answer "Safari"
set the_application to text returned of result as string

display dialog "Website" & return & "insert the web address omitting HTTP://www. e.g." & return & "melgab.com" & return & "Script will still work but the resulting link in the HTML file will not" default answer "None"
set the_website to text returned of result as string

display dialog "other info" default answer "None"
set other_info to text returned of result as string

set the_total_number to the_number as integer
set half_number to (the_total_number / 2)

-- Get random characters from alpha list without repeats
set k to 1
set alpha_result to {}
repeat until (count of alpha_result) = half_number
	set i to some item of alpha_list as string
	if (count of alpha_result) ≥ 1 then
		if i is in alpha_result then
			set i to 0
		else
			set alpha_result to alpha_result & i
			set k to k + 1
		end if
	else
		set alpha_result to alpha_result & i
		set k to k + 1
	end if
end repeat

-- Get random numbers from numeric list without repeats
set k to 1
set numeric_result to {}
repeat until (count of numeric_result) = half_number
	set i to some item of numeric_list as string
	if (count of numeric_result) ≥ 1 then
		if i is in numeric_result then
			set i to 0
		else
			set numeric_result to numeric_result & i
			set k to k + 1
		end if
	else
		set numeric_result to numeric_result & i
		set k to k + 1
	end if
end repeat

-- combine the two reulting lists
set result_list to result_list & alpha_result & numeric_result
set table_length to length of result_list
set new_list to {}

-- create the password by randomly mixing the two lists without repeats
set k to 1
set html to ""
set password_list to {}
repeat until (count of new_list) = (count of result_list)
	set i to some item of result_list as string
	if (count of new_list) ≥ 1 then
		if i is in new_list then
			set i to 0
		else
			set new_list to new_list & i
			set password_list to password_list & item k of new_list
			set k to k + 1
		end if
	else
		set new_list to new_list & i
		set password_list to password_list & item k of new_list
		set k to k + 1
	end if
end repeat

-- sets the layout of the file

-- CHANGE DESTINATION_FOLDER TO SUIT
set destination_folder to path to desktop

-- CHANGE FILE NAME TO SUIT
set new_file to "password.htm"


-- Creates the resulting HTML file with a link to the appropriate website
set create_file to destination_folder & new_file as string
tell application "Finder"
	if not (exists file create_file) then
		set html to html & "<html><head><link href=\"main.css\" rel=\"stylesheet\" type=\"text/css\"></head><body>" & return & "<table align=\"center\"><tr><td><h3>Website</h3></td><td><h3>Application</h3></td><td><h3>User name</h3></td><td><h3>Password</h3></td><td><h3>Other Info</h3></td></tr>"
		
		set html to html & return & return & "<tr><td><p><a href=\"http://www." & the_website & "\" target=\"new\">" & the_website & "</a></p></td><td><p>" & the_application & "</p></td><td><p>" & user_name & "</p></td><td><p>" & password_list & "</p></td><td><p>" & other_info & "</p></td></tr>"
	else
		set html to html & return & return & "<tr><td><p><a href=\"http://www." & the_website & "\" target=\"new\">" & the_website & "</a></p></td><td><p>" & the_application & "</p></td><td><p>" & user_name & "</p></td><td><p>" & password_list & "</p></td><td><p>" & other_info & "</p></td></tr>"
	end if
end tell

set the_path to create_file
open for access file the_path with write permission
write html to file the_path starting at eof
close access file the_path
set the_alias to the_path as alias

-- REMOVE THIS SECTION IF YOU DO NOT WANT TO OPEN FILE
tell application "Safari"
	activate
	open the_alias
end tell

Save the following as a file “main.css” in the same directory as the resulting HTML file (Default is Desktop)

For a password generating script, see here:

Make New Password

Jon