Password Suggestorator
I know it isn’t everyone’s cuppa tea, but Safari (on the Mac anyway) is pretty nice, and works well these days with Keychain. This allows folks, who might normally otherwise set all their passwords to “AuntMabel”, to set unique and strong passwords for the MANY websites we all sign up for these days. Now of course, a lot of people avoid this by just signing into everything using Facebook or Twitter, but frankly tying my entire digital life together into one easy to trawl location gives me the heeby-jeebies.
Safari got an update a couple of years ago so that when a website prompts you for a password, it will make a suggestion and offer to save it in Keychain. Nifty. But… what about if I am using Citymapper, where there is a website AND a native mobile app? iCloud stored Keychain items typically aren’t available to native apps (although this is changing, albeit really slowly), so whilst if you stick to (mobile) Safari you’ll never have to type that password in, you will most likely need to in an app. You can access Keychain stored passwords on iOS (look in Settings →Safari →Passwords) and copy them out of there, but maybe it would be nice to have another option when generating the password in the first place? Something other than the AB1-DE2-HI3-KL4 format — which is a pain in the backside to type on a phone, and hard to remember.
Having a trawl around the net, I found other folks had wanted something similar, to be able to generate password suggestions in the same way that the Keychain app does, but from the Terminal prompt. This got me thinking that this would then be easy to call from an AppleScript/Automator service, which can then be called either from the Safari Services menu, or via a keyboard shortcut.
First off, you will need to install Homebrew which you can get here, the package manager project for OS X. It’s pretty cool and allows you to do a bunch of stuff, but for now, we’ll just be using it for getting the password command tool. It will probably prompt you to install XCode command line tools, which is a bit of Apple dev software. Fear not, this lot only takes a few minutes to do.
Once you have that, you need to head over to bitbucket to pick up the Homebrew command line tool, sf-pwgen. Actually, you don’t really need to pick up the code there, but I wanted you to check that page out because a) you should be aware of where the code comes from, and b) it tells you how the command works with a bunch of different options. Anyone for Japanese word-phrase passwords? Hai!Skidesu.
To actually install sf-pwgen, fire up Terminal and run this line;
brew install sf-pwgen
It will prompt you along the way, so pay attention!
Once you have that you need to grab the script below and paste it into Script Editor on your Mac. Read those comments in the script — they are there for a reason!
— Script relies on installing Mac Homebrew, and sf-pwgen
— https://bitbucket.org/anders/sf-pwgen
— Get them first, else the script will NOT work!
— Also, check where Brew installs sf-pwgen*, and update the variable ‘brewPath’ below accordingly
— * do this by opening terminal and typing “brew list sf-pwgen”
— One suggestion: take this script and save it as an app. Then create an Automator SERVICE Workflow which takes no input and save it with a “friendly” name. Restrict to Safari or Chrome if you like. Then go into System Prefs — Keyboard — Shortcuts and create a keyboard shortcut to invoke the app (take care to use the exact text that appears in the service menu) via the service and voila! You can trigger a password anytime you need from the keyboard!
— WARNING: It should be fine, but I offer no warranties on the use of this script. Use at your own risk, entirely. Don’t use it for highly sensitive passwords, just to be safe!
tell application “Safari”
set brewPath to “/usr/local/somewhere/sf-pwgen/1.3/bin/”
— You can get sf-pwgen to return five passwords, but that would have required text mangling in Applescript lists, so it seemed easier and probably quicker to just run sf-pwgen 5 times
set myPasswords to {do shell script brewPath & “sf-pwgen -c1”, do shell script brewPath & “sf-pwgen -c1”, do shell script brewPath & “sf-pwgen -c1”, do shell script brewPath & “sf-pwgen -c1”, do shell script brewPath & “sf-pwgen -c1”}— You can skip this repeat block if you like. It simply forces the first character of the passwords suggested to be uppercase. SO many websites insist on at least one character always being upper case. Sigh…
set counter to 1
repeat with myPassword in myPasswords
set UpperFirstCharString to do shell script “echo “ & character 1 of myPassword & “ | tr [:lower:] [:upper:]”
set item counter of myPasswords to UpperFirstCharString & characters 2 through -1 of myPassword
set counter to counter + 1
— log “myPassword is “ & myPassword
end repeat
— Allow user to choose from a list of five suggestions (you could omit this and just copy one option with no review stage if you like)
set selectedP to {choose from list myPasswords} as text— Put password onto clipboard ready for user
set the clipboard to selectedP
end tell
Save this as an Application (File Format=Application) to your Applications folder. Use a name something like ‘Password Suggestorator’.
Next steps:
- Launch Automator
- Select ‘File →New’
- Choose option for a Service (cog icon)
- In the little search box in the left pane, type ‘launch’ and then drag the ‘Launch Application’ widget over to the actions pane
- Set the dropdown ‘Service receives selected’ to ‘no input’ and set ‘in’ dropdown to ‘Safari’ if you only want this available in the Safari service menu.
- Change the Launch Application dropdown to target the application you saved — ‘Password Suggestorator’
- Save the service — suggest you use the same name as the app — ‘Password Generator’
- You will now have a new option in the Services menu when you are in the Safari
- Now launch ‘System Preferences’
- Open the ‘Keyboard’ preference
- Click on ‘Shortcuts tab’
- Click on ‘App shortcuts’
- Click the + button to add a new shortcut
- Set Application dropdown to Safari
- In the ‘Menu Title’ box enter the new services menu names as you saved them, e.g. ‘Password Suggestorator’
- Click into the ‘Keyboard Shortcut’ box and press the keyboard shortcut combo you want to use to trigger the service.
Feel free to tweak the AppleScript as much as you like. For example, I don’t use the default settings for sf-pwgen, and I don’t bother with the whole 5 options to choose from in the dropdown. How about something a little Germanic?
tell application “Safari”
set brewPath to “/usr/local/somewhere/sf-pwgen/1.3/bin/”
set myPassword to {do shell script brewPath & “sf-pwgen -c1 -l20 -L de”} as text— Put password onto clipboard ready for user
set the clipboard to myPassword
end tell
Word of caution. I am NOT a coder! You use this entirely at your own risk! I am not responsible for the Homebrew package manager nor sf-pwgen. Maybe just use this for not very important passwords…