How to automate generating a Keyboard Maestro Macro

Have people made macros that do things in the KBM Editor? Where to start?

I would like to do the sort of thing described in this macro:
[FILE] Process Lines in Text File
and use it to add elements to a KBM macro that I am creating.

When I'm writing a macro, I usually start by outlining how it works. When I have detailed the steps in text enough that I'm pretty sure that I know how to perform each step with KBM actions, I then translate that outline into groups of KBM steps. I name the Groups according to the outline steps. The idea is that if I collapse all the groups to just their titles, it will read as a top-level overview of what the macro does.

So at some point I have a file with a list of steps that I want my macro to perform and for each line in the file, I want my macro to have a Group with that name. Inside each group I usually start with a comment explaining what's going on, if the task of the group has any complexity at all. So to automate getting that started, I would like each group to contain a Comment action with a Title that is the same text as the group title.

For example, here are two lines from the middle of the list of steps:

11	Hot key control shift command 3 to capture entire screen to clipboard
12	Save Image as Icon of Saved Filename

And here are the resulting two groups in the sequence in the in-progress macro:

image

I would probably want to polish it by doing something like letting the numbered list be the titles of the Groups and remove the numbers from the contained comments, but clearly that would be simply editing the clipboard contents or variable value in between the paste operations.

How do I best go about getting KBM to generate KBM? Any ideas?

See:

1 Like

Thanks @peternlewis,

I may have seen that before and the idea that it was possible stuck. Unfortunately that thread sidetracks into Text Expansion rather than being about automating Macro Creation.

To makes sure I understand, your structure is to build a KBM variable that contains the XML of the KBM macro (or action, group, etc.) that you intend to paste in. Then open AppleScript to read that variable from KBM and then send it back to KBM via the File>Import>ImportMacros command.

I'm a little unclear on a couple of points.

First, I would be wanting to import Actions, not macros. I'm confused about how to make that switch.

In the AppleScript, the command to KBM is importMacros with a string argument, while the KBM menu command title is "File > Import > Import Macros Safely" and it wants a filename of what to import. Where do I find documentation on how to convert from the menu name to the AppleScript command?

Second, is there a problem with doing it by menu instead of by AppleScript?

In this action by @JMichaelTx:
image
the macro explicitly selects a menu item in the running, foreground KBM Editor. Would it work to do that to import an action into the currently edited macro?

Instead of creating a variable and reading that variable from KBM in AppleScript, the macro would save the variable as a file and then import that file as an action.

For some reason, I find that more intuitive than the AppleScript route, I suppose because it uses the KBM menus, where I can "see" what's happening, rather than having to find out what the Action equivalent is of the undocumented (to me, so far) tell application "Keyboard Maestro" to importMacros e.

You can just generate the XML for the action list and Keyboard Maestro should be able to paste it in directly.

For example, if you copy the 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>MacroActionType</key>
		<string>SetVariableToText</string>
		<key>Text</key>
		<string>50</string>
		<key>Variable</key>
		<string>Local Size</string>
	</dict>
</array>
</plist>

(which I got by simply copying an action’s XML and then I removed the ActionID, which is a good idea but not necessary)

Anyway, if you copy that XML and just paste it in it to a macro it will paste as an action.

OK, I tried it. I copied the above XML right out of your post. I didn't even use the forum's Copy tool, I just highlighted it manually. It's in my clipboard as I went over to KBM, which happened to be open to a macro that had blank space in it:

image

I selected the "No Action" space and pressed ⌘V. Here's what I got:

image

That is really cool!

Can I also paste JSON (which is easier to read than XML)? Or do I need to convert it first? Is that what @DanThomas's KMET macros do?

Thanks!

No.

All Keyboard Maestro’s structures are in XML, not JSON.