KM8: Accessing and Controlling the Keyboard Maestro Editor

Continuing the discussion from How to set a keybinding to enable/disable action / multiple actions?:

Scripting the Keyboard Maestro editor (KM Wiki)


It took me a bit of time to figure this out.

I thought the command:
set macroList to selectedMacros

would give me a list of Macro objects.
But it does NOT. It just returns the UUID for each of the macros.

So here's how I got the current macro object:


tell application "Keyboard Maestro"
  #    Ver 1.1    2017-09-20
  
  set macroList to every macro whose selected is true
  
  --- Make Sure Only ONE Macro is Selected ---
  
  if ((count of macroList) = 1) then
    set oMacro to item 1 of macroList
  else
    error "Multiple Macros are selected.  Select only ONE and re-execute this script."
  end if
  
  --- Now We can Get/Set Macro Properties ---
  
  set macroName to name of oMacro
  
  return macroName
  
end tell

Properties of a Macro Object

from Script Debugger 6

If anyone has some good example scripts, please share.

2 Likes

selectedMacros is from the old dictionary, but still works.

What you want is:

tell application "Keyboard Maestro"
	selection
	selected macros
	selected macro groups
end tell
1 Like

Ah! I was using selectedMacros.

Well knowing that, here’s one way I could’ve counted the number of selected actions, to modify the “Enable/Disable X Actions” menu item:

tell application "Keyboard Maestro"
	set theActions to selection
	set theCount to count of theActions
	return theCount
end tell

@gglick’s method was much better, anyway. Watching this thread for more AppleScript/Keyboard Maestro tips.

Peter, maybe I'm missing something, but I'm not seeing any of those commands in the SDEF:

But I tested them and they seem to work.
We just need to document them in the Wiki.

tell application "Keyboard Maestro"
  set selList to selection
  set macroList to selected macros
  set grpList to selected macro groups
end tell

2 Likes

There not commands, they are properties of the application.

selection is a fairly standard property, and is part of the standard suite.

The other two are there as well. They probably should be in the Keyboard Maestro Suite application class instead, but I’m not changing that at this point (it’s probably safe enough to move it, but there is no sense doing it now without time for proper testing).

IME, yes, the application class has a property of selection. But in all cases I just checked, it is for selection of only one element class. What really mislead me is that there is a command for selectedMacros, so it never occurred to me to look for an Application property to select macros.

While I think the current design is misleading and makes it hard for users to discover the AppleScript access to KM, I suppose the important thing at this point is to provide extensive documentation in the Wiki so others will know what’s available.

Any other goodies hidden as properties of other classes?

I hope Chis can rejoin us soon, since he is the best person to fully explore and understand the new scripting model.

Peter, the initial statement I used to get a list of selected macros seems to be equivalent to the later one you gave me:

tell application "Keyboard Maestro"
  
  --- These Selection Statements Seem to be Equivalent ---
  
  set macroList2 to every macro whose selected is true   -- my initial statement
  set macroList1 to selected macros
  
end tell

Is there any difference you know of?

selection returns the currently selected objects. In an OSA compliant application, that means whatever is selected can be returned.

selection is how OSA is supposed to work. The old stuff is not, and even the selected macros is not how OSA is supposed to work, but OSA has issues with verbosity.

Well, that's an unfortunate side effect of backward compatibility.

I could just delete it, but that seems unnecessary.

There is a start on the wiki, including some meta-programming stuff for creating macros.

Absolutely. This whole section of 8.0 could be dedicated to Chris.

Only performance, they should give the same result.

1 Like

Just made a new topic that may be relevant to this discussion, as it contains macros that use scripts that in turn make use of KM8’s new editor scripting abilities: KM8: Copy, Paste, and Batch-Delete Triggers

1 Like

This is really nice. I can imagine modifying these in many ways… now I’ve got to go fumble my way through realizing them :joy:

1 Like