Textexpander: Javascript to generate random password


#1

I’ve been using Textexpander at work to save repetitive typing as I respond to IT support tickets and up until now they’ve just been basic plain text. Now however our password policy has changed and we need to use a random password when creating a new user account. I know Textexpander can execute Javascript in snippets and I was wondering if anyone had a script they could share that would be suitable?

I’ve never learnt Javascript so it’s not something I can really do myself.

n.b. Security isn’t a big concern as it’s only a temporary password that will require changing the first time the new staff member signs in.


#2

I wrote one of these in Java, let me see what I can do for you :slight_smile:


#3

Ok here it is tested in TextExpander

const charsToUse = "qwertyupasdfghjkzxcvbnmQWERTYUPASDFGHJKZXCVBNM23456789!@#$%&*";
var stringToReturn = "";

for(let i = 0; i < 8; i++ ){
    stringToReturn += charsToUse[Math.floor(Math.random() * charsToUse.length)];
}

TextExpander.appendOutput(stringToReturn);

Permit me to run through how it works. First we have a list of potential characters to use in the password, i have omitted potentially confused characters such as o, O and 0

Then we create the string we are going to use to store the password.

Then we have a loop to build the password by selecting a random character from the potential characters list. The I < 8 in the loop controls the number of times the loop runs.

Then lastly we send the output to TextExpander to output.

Now you should not go running scripts from rando’s on the internet without first taking the time to understand how they work. I just explained this one for you, but a book on JavaScript basics should only take a few hours to give you the skill set to do this kinda stuff on your own. That script took a whole of 10 minuets to write.

I might later host it on My GitHub along with a version that uses full words, but I don’t want to commit to that.


#4

Here is the same function adapted for use in Keyboard Maestro instead of TextExpander:

var stringToReturn = "";

for(let i = 0; i < 8; i++ ){
    stringToReturn += charsToUse[Math.floor(Math.random() * charsToUse.length)];
}

stringToReturn;

Put this in the “Execute JavaScript for Automation” action. I choose to “type results” to get the password.


#5

Thanks Ben, reading your explanation and looking at the code itself it makes sense.