Random Password Generator

Interesting. Thanks for looking into this, @peavine. :slight_smile:

Hey Folks,

Just for variety – a couple of command line methods:

Available by default on macOS:

Requires a 3rd party Unix command line utility:

-Chris

1 Like

FWIW, I tested TheKrell’s script and it contains numerous path references to an icon file on his/her computer, which causes the script to error. After changing that, the script seems to work well.

1 Like

Same on Mojave.

Fredrik71. The issue is the considering case statement. My language is English, and the following doesn’t seem to work as I would expect:

-- one of the following should evaluate to false
considering case
"a" < "Z" --> true
"A" < "z" --> true
end considering

The following is from the AppleScript Language Guide:

To determine the ordering of two text objects, AppleScript uses the collation order set in the Language pane of International preferences. A text object is greater than (comes after) another text object based on the lexicographic ordering of the user’s language preference… Text comparisons can be affected by considering and ignoring statements, as described in the Text section of equal, is not equal to.

Hi Nigel_Garvey,
Thank you for the fix and the heads up. I will remember in the future. I have not been here a while.

Hi peavine,
Sorry about not changing the icon file references. I wrote this a long time ago and forgot how it was welded into my directory.

I wrote a password script for my own use and thought I would post it here FWIW. The script differs a bit in that it allows the user to specify the number of uppercase and lowercase letters, numbers, and special characters.

-- revised 2023.02.18

on main()
	set passwordCount to "4 4 2 2" -- lowercase letters, uppercase letters, numbers, special characters
	repeat
		repeat
			set userInput to text returned of (display dialog "Enter the desired number of lowercase letters, uppercase letters, numbers, and special characters" default answer passwordCount cancel button 1 default button 2 with title "Password Generator")
			set text item delimiters to {" "}
			try
				set lowercaseLetterCount to text item 1 of userInput as integer
				set uppercaseLetterCount to text item 2 of userInput as integer
				set numberCount to text item 3 of userInput as integer
				set specialCharacterCount to text item 4 of userInput as integer
				exit repeat
			on error
				display dialog "The entered data must consist of 4 space-separated numbers, which represent the desired number of lowercase letters, uppercase letters, numbers, and special characters" buttons {"Cancel", "Try Again"} cancel button 1 default button 2 with title "Password Generator"
			end try
		end repeat
		set text item delimiters to {""}
		set thePassword to getPassword(lowercaseLetterCount, uppercaseLetterCount, numberCount, specialCharacterCount)
		set theButton to button returned of (display dialog "The password is " & thePassword buttons {"Cancel", "Try Again", "Copy to Clipboard"} cancel button 1 default button 3 with title "Password Generator")
		if theButton is "Copy to Clipboard" then
			set the clipboard to thePassword
			exit repeat
		end if
	end repeat
end main

on getPassword(lowercaseLetterCount, uppercaseLetterCount, numberCount, specialCharacterCount)
	set lowercaseLetters to characters of "abcdefghijklmnopqrstuvwxyz"
	set uppercaseLetters to characters of "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	set theNumbers to characters of "0123456789"
	set specialCharacters to characters of "@#$%&*+" -- edit as desired
	set lowercaseRandomLetters to getRandomCharacters(lowercaseLetters, lowercaseLetterCount)
	set uppercaseRandomLetters to getRandomCharacters(uppercaseLetters, uppercaseLetterCount)
	set randomNumbers to getRandomCharacters(theNumbers, numberCount)
	set randomSpecialCharacters to getRandomCharacters(specialCharacters, specialCharacterCount)
	set randomCharacters to (lowercaseRandomLetters & uppercaseRandomLetters & randomNumbers & randomSpecialCharacters)
	return getShuffledCharacters(randomCharacters) as text
end getPassword

on getRandomCharacters(theList, theCount)
	set randomList to {}
	repeat theCount times
		set the end of randomList to some item of theList
	end repeat
	return randomList
end getRandomCharacters

on getShuffledCharacters(theList)
	set {theIndices, shuffledList} to {{}, {}}
	repeat with i from 1 to (count theList)
		set end of theIndices to i
	end repeat
	repeat (count theList) times
		set anInteger to some integer of theIndices
		set end of shuffledList to item anInteger of theList
		set item anInteger of theIndices to missing value
	end repeat
	return shuffledList
end getShuffledCharacters

main()
2 Likes

I adapted the script to my tasks, and now I like the process of generating a password better. Thank you. :slight_smile:

--Orig https://www.macscripter.net/t/random-password-generator/74036/32

on main()
	set uppercaseLetterCount to 2
	set lowercaseLetterCount to 2
	set numberCount to 2
	set specialCharacterCount to 1
	set thePassword to ""
	set thePassword to getPassword(lowercaseLetterCount, uppercaseLetterCount, numberCount, specialCharacterCount)
	--Paste password
	set the clipboard to thePassword
	tell application "System Events" to key code "9" using command down
end main

on getPassword(lowercaseLetterCount, uppercaseLetterCount, numberCount, specialCharacterCount)
	--exclude I O i l j o q 1 0
	set uppercaseLetters to characters of "ABCDEFGHJKLMNPQRSTUVWXYZ"
	set lowercaseLetters to characters of "abcdefghkmnprstuvwxyz"
	set theNumbers to characters of "23456789"
	set specialCharacters to characters of "+-_="
	set lowercaseRandomLetters to getRandomCharacters(lowercaseLetters, lowercaseLetterCount)
	set uppercaseRandomLetters to getRandomCharacters(uppercaseLetters, uppercaseLetterCount)
	set randomNumbers to getRandomCharacters(theNumbers, numberCount)
	set randomSpecialCharacters to getRandomCharacters(specialCharacters, specialCharacterCount)
	set randomCharacters to (lowercaseRandomLetters & uppercaseRandomLetters & randomNumbers & randomSpecialCharacters)
	return getShuffledCharacters(randomCharacters) as text
end getPassword

on getRandomCharacters(theList, theCount)
	set randomList to {}
	repeat theCount times
		set the end of randomList to some item of theList
	end repeat
	return randomList
end getRandomCharacters

on getShuffledCharacters(theList)
	set {theIndices, shuffledList} to {{}, {}}
	repeat with i from 1 to (count theList)
		set end of theIndices to i
	end repeat
	repeat (count theList) times
		set anInteger to some integer of theIndices
		set end of shuffledList to item anInteger of theList
		set item anInteger of theIndices to missing value
	end repeat
	return shuffledList
end getShuffledCharacters

main()

1 Like