Save and recall a selection of variables

I’ve created a set of variables that inform the way a group of macros I’ve created behave. Different projects I work on require different methods for capturing and documenting that data. For example, a note taking macro I’ve created generates a take number for each set of instruments being recorded in a scoring session along with other data that is entered on a google spreadsheet. Some projects prefer take numbers to be unique and continuously incrementing for every take recorded, while others like for each new piece of music or new instrument(s) begin recorded to start back with take number 1. This is just one of many parameters that change from project to project. I’d like to be able to save and recall variables associated with a project, as I’m often switching between projects that are stretched out over a period of time.

Any thoughts or advice would be appreciated.

Thanks,
K

Hey Kevin,

You should provide a couple of examples of what you’re trying to store.

There are all kinds of ways to save a retrieve data – here’s one:

Passing complex data between KM actions using JSON

You can find/replace in a simple structured text file.

You can save an AppleScript object.

You can use the defaults system.

You can create your own SQLite database and then save and retrieve data to and from it.

A very simple AppleScript example.

Change “Ren_” in the example to something common in your Keyboard Maestro variables.

Run the example in the Script Editor.app.

tell application "Keyboard Maestro Engine"
  tell (variables whose name starts with "Ren_")
    set {varNameList, varDataList} to {name, value}
  end tell
end tell

Then examine the Result panel in the Script Editor.

You’ll see something resembling this:

{{“Ren__Find”, “Ren__Repl”}, {“PopChar”, “PopChar X”}}

That’s two lists – one is the variable name – one is the variable content.

Something like this can be stored and retrieved quite easily.

-Chris

1 Like

This is great Chris. I’ll see if this solution will work for me.

Thanks!

Hey Kevin,

Something you can do is give your project-oriented variables a common prefix or suffix and then collate them like this:

tell application "Keyboard Maestro Engine"
  set varList to variables whose name starts with "projX"
end tell

Other verbs are contains and ends.

-Chris

Instead of projX, can I just put the list of variable names? I’d rather not have to change the name of all the variables I’ve created.

Hey Kevin,

Sure.

set variableNameSet to items 1 thru -2 of {¬
  "ENV_PATH", ¬
  "makeFoldersDest", ¬
  "makeFoldersSrc", ¬
  "Ren__Find", ¬
  "Ren__Repl", ¬
  "ReplyUsingRulesOutput", ¬
  "X", ¬
  "Y", ¬
  ""}

set variableDataSet to {}

tell application "Keyboard Maestro Engine"
  repeat with varName in variableNameSet
    set end of variableDataSet to getvariable varName
  end repeat
end tell

variableDataSet

An AppleScript list is usually a comma-delimited list of items contained in curly-brackets like this:.

set theList to {"one", "two", "three", "four"}

In the script above I've used vertical notation, because it's easier to read and maintain.

* Note that I'm skipping the last item (""). It's just there as a placeholder, so I can see the last actual list item ("Y").

-Chris

I entered in my variable names that I wanted to save and this is what I got.

This worked great although I noticed that the name of the variable is not saved in the result. How would I then save and recall these values to their respective variables?

Thanks!

Kevin, I'm not sure I fully understand your desired workflow, but it sounds like you need both logic an data. Chris has given you some great options for storing the variable data outside of a macro.

Here is an alternate method that used the KM Switch Action, to set the data according to the project. Maybe it will give you some ideas. It is just an EXAMPLE, not intended to be a complete solution.

##Macro Library Project Based Data [EXAMPLE]

Project Based Data [EXAMPLE].kmmacros (7.7 KB)

###Result:

Thanks guys! I really appreciate your help.

K

Hey Kevin,

variableDataSet is just a variable that you're looking at in the Script Editor. It is not the final result of a script that saves a data-set.

You already have your list of variable names in variableNameSet.

So, one way to save those items is using a plist file like we're discussing in this thread:

Saving Keyboard Maestro variables in a Plist file

Another way is to use a simple script-object:

---------------------------------------------------------------------------------
# Data Storage with a simple script object.
---------------------------------------------------------------------------------
set savedPrefsFolder to alias ((path to home folder as text) & "test_directory:Saved_Preference_Files:")
---------------------------------------------------------------------------------
script saveDataScriptObject
  property savedVariableNameSet : {}
  property savedVariableDataSet : {}
end script
---------------------------------------------------------------------------------
set variableNameSet to items 1 thru -2 of {¬
  "ENV_PATH", ¬
  "makeFoldersDest", ¬
  "makeFoldersSrc", ¬
  "Ren__Find", ¬
  "Ren__Repl", ¬
  "ReplyUsingRulesOutput", ¬
  "X", ¬
  "Y", ¬
  ""}

set variableDataSet to {}

tell application "Keyboard Maestro Engine"
  repeat with varName in variableNameSet
    set end of variableDataSet to getvariable varName
  end repeat
end tell

set saveDataScriptObject's savedVariableNameSet to variableNameSet
set saveDataScriptObject's savedVariableDataSet to variableDataSet

store script saveDataScriptObject in ((savedPrefsFolder as text) & "savedPrefs01.scpt") with replacing
-------------------------------------------------------------------------------------------

And this is how we get the data back.

---------------------------------------------------------------------------------
# Retrieval of Keyboard Maestro variable names and data from saved prefs file.
---------------------------------------------------------------------------------
set myPrefs to load script alias ((path to home folder as text) & "test_directory:Saved_Preference_Files:savedPrefs01.scpt")

tell myPrefs
  set variableNameSet to its savedVariableNameSet
  set variableDataSet to its savedVariableDataSet
end tell

---------------------------------------------------------------------------------
# Restoration in Keyboard Maestro
---------------------------------------------------------------------------------
tell application "Keyboard Maestro Engine"
  repeat with i from 1 to (length of variableNameSet)
    setvariable (item i of variableNameSet) to (item i of variableDataSet)
  end repeat
end tell
---------------------------------------------------------------------------------

This method is very fast.

-Chris

3 Likes

This is great! Works perfectly, and blazingly fast!

Thanks!
-K

Any way to make this into a plugin Action?

For anyone that might be interested, I’m working on a script and macro(s) that provide a general plist set/save of KM variables. I’ve completed proof-of-concept testing, and it looks good. I just need to add the finishing touches, and a good UI.

Should be ready in a day or two.

Once completed, maybe one of your KM plugin wizards can convert it to a plugin, if that makes sense.

1 Like

Very interested! The solution that Chris designed works very well, but I’m sure there’s functionality that could be added to improve it, such as a prompt that asks where you would like to save the plist and what you would lie to name it. The recalling from plist could also have a prompt to select plist to recall from.

K

Hey Dan,

I don't see any obstacles, but I haven't written any plugins either.

The handy thing about this method is that saved items are native AppleScript objects and don't have to be translated to be used.

-Chris

Seriously? LOL. To someone with your skill set, you'd find it trivial, trust me. Take any plugin that has been posted on this forum, download the zip file, and look at its contents. Open the plist file in a text editor. You'll see what I mean.

Yeah, as has become apparent in the other thread, I clearly don't know how to "use" them. Sigh. And other stronger expletives.

Wondering what came of that macro you were working on for plist set/save of KM variables. Still very interested.

Thanks,
K

Thanks for the reminder.

I'm reviewing my script now (have not looked at it for over a year). If I don't respond back here within a few days, feel free to ping me again.

ping :slight_smile:

Any news on this script?

Sorry for the delay -- I completely forgot about this script.

IAC, KM9 is about to be released, which will provide a lot of Actions etc for managing JSON strings. JSON is a much better format than plists.

So, I've set a reminder to review/revise this script to use JSON as soon as KM9 is released.