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.
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 , 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.
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:
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.
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.
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.
@DanThomas, just put me down for slow.
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.