MACROs: Key/Value Pairs Storage and Retrieval

MACROs: Key/Value Pairs Storage and Retrieval (Persistent Key/Values)

NOTE: This file is larger than most macro files, because it's got some embedded RTF in the documentation macro, and for whatever reason that makes it large. But the macros themselves are simple.

Key Value Pairs Macrosv1.0.kmmacros.zip (2.3 MB)

It's pretty common to need to store key/value pairs. That's what Dictionaries are for in many languages, as are Apple's own Property Lists (Plists).

Here's a couple of sub-macros that facilitate key/value pairs in Keyboard Maestro.

They work using search and replace actions with KM variables, so they should be reasonably quick.


They're very easy to use, as you can see above, although it might take you a couple of minutes to wrap your head around the format I use for passing parameters. But there's some good RTF documentation present with pictures and everything :slight_smile: , so it shouldn't be too hard to pick up.

You can't store multi-line values. If you need that ability, let me know and I have some ideas, but I didn't want to complicate things unnecessarily.

3 Likes

Dan, thanks for what I'm sure is another great macro.

But could you please explain to us why we need these macros, or what the use case is?

Since KM Variables are automatically persistent, it is not clear when we would need this alternate method.

Thanks.

I really struggled with an explanation, and I’m not happy with that. Let me tell you what I’m using it for, and maybe you can help. Or maybe it’s not needed at all. Anyway:

I’m using it specifically for a macro I’m just about done with. This macro lets me swap between editing two macros, and it maintains the vertical scroll position in the editor for both of the macros.

So I use the key/value pair macros for storing the scroll position for each macro. In other words, UUID=Scroll Position, or:

A57D7C69-A295-457E-872C-FC49AC342E41=0.649895178197065
499E8D39-E9D5-4F7D-891C-14EF8092D568=0.5750487329434698

Does this make sense? Often times, when I can’t explain something, it can mean that it isn’t needed. What do you think?

1 Like

I have the strongest sense of deja-vu. We’ve had this discussion before, haven’t we?

OK, I understand that.

But why couldn't you just store the same info in KM Variables?
IOW, what is the advantage of using your "Key/Value" system?

I am actually storing them in KM variables - well, one variable, anyway. But rather than litter up the variable space with one variable per macro, I store them all in one variable.

OK, I get that. If you have a lot if key/value pairs, all related, that makes sense.

Now, being devil's advocate: why reinvent the wheel?
What not just use the Mac standard of plists? You could still store the plist data in a KM var.

By using a known standard, anyone wanting to use your key/value pairs for some other purpose could easily do so, just like you hack the KM data.

I'm not saying you shouldn't do it or use it, just trying to understand the advantage of doing so.

Trying to keep it small and quick. There's a lot of overhead with plists, including having to run scripts, reading and writing the entire thing from/to disk, etc. This method is pretty quick, because all it is is a search and/or replace in a KM variable.

By the way, be the devil's advocate any time you want. I love it, and I often play that role. If our logic can't stand up to a little scrutiny, then there's something wrong with it, right?

That's always good. But remember, "better is the enemy of good enough" LOL
I suspect for relatively small lists, the difference in processing time is not material.

No need to write to disk. Just save the plist in a KM variable.

Don't we already have JXA/JS functions to get/set a key in plist?

Thanks. Please do the same for me. I always welcome constructive criticism.

Yes, but there's still overhead. Shut up. Leave me alone. (Sticks head in the sand)

:slight_smile:

I have had on the list for a very long time the idea of doing a hash table/dictionary kind of thing in Keyboard Maestro, which is along these lines. Figuring out exactly how it would look was always the problem. Your solution of parsing the parameter is quite clever, though I don’t think it would work for doing it natively in Keyboard Maestro that way.

I agree completely.

But honestly, I'm not sure how many people would use it, no matter how you designed it. Probably not worth the effort (just my opinion).

OK, I'll leave you alone to sort out your demons. LOL

1 Like

Good point. IMO, storing the scroll position should be native. Without it editing large macros can be really annoying.

1 Like

@DanThomas, just put me down for slow. :wink:
Two years later, and I totally get the value of your cool key/value system for KM.
Even though KM now has a native Dictionary, I have to say I'm not a fan.
But I do like your approach. . .

I was searching for something else and came across this old thread, and it just hit me that I have been effectively doing the same thing with my KM variable prefixes, but not nearly as neat you you do.

Actually I could use that, if you happen to have already developed it.

LOL. You snooze, you lose. It's been too long. You're on your own!