0

The contents of my clipboard are a series of 10 words separated by spaces. I would like to paste them into an application which requires each word to be entered individually into text boxes, where you can navigate to the next text box by pressing the Tab key.

How can I create an Automator script to split the contents of my clipboard on space, and then paste each word followed by the tab key?

1 Answer 1

1

How can I create an Automator script to split the contents of my clipboard on space, and then paste each word followed by the tab key?

You can use a Run AppleScript action in an Automator workflow to accomplish this.

Example AppleScript code:

set cbText to (the clipboard)

set AppleScript's text item delimiters to space
set cbTextAsList to text items of cbText
set AppleScript's text item delimiters to {}

repeat with aWord in cbTextAsList
    tell application "System Events"
        keystroke aWord
        delay 0.05
        key code 48 --  # Tab key
        delay 0.05
    end tell
end repeat

Notes:

  • The example AppleScript code was tested in Script Editor under macOS Catalina.
  • Whatever the words are to be typed into must be frontmost as this is required for UI Scripting code, which is what System Events is doing.
  • As coded, it assume focus is already in the first text box the first word is to be typed when the script is triggered.

If you really want to paste the words instead of typing, then use the following example AppleScript code instead:

set cbText to (the clipboard)

set AppleScript's text item delimiters to space
set cbTextAsList to text items of cbText
set AppleScript's text item delimiters to {}

repeat with aWord in cbTextAsList
    set the clipboard to ""
    delay 0.05
    set the clipboard to aWord
    delay 0.05
    tell application "System Events"
        keystroke "v" using command down
        delay 0.05
        key code 48 --  # Tab key
        delay 0.05
    end tell
end repeat

Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

4
  • Thank you, it works very well. One small issue is with permissions, as described here apple.stackexchange.com/a/405338/91027 with workarounds
    – mark
    Commented Mar 14, 2021 at 21:16
  • Potential pitfall: if contents of aWord ≠ (last item of cbTextAsList) in situations where aword does equal the last item in the list, but is not itself the last item in the list. I think a counter variable needs to be used to construct the loop.
    – CJK
    Commented Mar 15, 2021 at 4:26
  • @CJK, Yeah, I agree and will update it in the morning... off to sleep. Thanks. Commented Mar 15, 2021 at 5:20
  • @mark, I've removed the if statement as it could be problematic as mentioned by CJK's comment. If you do not want tab pressed after the last word, let me know and I add some conditional coding, otherwise I'll let it stand as it now is, thanks. Commented Mar 15, 2021 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .