KM Variables -- Storing a batch of them for later reloading and reuse

Would appreciate some general advice about storing a batch of KM Variables for later reloading and reuse.

In a KM macro I have a FOR...EACH loop for i=1 to 100
In each loop it loads e.g. 8-12 variables that exist out of a possible 20. [ e.g. grab all 8 filenames from Folder1. e.g. grab all 11 filenames from Folder5 etc. ]
Then it does something with those variables.
Then it moves onto the next loop and resets those same variables/reuses them etc.

But I want the option of this -- In another KM macro:

SET i = 73 manually (e.g.) then to RELOAD and REUSE the 8-12 variables relevant to the 73rd loop directly from KM or maybe a TXT file (e.g.).

In other words I am looking to store inside KM or a TXT file (or somewhere else??) an array of 100 x 20 variable values.

What's the best way to do this in KM?

At the moment the best way I can think of is to create a TXT file filled with variables & their values during each of the 100 loops. 100 TXT files. Then inside each TXT file have something like:

Var1; VarValue1
Var2; VarValue2
Var3; VarValue3 etc......

Then reload them by using regex.

This doesn't seem like a very "clean" solution though? Am I missing a better way to do this in KM? Can KM Dictionaries help here? Thanks.

We really need to know much more about your total workflow to provide good suggestions. Having said that, what comes to my mind first is to use a SQL database, like SQLite, which is built in to the macOS.

I would not use KM dictionaries.

But then I'm an old database guy, who knows the SQL (Structured Query Language) pretty well.
And AppleScript guru @ShaneStanley has released a script library for managing SQLite databases, which makes it very easy to use with AppleScript.

OTOH, 100 rows of data is not much, and you could easily write/read an entire tab-delimited text file very fast. I would probably use JavaScript for Automation (JXA) because JavaScript offers such great array handling tools built into the language.

But I'm just throwing out ideas. I would need more details to actually suggest or design a solution.

So, it's up to you. Take these ideas (and others that may be offered) and determine what will work best for yourself. Or, post a lot more detail, and we will try to offer better, more specific solutions. I would offer one more thing: the same detail I'm asking for here you also need yourself to do a decent design.

Thanks for this feedback JMichaelTX, definitely food for thought (and research).

I'd welcome others opinions as well.

Would an array of arrays of variables in KM be the solution? Is such a new KM feature likely in the future or is that outside the scope of KM? Thanks.

KM does not have true arrays. A KM Variable that has comma separated values can be addressed like an array:

Given SomeVarName that is:

value1,value2,value3

can be read (but not set):
SomeVarName[2]
will return "value2"

While the Variable may contain multiple lines, there is no way of directly addressing the data in other than the first line. To do so requires use of the For Each Action.

So, as soon as it starts to get complex, I would want to jump to JavaScript arrays/objects.

That looks quite promising. So I could build just 1 TXT file (rather than 100) and then get to the line 73 set of variables by using the FOR...EACH action. Then accessing the loop#73 variables as %SomeVarName%[n] ...

I would not use For Each Action just to get the nth line.
See:

MACRO: Get Item from List by Position [Example]

In the JXA script, there are options for getting the list from a KM Variable or from a file.

1 Like

Hey Steve,

Sed was born for this sort of thing and will happily take a string variable or a file as input.

-Chris


Sed ⇢ Extract Data-Record from List by Line Number v1.01.kmmacros (5.8 KB)

1 Like