Use a named clipboard that changes name based on the iteration of my FOR EACH loop

I am trying to reference a named clipboard that changes name based on the iteration of my FOR EACH loop.

Similar concept to Peter's solution with variables here:

Can this be done? ( I am using KM 7.1.1 at the moment).

This code is not working:

No, you can’t use a similar technique for styled text from Named Clipboards.

You could do it using the more tricky XML technique shown here:

Not sure I get this concept (after reading the post)…

Basically, you need to get the XML for the action you want to mangle (Set Clipboard to Styled Text) and adjust the text of the named clipboard within the action and then execute the XML as an action using AppleScript.

You can get the XML for the action from the Gear menu Copy as XML.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

Then you need to adjust the XML for the StyledText to match %NamedClipboard%WHATEVER%, and then run the XML using the AppleScript:

tell application "Keyboard Maestro Engine"
	set v to getvariable "ActionXML"
	do script v
end tell

Unfortunately, for your purposes, to use the Styled Text, you need to adjust the StyledText field, and while that is doable, it is difficult as well. @JMichaelTX has a script for doing it here:

But it is tricky to put it all together.

Perhaps a simpler solution is to use a Switch statement, switch on the variable and each branch does a different named clipboard.


I will try the SWITCH approach. Thanks also for the info on the XML approach which sounds interesting here and perhaps in other KM Actions (?)