Вики IT-KB

Пошаговые руководства, шпаргалки, полезные ссылки...

Инструменты пользователя

Инструменты сайта


apple-mac-os:macos-ventura:applescript-password-generation-script-for-macos-ventura

Скрипт AppleScript для генерации паролей в ОС Apple macOS

Достаточно длительное время на сочетании клавиш «Control + Command + P» находился простенький скрипт для генерирования паролей. Примеров подобных скриптов встречается множество. Однако все эти скрипты имеют один серьёзный недостаток — нет гарантии, что полученный пароль будет содержать в себе символы из указанных списков.

--Возможные символы в пароле
--Исключены I O i l j o q 1 0
set List1 to {"A", "B", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
set List2 to {"a", "b", "d", "e", "f", "g", "h", "k", "m", "n", "p", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
set List3 to {"2", "3", "4", "5", "6", "7", "8", "9"}
set List4 to {"+", "-", "_", "="}
set List5 to {List1, List2, List3, List4}
 
--Длина пароля
set PasswordLength to "7"
 
--Генерируем
set genPassword to ""
repeat (PasswordLength as integer) times
	set Symbol to some item of List5
	set genPassword to (genPassword & some item of Symbol) as text
end repeat
 
--Вставка пароля
set the clipboard to genPassword
tell application "System Events" to key code "9" using command down

То есть результатом выполнения скрипта может быть пароль вида «6-=_S43», в котором не окажется строчных букв.

На тематическом форуме попался более интересный вариант, который с небольшой доработкой получилось адаптировать для использования сочетанием клавиш.

--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)
	--Вставка пароля
	set the clipboard to thePassword
	tell application "System Events" to key code "9" using command down
end main
 
on getPassword(lowercaseLetterCount, uppercaseLetterCount, numberCount, specialCharacterCount)
	--Исключены 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()

Проверено на следующих конфигурациях:

Версия ОС
Apple macOS Ventura (13.0)
Apple macOS Sonoma (14.0)

Автор первичной редакции:
Виталий Якоб
Время публикации: 05.06.2023 16:15

Обсуждение

Ваш комментарий:
 
apple-mac-os/macos-ventura/applescript-password-generation-script-for-macos-ventura.txt · Последнее изменение: 08.11.2023 20:38 — Виталий Якоб

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki