Easy Dummy Text using Python Faker and Keyboard Maestro

Yet again I’ve found a great use for Keyboard Maestro. I’m a web developer and often have to create “filler” or “dummy” text during development. In the past I’ve used Alfred workflows, copy-and-paste, browser extensions, and more. I’ve recently come up with a much cleaner (and more powerful) solution to achieving field-by-field or one-off dummy text.

What do I mean by field-by-field and one-off? Well, I use tools sometimes to automate filling an entire form repeatedly (Fake, Selenium, iMacros, etc.). I’ve even written a Selenium extension to inject random text into the fields Selenium is automatically filling. Unfortunately, this all takes time. Sometimes I just need to fill some fields in a form a few times and move on. It’s not worth automating at that point. Enter Keyboard Maestro.

My goal was to just have a tool palette that I can pull up when I need various types of dummy text. Here’s the finished result I’m using these days:

img20150602111956

All of these macros are nearly identical. They do the following:

  1. Check if the user is holding the CMD key when they click the macro
    1. If they are, replace the field value with the generated text.
  2. Execute a fake.xyz() call in Python to generate the dummy text
  3. Store the result in a variable
  4. Insert the text from that variable

Here’s what this looks like in the User Email macro:

img20150602112321

The README macro simply explains that these macros all require Python and the “Faker” package from https://fake-factory.readthedocs.org/en/v0.4/

The real beauty of the solution is that Faker is robust. You can use it to generate everything from social security numbers to domain names, first names to country codes. It’s all nicely-documented at https://fake-factory.readthedocs.org/en/v0.4/providers.html

I’ve provided an export of the macros as I use them currently. It should provide a good starting point for you. Download Macros

After you’ve imported them, just open the palette and leave it open while you use it. When you want to fill a field with dummy text, simply put your cursor into the field and click the macro you want. If you want to clear what’s already there, hold the CMD key when you click the macro.

It’s very easy to add more more macros, so if you find yourself needing something (e.g., fake credit card numbers) just copy one of the existing macros and change the macro name and the function call according to the documentation.

Tagged with: , , , , ,
Posted in Development, Tech Tips
2 comments on “Easy Dummy Text using Python Faker and Keyboard Maestro
  1. Patrick Hund says:

    Hello Adam,

    thanks for this interesting article!

    Unfortunately, in Keyboard Maestro 7, executing a python script with Keyboard Maestro the way you describe here doesn’t seem to work anymore.

    I tried your code in a KM macro and get a message from KM: “ImportError: No module named faker”. I did install the Faker module and executing the script on the console works just fine.

    There is a thread on the KM discussion forum that deals with this problem, but they didn’t find a solution: https://forum.keyboardmaestro.com/t/cannot-import-certain-python-modules/2877

    Can you help make this work?

    Cheers,
    Patrick

  2. adam says:

    Hello Patrick,

    I’m using all of my Faker macros with 100% success in Keyboard Maestro 7, interestingly enough.

    Here’s an example of a “Title Text Uppercase” expansion I’m using:

    python -c “from faker import Factory; fake = Factory.create(); print fake.sentence(5).title()[:-1]”

    It probably has to do with how you’ve installed Faker, or which version of Python KM is using by default. I installed Faker (a long time ago) using pip as far as I remember.

    When I run “which python” in a macro I get “/usr/bin/python” as a result.
    When I run “python –version” in a macro I get “Python 2.7.10” as a result.

    Maybe could install Faker for a specific version of Python and use that specific version instead of just “python -c”.

    Anyhow, I can confirm that I am not having any issues using those faker-based Python macros, so it should be possible for you.

    Thanks for posting!
    Let me know if you’re still stuck.
    -Adam

Leave a Reply

Your email address will not be published. Required fields are marked *

*