Calculating the Next Number for a Macro Naming Scheme

Going out on a limb a little...

I have a numbering system for my macros.

Even though the new addition of 'jump to macro from the debugger' has been a huge addition for me that I use all the time...so I rely on the numbers much less...

I was wondering if there is a way to find the next available number so I can number the next macro I'm creating.

My numbers are Kxxxx, always a K followed by 4 digits.

Is there a way to query my KM macros and show me the highest number to date?

And / Or if there is a whole in the sequence? (number missing)

Cheers

Keyboard Maestro's global variables are accessible from all macros and are persistent across invocations. If you keep the last macro number in a global variable, you can retrieve it and increment it in any of your macros.

To preset the variable, create a new variable in KM's Variables preference pane and fill it with your current highest number.

As for figuring out where the holes are in your numbering system, I think that could be done via AppleScript, but there may be an easier way.

Woof! Such a simple solution, awesome. The only catch is that I work on 6 to 10 different computers. So I guess I’ll just create a macro named next macro number and put the number in the title of the macro and that’ll be synced to all the computers. How simple!!
Awesome solution, thank you!

Yes, this is certainly doable.

Where exactly are the numbers in the macro names - at the start, end? And just to be safe, can you include a couple of names? Thanks.

Hey @DanThomas , really want to say how appreciative I am of your time and expertise on this forum, really excellent stuff my friend..... seriously, thank YOU.

  • ok, yeah, they are always at the end of the macro name ie.
    Multi ⇧βŒ₯ Space Shift Option Time - Set K0345
    Kens βŒƒβŒ˜ Scroll Up Cnt Cmd VARIABLE K0099
    Multi Kens Global ⌘+⌻B4 Cmd B4 VARIOUS K0027
    Multi Left ⌘ Command VARIOUS K0010
    the word 'various' is not always in the title, but definitely the last 5 characters of the macro name are always Kxxxx
    Thanx man!

I think this JXA code should do the trick:

(function () {
	'use strict';

	const _km = Application("Keyboard Maestro");

	function execute() {
		var names = Array.from(_km.macros.name());
		return names.reduce((prev, curr) => {
			var match = /\d{4}$/gm.exec(curr);
			return match ? Math.max(prev, parseInt(match)) : 0;
		}, 0);
	}

	try {
		return execute();
	} catch (e) {
		return "Error: " + e.message;
	}

})();

Use it something like this:

image

Β 
Get Max Macro Number.kmmacros (4.6 KB)

2 Likes

Hey @DanThomas , Happy New Year to you man.....

  • When I run the macro it returns 'Max Number: 0'
    I have it in my KM group and there are macros in that group that have the Kxxxx at the end of the macro title.

Make a new macro, and add an "Execute JavaScript for Automation" action.

Set it to "display results in a window", and paste this into it:

(function () {
	'use strict';

	const _km = Application("Keyboard Maestro");

	function execute() {
		var names = Array.from(_km.macros.name());
		return '"' + names.join('"\n"') + '"'
		// return names.reduce((prev, curr) => {
		// 	var match = /\d{4}$/gm.exec(curr);
		// 	return match ? Math.max(prev, parseInt(match)) : 0;
		// }, 0);
	}

	try {
		return execute();
	} catch (e) {
		return "Error: " + e.message;
	}

})();

Run it, and give me the results - not all of them, just 10 or 20 lines. You can pick and choose which lines to post, if there are names you don't want to share. Just don't alter the lines you do share.

"Multi Kens FMP β‡§βŒƒβŒ₯⌘+B2 ShConOptCom VARIOUS 0012"
"Multi Kens FMP β‡§βŒƒβŒ₯⌘+B4 ShConOptCom VARIOUS K0013"
"Multi Kens FMP βŒƒβŒ₯⌘+B2 ConOptCom VARIOUS K0019"
"Multi Kens FMP βŒƒβŒ₯⌘+B4 ConOptCom VARIOUS K0014"
"Multi Kens Global ⇧+B2 Shift VARIOUS K0005"
"Multi Kens Global β‡§βŒƒ+B2 Shift Control VARIOUS K0006"
"Multi Kens Global β‡§βŒ˜ ShCom Scroll Up VARIOUS K0026"
"Multi Kens Global βŒƒ+B2 Control VARIOUS K0007"
"Multi Kens Global βŒƒβŒ₯+B2 Cnt Opt VARIOUS K0028"
"Multi Kens Global ⌘+⌻B4 Cmd B4 VARIOUS K0027"
"Multi Kens Global ⌘+B2 Cmd B2 VARIOUS K0029"
"Multi Kens Global βŒ₯+B2 Option VARIOUS K0008"
"Multi Kens Global βŒ₯+B4 Option Button 4 VARIOUS 009"
"Multi Kens Global ⌻B2 VARIOUS K0021"
"Multi Kens Global ⌻B4 USE VARIOUS K0017"
"Multi Kens Global Cnt Cmd B2 VARIOUS K0015"
"Multi Kens GlobalCnt Cmd B4 VARIOUS K0016"

Thanks. Try this:

(function () {
	'use strict';

	const _km = Application("Keyboard Maestro");

	function execute() {
		var names = Array.from(_km.macros.name());
		return names.reduce((prev, curr) => {
			var match = /\d{4}$/gm.exec(curr);
			return match ? Math.max(prev, parseInt(match)) : prev;
		}, 0);
	}

	try {
		return execute();
	} catch (e) {
		return "Error: " + e.message;
	}

})();

Assuming that works:

Right now, if something ends in 4 digits, that's the number I'm using. So, for example, if something ends in only 3 digits, it doesn't get included. I'm not checking for "K".

Also, I'm doing this for all your macros, not just the ones in the group you this from.

Let me know if that's how you want it to work or not.

2 Likes

Wow, a thing of beauty Dan, really... Perfect...
Thank you.

2 Likes