I'm trying to capture a number of the past clipboard items. Is there a better way to do this? Also, how can I skip all non-text clipboard items in this process?
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:
Is it always the same number, or does that vary? What are you going to do with those captured items after this?
The "KM way" would be to load each item onto the System (or maybe a Named) Clipboard and test it with a Clipboard Condition (you can't use that directly on past clipboards).
Demo that puts all the text into a pseudo array -- as @August says, that's much easier than using Dynamic Variables and you may find it's enough for what you want:
Collect Last n Text Items from Clipboard History.kmmacros (9.4 KB)
Maybe we should first ask why..?
Thank you @August and @Nige_S!
I'm passing these results to a speech-to-text script that can refer dynamically to past clipboard items as I verbally reference them.
That doesn't tell us much.
One problem with Dynamic Variables, for example, is accessing their values later. If you are creating an unknown (when you write your macro) number of variables -- Local_var1, Local_var2,...,Local_var*n* -- you have to be careful about trying to get values from variables that were never created and not ignoring variables you did create. Pseudo arrays make that easy because you can always find out how many elements there are with Local_myArray[0]==myDelim==.
But if you always create 10 variables, for example, that's not a problem. The only "dynamic" bit is creating your variable names as you iterate through a loop -- you know you'll have Local_myVar1 thru Local_myVar10 at the end of that and can access them "normally".
So you're squidging your OP's multiple "Set Variable"s into a loop, with an added inner loop to step over any Clipboard History item that doesn't contain text. And you can avoid "promoting" History items to the System Clipboard, which should make things faster.
Demo:
Collect Last n Text Items from Clipboard History (Dynamic).kmmacros (8.3 KB)
You will, of course, skip any Clipboard History item that matches (and you could make it a case-sensitive match with regex) the text "No Text in Clipboard" ![]()
Great solution!


