Get Value of Total Macros in Selected Groups

Does anyone know a method to get this number into a Variable?

In other words, I have multiple-selected some Macro Groups and I would like to find the total number of Macros those Groups contain. In this case there are 314 Macros in those selected Groups. I can see the number (in the image above) but I can't find a way to have that number as a Variable to make use of. I wonder if there is an AppleScript that can return this number? Thanks for any help.

(I am making a Progress Bar and can use that total to calculate progress of a Macro that works its way through each Macro in selected Groups.)

The following macro will return the macro count. It just extract the text of the text element. If you want something more robust, another way is to use Applescript to get all selected macro groups and then retrieve their macro count and sum them up.

[EXP] Get Macro count of selected Macro Group.kmmacros (3.1 KB)

1 Like

Great. Thank you @macdevign_mac!

This is something more robust since it does not depend on UI element positioning, and using Applescript to retrieve the macro groups and their macros for the count

tell application "Keyboard Maestro"
	set grpList to selected macro groups
	set totalMacroCount to 0
	repeat with curGroup in grpList
		set macroList to every macro of curGroup
		set macroCount to length of macroList
		set totalMacroCount to totalMacroCount + macroCount
	end repeat
	return totalMacroCount
end tell
1 Like

Yes, that is stronger, as I was finding the Text solution could sometimes return for example, "1 of 773 Selected" instead of "773 Macros" which then made extracting the total harder.

Thanks again! Hope this is of use to others too.

And in an Execute JavaScript for Automation action you could write:

Application("Keyboard Maestro").selectedMacroGroups()
    (sofar, group) => sofar + group.macros.length,

just curious, how do you find out those methods of Application("Keyboard Maestro") since there is no documentation? I try to query the Application object but to no avail.

The documentation is essentially the sdef file, which you can view in either AppleScript or JavaScript mode from:

Script Editor > File > Open Dictionary > Keyboard

and, of course, to test the code in Script Editor you need to set the language drop down at top left.

1 Like

I see. thank