Export of Macro XML via Clipboard or AppleScript

Here are two test Macros to export a Macro's XML and then import it into Keyboard Maestro as a new Macro.

The first demo works (using Keyboard Maestro's menu option "Copy as... XML")

The second macro fails. It uses AppleScript to get the XML and fails on the import part.

I am assuming the problem is with the AppleScript I have for exporting the XML but I think the syntax is correct and it does indeed save the XML to the Variable. For some reason the XML exported via AppleScript is not liked for importing the Macro.

I've searched the Forum and Google Searches and Claude AI but not solved this so, I'm here asking for help!

TEST Export and Import Macro via XML - via Clipboard.kmmacros (4.4 KB)

Click to Show Image of Macro

TEST Export and Import Macro via XML - via AppleScript.kmmacros (4.1 KB)

Click to Show Image of Macro

I had never got this to work either. But, spurred on by it being someone else's problem rather than something I'd just work round...

I think the copied macro XML is missing important information needed for an import -- specifically, the Macro Group to import into. You attempt, and my previous, has the XML as eg:

<?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">
<dict>
	<key>Actions</key>
	...etc
	<key>UID</key>
	<string>50601C6C-2969-4A24-ABF8-636F6D4F1826</string>
</dict>
</plist>

Comparing that with @peternlewis's template in his "Create Text Expansions Macro":

<?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>
		<key>Activate</key>
		<string>Normal</string>
		<key>Macros</key>
		<array>
			<dict>
				<key>Actions</key>
				...etc
			</dict>
		</array>
		<key>Name</key>
		<string>Text Expansion</string>
	</dict>
</array>
</plist>

...and we can see the extra lines required and where to inject them into your copied XML. You'll need to change the name for the Macro Group, obvs :wink:

Try this -- Group Name to import into is stored in the first action:

TEST Export and Import Macro via XML - via AppleScript v2.kmmacros (5.2 KB)

Image

1 Like

You can also use this macro set:

1 Like

Thanks @Nige_S and @DanThomas
I’m away from Mac right now but plenty for me to look at when I’m back.

Yes, that did it. I have added some AppleScript to automatically get the Group name and moved your code into the first part so that it assembles an XML ready to be imported. I left my original Import AppleScript unchanged as the generated XML is now good to be imported.

The goal in all this was to have a method that exports the XML text in a method that doesn't need replicating menu commands and doesn't use the System Clipboard and this now does this.

@Nige_S you are a genius. (@DanThomas you are a genius too, of course!)

Here is my final Macro (the AppleScript is in three Actions so that I know what is going on). The Magenta section does the Export - without needing to use the Clipboard. And the Purple section does the Import.

TEST Export and Import Macro via XML - via AppleScript v3.kmmacros (7.8 KB)

Click to Show Image of Macro