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: