How to create Automator workflow to paste the clipboard in the current text window?

ok, new to Mac, longtime Linux guy, so somewhat at home, and loving it!

i’m trying to create a Automator Quick Action that will:

  1. grab text from the clipboard.
  2. format that text with an awk script (it’s a bit complicated).
  3. put the reformatted text in the clipboard.
  4. paste the reformatted text into the current text box (usually within a browser window, etc).

the good news is i’ve got steps 1, 2 and 3 working fine. i can for instance have TextEditor window open up and display the properly formatted text, so i know i’ve already got the heavy lifting done.

the thing that’s troubling me is step 4, automagically pasting the text into my current (text editing) window. i know i can just do command-v on the keyboard and there’s my text but i’ll be needing to do this whole business many times a day so i’d rather just have it paste in the text so i can quickly move on to the next bit of work.

i have my Quick Action showing up as a Service and all is well but … any suggestions how i might do the last step and tell Automator to paste my clipboard into my current window?

Automator should be a last resort at the moment if you want to future proof your work. Sometimes it is still the best option, but Apple are putting their automation focus on Shortcuts for several years now and at some point Automator may be deprecated. Shortcuts carries forwards a lot of inspiration from Automator, so you should find the approach familiar assuming you haven’t touched Shortcuts before.

That being said I did start by looking to see what could be done with Automator anyway,

The first thing I tried was experimenting with the Workflow Receives setting and the Output replaces selected text on the Automator Quick Action settings.

