Manipulating Selected Text in macOS with Alfred Workflows
Alfred excels at speeding up repetitive tasks and making laborious jobs easier; Whether it's formatting text on the fly or manipulating text for convenience, a quick workflow is all it takes!
We'll take a look at two examples in this post: In the first one, we'll grab the selected text in macOS and paste it as plain text. In the second one, we'll copy a selection and replace part of it before replacing the original text.
The examples in this post are created using 2 or 3 objects, and no scripting or coding is required. You can download the workflow to take a closer look as we go through the examples.
Bringing selected text into a workflow
Before we can manipulate the selected text in macOS, we need to bring it into a workflow.
This is a simple task with the Hotkey trigger object (in yellow, in the screenshot below), using its Argument option, which passes the selection into the workflow as plain text.
We now have the selected text in macOS coming into our workflow. For this to be useful, we need to get it back out of the workflow.
There are multiple ways to do this, but we'll use the Copy to Clipboard output object (in green) here, as this allows direct manipulation and replacement of the selected text when using the hotkey.
This gives us a very basic, but already useful workflow, which takes the current selection in macOS and places a plain text copy of it into the clipboard.
We can take this one step further; By selecting the "Automatically paste to frontmost app" option in the Copy to Clipboard configuration, the selected text in macOS will automatically be replaced when you use the hotkey.
This can be particularly useful when a work colleague sends you a document filled with terrible formatting. Let's work a little magic on it by using our hotkey to turn it to plain text.
That's much easier to read!
More advanced processing
What else can we do once we have an input and an output? We can process the selected text further to perform a particular task, like replacing part of the copied text string before pasting it.
Let's create a workflow just like the one above, with the Hotkey and Copy to Clipboard object, and let's add a Replace utility object (in pink) between them.
The Replace utility is configured to simply replace http:// with https://, ensuring we're visiting websites more securely.
Now, when I select a URL in my browser's address bar and press the hotkey combination, the http:// part of the URL is automatically replaced with https://. The new string is then pasted, replacing the original one, and pressing Enter launches the new URL.
Endless possibilities with workflows
These are two of the simplest examples illustrating how you can manipulate selected text with a workflow.
Now that you understand how to use an input and an output, you can process your macOS selection in more advanced ways, either with Alfred's built-in objects or using your own scripts to work with the text you've selected.
Using a workflow for repetitive tasks not only saves you manual typing, but also gives you consistency with the knowledge that you haven't missed a quote or made a typo error. Get it right the first time, every time!
Think about your own daily tasks and see how you can simplify them with workflows. And of course, take a look at our Guides and Tutorials to help you discover workflows and more.