Email the URL and title of the current web page in one keystroke with Keyboard Maestro

I'm a big fan of efficiency, and this is one of many timesaving macros you can create with Keyboard Maestro. If you're already familiar with Keyboard Maestro variables and tokens, you can skip the tutorial and download email URL and title of current web page.kmmacros from my Keyboard-Maestro-Macros repo on GitHub, then import it via File -> Import Macros....

Once imported, it will be placed in a "Browsers" Group that defines the applications in which the keyboard shortcut (aka Hot Key Trigger) for the macro is enabled. I added Safari, Chrome and Firefox to the Group. Before you run the macro, you should at least edit the email address in the Insert Text action. You might also want to change the Hot Key Trigger and/or the email client launched by the macro. Once you've tested the macro on your machine, you should add an action at the end that actually sends the email.

For those of you who are new to Keyboard Maestro, let me walk you through the process step by step.

1) Launch Keyboard Maestro and create a new macro via File -> New Macro or the + button at the bottom of the center pane:

create a new macro in Keyboard Maestro

2) Rename the macro from "Untitled Macro" to something more meaningful, such as "Email URL and title of current web page".

3) Click on New Trigger, select Hot Key Trigger, then press the keyboard keys that correspond to your desired keyboard shortcut for this macro, such as control-option-command-e. You could also select Typed String Triggerinstead of Hot Key Trigger if you wanted to trigger the macro by typing "send", for example.

Now that we've set up the basics, we need to think through what we want to automate. Our goal is to send an email with the title of the current web page as the Subject, and the URL in the Body. First, we need to capture the web page's URL and title. The easiest way to grab the current URL is to use the keyboard shortcut ⌘L, which highlights the current URL in the browser's URL field (I've verified this works in Safari, Chrome, and Firefox). From there, all we have to do is copy it. Let's set up those two actions in Keyboard Maestro.

4) Click on "New Action".

5) In the search field at the top of the Actions pane on the left, type keys, then double-click on Type a Keystroke.

6) Once the Simulate Keystroke action appears in the right pane, click inside the Simulate Keystroke field, then press command and L on your keyboard. You should end up with something like this:

simulate keystroke action in Keyboard Maestro

After selecting the URL, we need to copy it, so we need another Simulate Keystroke action.

7) You can create this new action by double-clicking on Type a Keystroke or copying and pasting the existing action (click on it, then use ⌘C and ⌘V). This time, we want to simulate the ⌘C keystroke.

Once we've copied the URL, we want to store it in a variable so we can paste it in the email later on.

8) With the Actions pane still visible, search for variable, then double-click on Set Variable to Clipboard. (If you closed the Actions pane, just click on New Action to create a new action.)

9) Replace the highlighted word Variable in the Set variable field with a more meaningful description, such as url. Leave the to: field set to %CurrentClipboard% as that is exactly what we want. The current clipboard contains the last thing we copied, which was the URL.

Next, we want to capture the page title. We'll need to use variables and tokens for this one as well.

10) Create another Set Variable to Clipboard action (as in Step 8).

11) Replace the highlighted word Variable in the Set variable field with a more meaningful description, such as page_title.

12) Delete %CurrentClipboard% from the to: field, then click on the Insert Token dropdown, and select Front Window Name. Or, just type %WindowName%1% in the to: field.

Here's what you should have so far:

Set variable to front window name in keyboard maestro

Next, we need to launch our preferred email client. In this tutorial, we'll use Apple's Mail app.

13) Search for application in the Actions pane, then double-click on Switch to Specific Application. Click on the Activate: dropdown and select Mail.

Once Mail is the frontmost app, we want to open a new message window. We can either use the menu item or its keyboard shortcut. However, since the shortcut for "New Message" is ⌘N — a shortcut used by most apps — it's better to use the menu item's text, just in case Mail app is not in focus when the macro needs it to be.

14) For the menu item, search for menu in the Actions pane, then double-click on Select a Menu Item. You can either browse to the New Message menu item via the Menu dropdown, or just type File in the Menu Title field, and New Message in the Menu Item field. For the keyboard shortcut, repeat what we did in Steps 5 and 6 (or Step 7), and use ⌘N as the keystroke.

Once the new message window is up, and since the To field is in focus by default, we want to enter the email address of the lucky recipient. I created this macro because I often share interesting finds with my wife, so I've hardcoded her email in the macro. If you regularly send links to more than one person, you can have the macro prompt you for the email address. Let's start with the easy version first.

15) Search for insert in the Actions pane, then double-click on Insert Text. Inside the text field, type in the email address.

If you're going for the prompt, follow these steps:

  1. Search for prompt in the Actions pane, then double-click on Prompt for User Input.

  2. In the Title field, type in Enter the email address, for example. You can leave the Prompt text as is, or change it to Please enter the email address. Click on the + under Variables and Default Values, and replace the highlighted Variable text with email or whatever you want to call this variable.

  3. Add an Insert Text action, and select the email variable you created in the previous step by clicking on the Insert Token dropdown and navigating to Variable -> email. Or, just type %Variable%email% in the text field.

Here's what the prompt action should look like when you're done:

prompting for email address in Keyboard Maestro

Next, we want to paste the page title in the Subject field. To do that, we'll have to tab to the Subject field. The number of times we'll have to tab until we reach the Subject field will depend on which fields are enabled between the To and Subject fields. If you only have the Cc field in between, you will need 2 tabs. If you also have the Bcc field, you'll need 3 tabs.

16) Once again, we'll use the Type a Keystroke action and set it to Tab. Duplicate this action as many times as necessary to reach the Subject field.

17) Add an Insert Text action, and select the page_title variable you created in Step 11 by clicking on the Insert Token dropdown and navigating to Variable -> page_title. Or, just type %Variable%page_title% in the text field.

18) Insert another Tab Keystroke action (as in Step 16) so the macro can leave the Subject field and place the cursor in the Body of the message.

19) Add an Insert Text action, and select the url variable you created in Step 9 by clicking on the Insert Token dropdown and navigating to Variable -> url. Or, just type %Variable%url% in the text field.

Another way to paste the URL, without having to create a variable first, is to use the Past Clipboard token. So, you would skip Steps 8 and 9, and instead of choosing the url variable in Step 19, you would select the Past Clipboard token. However, you would have to change the value that gets pasted in by default, %PastClipboard%1%, to %PastClipboard%2%. That's because each Insert Text action uses the Clipboard.

By looking through our macro, we can see that we've used the Clipboard 3 times before Step 19. The last time was in Step 17, which makes that the Current Clipboard. The time before that, which would be the first Past Clipboard, was in Step 15. The third time was when we copied the URL, which is the second Past Clipboard.

Sometimes, macros can run so fast that the UI can't keep up. If you find that this macro fails to paste the correct info in the intended email fields, try inserting a Pause action in between actions. In the Actions pane, search for pause, then double-click on Pause, set it to 1 second, then drag and drop it right before the action that inserts the email address, for example.

Here's what the rest of the macro should look like since the last screenshot after Step 12:

Keyboard Maestro macro to email title and url of current web page

We're almost done. We just need to add one more action to send the email. First, do a dry run to make sure the macro works up until this point. Once you're satisfied, just add a Type a Keystroke action set to shift-command-D, which is the keyboard shortcut for Send.

As exciting as it is to see how much time this macro saves you, please use it responsibly.