How to transform text by a shortcut

March 08, 2021

Recently I’ve been copying links from interesting pages and then adding them to a Markdown document in my knowledge repo. My workflow is that I always have to format the link accordingly. Here is an example how a link looks like in Markdown:

Link: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
Output: [github.com](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)

I found this workflow annoying after some time, so I started looking for a solution and found Automator from Apple. This tool is built into macOS and allows you to run automation workflow. You can then a automation by a keyboard shortcut. In this blog post, I’ll show you how you can also do that.

Let’s create the script

  1. First open Automator by navigating to your application folder or by using Spotlight (CMD + Space and search for Automator) Create new script
  2. Create new automation by selecting Quick Action.

Drag execute javascript 4. Configure script settings

  • Workflow receives current: text
  • In Any application
  1. Search for the Run Javascript action and drag it into the right corner
  2. Search for Copy to clipboard action and drag after the javascript action
  3. Add the javascript code
function run(input, parameters) {
	var url = `${input}`;
	var domain = url.match(/^(?:https?:)?(?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/)

	return `[${domain[1]}](${input})`;
}
  1. Safe the workflow with a nice name

Execute that script by a shortcut

Set the shortcut in settings

The final step is to configure a shortcut which executes the workflow.

  1. Go to Settings -> Keyboard -> Shortcuts
  2. Search by your workflow name and configure a key combination e.g. ^ + ALt + CMD + C

Further ideas

  • You can build an army of formating helper functions.
  • Something I want to improve is for example to add a menu where I can select between different options.
  • You can use this approach to run any code. I use this for sending a selected url to a webhook which then triggers a n8n workflow.
  • Or you can just use the given functionality of automator to manipulate images.

Written by Mathias Michel who lives and works in Munich building useful things. Follow me on Twitter