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.
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.
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").
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?
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.
---------------------------------------------------------------------------------
# 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
---------------------------------------------------------------------------------
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.
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.
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.