Setting the receives to “no input” along with the enabled output dropped the Quick Action from the services menu. Presumably it is an invalid combo as far as Automator is concerned. Changing it to text like this works (with a simple awk instruction to reverse and output the text on the clipboard (supplied via the use of pbpaste (the clipboard on macOS is technically the pasteboard, hence the pb prefix)).

However, this then only shows up when there is selected text. So that would b great if you wanted to transform selected text rather than your clipboard (in which case you could use this approach and use Automator to get the text selection and forego the clipboard). Assuming you are not replacing text, then that’s a no go.

Although you could trigger via keyboard shortcut which might work around that - you could try it via the Keyboard Settings on your Mac.

The next thing I considered was to use some AppleScript to paste the result after placing the script output on the clipboard.

tell application "System Events" to keystroke "v" using command down

This actually gave me an error telling me the Automator helper was not permitted to send application keystrokes. I tried a similar approach in Shortcuts and it gave the same error but for the Shortcuts helper. This is interesting to note from a security perspective, but fortunately Shortcuts has a better option anyway, which does work.

You can set a Shortcuts shortcut as a Quick Action and you can also set it to output, just like Automator, but here the options to include in the Services menu and not require text selection to output are more forgiving.

Here’s an example shortcut using the same script as before, and you can download it with the link below.

https://www.icloud.com/shortcuts/b602abfb87aa40ac865db15036deacd3

pbpaste gets the clipboard content and as in Automator, I can just pipe that straight into awk. I think that’s the most efficient approach for you at this point rather than dealing with Shortcuts’ variables, though that is also an option. I think in this case I’d probably just script it like this anyway and have it contained rather than adding in more “colour” from Shortcuts.

I did actually try piping this into pbcopy in the script to put the content on the clipboard, but there seemed to be a timing issue and it didn’t work reliably for me. Instead, what I did was simplify things to just have Shortcuts output the shell result (to wherever the cursor is). This leaves the clipboard intact, which may or may not be desirable. It depends what you want to do with the clipboard next, if anything. If you do want the clipboard modified, you can insert a Copy to Clipboard action after the script step and it will copy to the clipboard just before outputting at the cursor, and that seemed to work reliably when I gave it some quick tests.

Hope that helps lay things out a little, and I am sure there are more ways and tweaks that could be applied. There may even be a better way to do it with Automator than what I tried. I must admit I create new Automator workflows so infrequently since Shortcuts came to the Mac that it’s getting fuzzy now; but hence I’d come back to recommending Shortcuts over Automator … though as you’ll derive from a number of conversations on the forum, Shortcuts does have its issues, so Automator’s stability and maturity do have that still going for it.

1 Like

wow! MANY thanks for that excellent little tutorial, and the guidance toward Shortcuts vs Automator. regarding this particular little project, i just ended up using the clipboard because it seemed reliable and expedient. my actual process is: select or copy text on page X, transform text through sed or awk, paste transformed text on page Y. anything that accomplishes that end is fine by me. the original text–in the keyboard or otherwise–is no longer of interest once the paste is done.

having looked at both Automator and Shortcuts recently – created a Shortcut to cycle to the next (random) wallpaper from a collection i’ve saved over the years – i assumed Shortcuts was the older method and Automator the most recent. no worries, live and learn, happy to switch to Shortcuts if that’s the way to go.

that points to another (largely unrelated) question that’s come up as i google around for how to do these things … are proprietary apps like Alfred and KeyboardMaestro worth looking into if one is going to be doing a lot of this? i was a coder in a former life so i do still find this kind of thing – automations, scripting, etc – satisfying and, well, a bit of the old buzz i suppose.

Probably everyone here is an enabler for such tools. :smile:

If you haven’t already, go through the back catalogue of episodes and listen to the shows on those apps and maybe check out the topics of interest that are more general to get an idea of what options are out there.

3 Likes

will do, thanks again. :+1: :v: :vulcan_salute:

Hi all, my first post on this site so I apologise if posting in an older thread is not the done thing.

While I am a long term Mac user I have never really used automation or the command line so far. However I have decided its time to learn.

I found this thread really useful as I am trying to create a service that cleans unwanted spaces from the selected text in any application. Referring to the screen shot above I duplicated the Quick Action and tried it with the result of all the words in the text being reversed. Unfortunately all of the newline characters are being removed which is undesirable. When the same command is run in the terminal the result included the line endings.

I have created an Awk command that removes multiple spaces which again works when run from the terminal but strips the line endings when run/called from automator.

Is this a know feature / fault of Automator or have I missed a setting? My workflow is below:

I know that Automator has been replaced but I need my something to remove extra spaces on Big Sur as the laptop is circa 2013.

best wishes

S

It would have been better to post this as a different topic as it is a different topic - you can always include a link to another topic to note it helped but didn’t resolve your issue. Keeping things separate makes them easier for people to discover, keeps things aligned, etc.

I think the terminal command you want is more something like this:

pbpaste | awk '{gsub(/ +/, " "); print}'

So rather than echo-ing (which would also have added a newline to the end without a -n option), this is just piping the clipboard content straight into awk. Then the substitution is replacing one or more spaces with a single space, and outputting the result.

If I put this text on my clipboard:

This line has single spaces
This  line  has  double  spaces
This line has single spaces

This  line  has  double  spaces
This line has single spaces
This  line  has  double  spaces

… and run the command above, I get this output:

This line has single spaces
This line has double spaces
This line has single spaces

This line has double spaces
This line has single spaces
This line has double spaces

All the newlines remain intact and the double spaces are reduced to single spaces.

Now, if we transfer this approach into an Automator quick action for selected text, we can actually drop the piping of the clipboard because we are going to use Automator to grab the text selection and pass it in. It is always neater not to use the clipboard if you can at all avoid doing so. For example, if you had something useful on your clipboard and run the quick action, you won’t have to go and copy it again after or use a clipboard history manager, because it will still be there untouched.

We can use the same awk command and set the script step to use the input from Automator. In addition we can set Automator to replace the text as you had done in your screenshot above.

The result should look something very much like this:

2025-01-24-20.13.52

Using the original text above in an editor, I can select the source text, choose the ‘Multiple to Single Spaces’ entry in the Services sub menu of the context menu when I right click on the text, and a fe moments later, it is replaced by the same result - the only change being the double spaces become single spaces.

Hope that helps.

Thank you for your reply and working solution.

I obviously need to learn more about the command line tools that are available so have ordered a book.

Thanks again

S