May 22, 2018

Random Keyword

Having a random word comes in handy when you need a word but want to be creative and something more than just 'abc’ or ‘xyz’.

Random Keyword in Keyboard Maestro

Here’s the random keyword macro that I have setup in Keyboard Maestro.

Click on image for the full view of the macro.

$words_file = fopen("/usr/share/dict/words","r");
$words_length = filesize("/usr/share/dict/words");

function random_word() {
  global $words_file, $words_length;
  $rand_offset = rand(0,$words_length - $words_length % 1024);
  fseek($words_file, $rand_offset - $rand_offset % 1024, SEEK_SET);
  $words = explode("n", fread($words_file, 1024));
  $rand_word = utf8_decode($words[rand(1, count($words) - 1)]);
  $description = str_replace("'s", "", trim($rand_word));
  return trim(ucfirst($description));

echo random_word();

There are many ways to generate a random keyword from the Apple System dictionary file. Using the above PHP method has worked for me. In the above example, I remove any 's in any keywords because they were breaking some testing that I was doing. In addition, the keyword isn't returning any breaking space and the first letter will always be upper case.

A couple of Problems with a Random Keyword

The only problem with this is that the keyword is always random. I have no idea the word is that I used. Well, what if I wanted to remember the last random keyword that I used? (Filename or something.)

That's why Keyboard Maestro comes in handy! In the above example, I have the random keyword set to a variable before it was displayed.

Getting the Last Random Keyword

Here’s a Keyboard Maestro's Macro that I have set up to get the last used random keyword:

Repeat Word Y Y

The Other "Problem"

Every now and again the random keyword generator will echo an inappropriate keyword. The best way around this is to use a different dictionary file.

Easy Way to get a Random Keyword

Whenever I need a random keyword, I type in xx. When I want to recall the last random word generated, I type in yy.These are quick and easy strings that I don't have to worry about accidentally typing.


Add your Comments

Feel free to leave a comment about this post.

- Feel Free to add HTML to your comment!