How to Save a Selection of Variables Like Presets

Hey Folks,

I think that ultimately JSON may be the easiest medium to manage and manipulate, because JavaScript has so many tools for managing it – but you’ve got to get over the learning-curb first.

In the meantime we use the tools we know…

This AppleScript will save all Keyboard Maestro variable name-value pairs to a plist on the Desktop.

It is of course quite easy to filter out specific variables.

---------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/06/01 00:35
# dMod: 2016/06/01 00:49
# Appl: Keyboard Maestro, System Events
# Task: Store Keyboard Maestro variables' names and values in a plist file on the Desktop.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @System_Events, @Store, @Variables, @Plist
---------------------------------------------------------------------------------

tell application "Keyboard Maestro Engine"
  tell variables
    set {kmVarNameList, kmVarValueList} to {name, value}
  end tell
end tell

tell application "System Events"
  tell (make new property list file with properties {name:"~/Desktop/TEST.plist"})
    repeat with i from 1 to length of kmVarNameList
      set kmVarName to item i of kmVarNameList
      set kmVarValue to item i of kmVarValueList
      make new property list item with properties {name:kmVarName, value:kmVarValue} at the end of property list items
    end repeat
  end tell
end tell

---------------------------------------------------------------------------------

Retrieval is something like this:

---------------------------------------------------------------------------------
set plistFile to "~/Desktop/TEST.plist"

tell application "System Events"
  tell property list file plistFile
    
    set plistItemList to property list items
    set plistItemNameList to name of property list items
    set plistItemValueList to value of property list items
    
    set filteredPlistItemList to property list items whose name contains "test"
    
    set testVarValue to value of property list item "testVar"
    
  end tell
end tell
---------------------------------------------------------------------------------

-Chris

2 Likes