Palettes - A couple of requests

Totally. Your macro wasn't incomplete in any way; I just have a preferred way to use it.

I managed to generate a macro that included the action, but the sticking point was getting it to appear in the same group as the currently selected macros. I got as far as it generating in a new group (with the wrong icon). For the sake of my own education, I might try again later.

Look away now if you'd rather try yourself. Otherwise:

Sneak a Peek!

Here's making a new macro in the same Group as a selected macro:

tell application "Keyboard Maestro"
	set theGroup to macro group of item 1 of (get selection)
	tell theGroup
		make new macro with properties {name:"Created by AppleScript"}
	end tell
end tell

Correct me if I'm wrong but isn't that the same as just hitting โŒ˜N?

What I was trying to do was create a new macro in the current group with a pre-populated Show Palette of Macros action and a designated hotkey trigger, named "- Show Palette", with a custom icon.

I got awfully stuck and confused. I did actually get it to import a macro at one point, albeit in a new group with the wrong icon... but I deleted all that work this afternoon, tried to recreate it (after four G&T's) just now, and failed. :woozy_face:

- Show Palette of Macros - Create.kmmacros (67 KB)

Macro screenshot

Here's one of my Show Palette macros in case you fancy having a go:

- Show Palette.kmmacros (50.4 KB)

Well, you said that...

..which is what that AppleScript snippet does. One sticking point at a time!

I'm an AS busker, so I have no idea whether that can be used to create a macro with specific XML, or just a blank macro... Hopefully the former! :grimacing:

If you look at the KM AppleScript dictionary, you'll see that for macro the xml property is read-only -- so not the former. But a macro contains actions and when you look at action you'll see that the xml property is editable. So the route appears to be:

  1. Make a new macro in the Group where your selection is
  2. Make a new action in that macro
  3. Set the xml of the action

It's the XML that sets the action "type", which gives it the appropriate icon etc. So if we get the XML from an empty "Show Palette of Macros" we can use that as our template.

Putting the above together:

set theXML to "<?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>ActionUID</key>
	<integer>11267884</integer>
	<key>MacroActionType</key>
	<string>ShowPaletteOfMacros</string>
	<key>TimeOutAbortsMacro</key>
	<true/>
</dict>
</plist>
"

tell application "Keyboard Maestro"
	set theGroup to macro group of item 1 of (get selection)
	tell theGroup to set theMacro to make new macro with properties {name:"Created by AppleScript"}
	tell theMacro to make new action with properties {name:"Created by AppleScript", xml:theXML}
end tell

Luckily KM is smarter than me and knows that the action UID is read-only and unique -- when we pump in the XML above the value we pass as "ActionUID" is ignored/replaced with the new action's UID.

So all we have to do now is populate the palette as we did previously, except using AppleScript's text manipulators instead of KM's. One for tomorrow, I think...

Ok great! This creates the macro properly. Any idea how to set the icon and hotkey trigger?

Show Palette of Macros - Create.kmmacros (48 KB)

Macro screenshot

I managed to get the syntax to add in the Trigger.

tell theMacro to make new trigger with properties {xml:txml}

So, the below works to do everything apart from setting the Icon.

There must be the equivalent to set the Icon as it can be seen in the Macro XML but not sure what it is.

Show Palette of Macros - Create (with trigger).kmmacros (48.2 KB)

Click to Show Image of Macro

1 Like

@Zabobon's done the trigger part -- but do you want the same trigger for all of these (managing via Group enabled/disabled and/or the Conflict Palette) or do you want to set triggers at time of creation? The second could be tricky -- not sure how you'd get modifier keys into a standard AS "Prompt for answer" dialog, but I can see a two-step process where you put up a multi-select "Pick from list..." to select your modifiers and then a "Prompt for answer..." for the keystroke (possibly using "\r", \f1", etc to enter "unusual" keystrokes).

I can't see anything in the AS dictionary about icons, and I've never played with them myself. But do you want the same icon every time? If so, another approach might be open -- in KM you create an empty template macro, complete with hotkeys and icon. Then we use AS to duplicate templateMacro to selectedGroup then set the macro name, add actions, etc.

2 Likes

Awesome! The icon is obviously a luxury feature, and I'm mostly curious about how to do this in case it ever comes up again. This may be the first thread to have discussed macro auto-creation in as much depth, so it's not a bad thing to explore.

Me? For this use? Yes. :blush: If there were an easy way to make it more versatile, that would probably be handy for other uses, but it doesn't seem that straight-forward.

1 Like

OK -- so the icon is stored in the macro's XML, in <key>CustomIconData</key><data>...ImageDataHere...</data>. Since the XML of a macro is r/o via AppleScript, not much joy there.

You've used ยง as the hotkey trigger in your example, so I have here.

Here's one that does the creation using AS then switches to KM to set the icon -- I've used a named clipboard called "Palette Icon" with your icon on it, so set that up or change the macro to suit.

Create Macro Palette (Improved).kmmacros (35.7 KB)

Image

It wouldn't take much to let the user set the name of the new macro at runtime, and I reckon you could also choose an icon by selection then posting it to the named clipboard. And it looks like @Zabobon has ideas on how to set the hotkey trigger via user input...

For completeness -- doing the same via duplication needs only the AppleScript and so will save flipping into KM, but you have to do some setup first. Create a new, disabled, Group called "Templates" and in that create a new macro with no actions, setting its name, icon and trigger. You're then ready for the following:

Create Macro Palette (by Duplication).kmmacros (3.5 KB)

Image

Again, you could easily rename by dialog and use whatever @Zabobon comes up with to set the hotkey, but if wanted to choose an icon you'd probably be better with the version above.

1 Like

The template duplication idea is an interesting one for sure.

Assuming you don't already have a macro in the current group called "- Show Palette", you can set the icon without using found images (maybe it's just me, but I try to avoid them):

This might be the safest way (for now).

Trying something else, and I'm not confident in the "write trigger by XML" method. I don't think that one is clever enough to spot and rewrite a duplicate UID and I think I've been generating trigger clashes by cloning the XML from something else.

Ignore me -- while folder action triggers have UIDs, hotkey triggers don't. I did wonder why it wasn't misbehaving in tests!