Instantly Create Gmail Addresses for Testing with a Keyboard Shortcut on Mac OS
Tags: productivity, testing, javascript, shell
Published: 2021-03-08 15:00:34

![The Shortcut in Action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5lcy32ttramows9i1fl.gif)

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](https://workspace.google.com/) (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](https://support.google.com/a/users/answer/9308648?hl=en) 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](https://support.apple.com/guide/automator/welcome/mac) 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_](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q60ngoip4zk51s8zm0ox.png)

On the left hand pane, select _Utilities_, then _Run JavaScript_ or _Run Shell Script_.

![Choose _Utilities_ then _Run JavaScript_](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gwbid0w3o6eytk8v102v.png)

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u7dptdl3dh9ovev2pjae.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okgi5sjan2umg42tc9ew.png)

# 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](https://syntax.fm/show/315/hasty-treat-hyper-productivity-with-keyboard-shortcuts-window-management) for things like this instead.)

![Set the Keyboard Shorcut under _Services_](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mggutoirr2wh1961t92h.png)

Now, give your keyboard shortcut a try!

![The Shortcut in Action](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5lcy32ttramows9i1fl.gif)

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!