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" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>JustDisplay</key>
		<false/>
		<key>MacroActionType</key>
		<string>SetClipboardToText</string>
		<key>StyledText</key>
		<data>
		cnRmZAAAAAADAAAAAgAAAAcAAABUWFQucnRmAQAAAC5kAQAAKwAAAAEAAABc
		AQAAe1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcY29jb2FydGYxNTA0XGNvY29h
		c3VicnRmODMwCntcZm9udHRibFxmMFxmbmlsXGZjaGFyc2V0MCBIZWx2ZXRp
		Y2FOZXVlO30Ke1xjb2xvcnRibDtccmVkMjU1XGdyZWVuMjU1XGJsdWUyNTU7
		XHJlZDBcZ3JlZW4wXGJsdWUwO30Ke1wqXGV4cGFuZGVkY29sb3J0Ymw7O1xj
		c2dyYXlcYzA7fQpccGFyZFx0eDU2MFx0eDExMjBcdHgxNjgwXHR4MjI0MFx0
		eDI4MDBcdHgzMzYwXHR4MzkyMFx0eDQ0ODBcdHg1MDQwXHR4NTYwMFx0eDYx
		NjBcdHg2NzIwXHBhcmRpcm5hdHVyYWxccGFydGlnaHRlbmZhY3RvcjAKClxm
		MFxmczI2IFxjZjIgJU5hbWVkQ2xpcGJvYXJkJXdvbWJhdCV9AQAAACMAAAAB
		AAAABwAAAFRYVC5ydGYQAAAA5lFpWrYBAAAAAAAAAAAAAA==
		</data>
		<key>Text</key>
		<string>%NamedClipboard%wombat%</string>
	</dict>
</array>
</plist>

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.

2 Likes

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 (?)