Programmatically Capture X Recent Textual Clipboard Items?

What you have done seems pretty straightforward. If you were, for instance, wanting to capture the previous 250 clipboard items, it might be a PITA to manually create 240 more of those. In that case you might want to consider how to programmatically generate all of those "Set Variable" actions. I can think of a few different approaches:

  • Generate the variable names and the %PastClipboard%... strings using text variables inside a loop. See the KBM Wiki page action:Set Variable to Text [Keyboard Maestro Wiki] and scroll down to the section on " How To Use Dynamic Variable Names". I'm not very good at this (I've never done it), so I leave it to someone else to explain how to do that. Try searching the Forum for "Dynamic Variable".

  • Use @DanThomas 's KMET macro set (Keyboard Maestro Edit as Text) which allows you to export an entire group as JSON or XML and by default opens the text in BBEdit. You could write a little shell script that spits out the JSON or XML for each incremented Set Variable action and insert that into the macro wrapper as exported by KMET. I don't know what the size limit might be for the number of variables but chances are your clipboard history is not big enough to be an issue.

  • As noted in Setting Multiple Varibles at Once..., for a large number of values, storing them in a single JSON structure is more efficient and cleaner.

  • I have also heard of the "TFL Set Multi Variables" plugin to set multiple variables in a single action, but I've never used plugins, let alone that one.

As for skipping the ones that are not text, you can determine if a clipboard item is text or something else using the Clipboard Condition as a test within an "If All Conditions Met" action. The condition "has text" tests for text content, while "does not have text" indicates an image, file, or other data.

If you want more detail than just Text vs. Non-Text, take a look at this:

1 Like