DEV Community

Josh Stillman for Giant Machines

Posted on

Instantly Create Gmail Addresses for Testing with a Keyboard Shortcut on Mac OS

The Shortcut in Action

When developing new features, it can be useful to be able to create new, unique, working email addresses for testing. For example, say you want to repeatedly test a sign-up flow end-to-end, and you need a new, unique email address each time through the flow. Furthermore, you'd like to use a real, working email address to verify the behavior of a confirmation email that gets sent after signing up. How can we easily accomplish this?

If you have a Gmail or Google Workspace (formerly G Suite) email address, you're in luck! One of Gmail's coolest under-the-radar features is the ability to create task-specific email addresses by adding a plus sign followed by additional characters to your email address. So, if your email address is bob.smith@gmail.com, you can sign up with bob.smith+hello-world@gmail.com, and emails to bob.smith+hello-world@gmail.com will reach your inbox. This makes it easy to repeatedly create new, unique, working addresses for testing!

But if you do this a lot, it becomes mentally draining to keep track of which email addresses you've already used and come up with new addresses on the fly. You'll have to keep track of whether you've already used bob.smith+test15@gmail.com or should instead be on bob.smith+test16@gmail.com, for example. Instead, let's automate it!

With some built-in Mac OS tools, it's possible to automatically create new, unique Gmail addresses with a handy keyboard shortcut to speed up our smoke testing. First, we'll use the Mac OS Automator application to create a script to generate new email addresses using the current system time. Then we'll attach that script to a keyboard shortcut in our system keyboard preferences.

Automator Quick Action

Mac OS ships with the Automator program that allows you to create scripts using JavaScript or your shell, and these scripts can output text. In Automator, select New, then choose Quick Action.

Choose _Quick Action_

On the left hand pane, select Utilities, then Run JavaScript or Run Shell Script.

Choose _Utilities_ then _Run JavaScript_

On the top pane, select Workflow receives no input in any application. Also select the Output replaces selected text checkbox.

In the Run JavaScript dialog below, create a script that will interpolate the system time in milliseconds into your Gmail address after the + sign. Using ES6 template literals, you can have the function:

return `bob.smith+${new Date().getTime()}@gmail.com`;

This will return an email in the format bob.smith+1613507754883@gmail.com. Make sure not to check Show this action when the workflow runs, otherwise Automator will show a pop-up each time. Then, save and name your Automator action.

Write your JavaScript

You can also use Automator to execute shell scripts, which opens up all kinds of possibilities. The equivalent shell script here (in seconds) would be printf "bob.smith+%s@gmail.com" $(date +%s).

Or write a shell script

Keyboard Shortcut

Next, open System Preferences, choose Keyboard, then Shortcuts. In the left-hand pane, choose Services. You should see the Quick Action you created with Automator as an option in the right-hand pane. Add a memorable keyboard shortcut, then click the checkbox to activate. (I chose Control + Option + Command + C, but you might want to investigate setting up a Hyper Key for things like this instead.)

Set the Keyboard Shorcut under _Services_

Now, give your keyboard shortcut a try!

The Shortcut in Action

This is one simple example of how to use scripting, Mac OS Automator, and keyboard shortcut mappings to automate simple tasks and speed up your development experience. But these tools open up tons of other possibilities. Got any cool ideas of your own? Let us know in the comments!

Discussion (0)