Save and recall a selection of variables

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.

Thanks! Looking forward to the update.

1 Like

Oooh! That's the first hint I've seen as to what's coming in v9! So exciting! It appears that JSON is a typed data structure (I've never used JSON, but I've certainly used data records before.) I like data. This will help me.

I may be reading too much, but I'm going to make a wild guess that there will be actions that let us read and write JSON values into regular KM variables using a sequence of fields in a KM action, something like the Search Variable action's dynamically-created fields:

Instead of a regular expression, that field will instead be a multi-line JSON schema. And when a valid schema is placed into the field, a series of nested variables/fields will be dynamically created to correspond to each of the fields in the data structure. It would also be nice if KM handled the nested nature of a JSON structure by using dots between levels, like this:

Person.Address.CityName