Pass Applescript Array into KM Variable?

Hi, I am trying to pass a result from Applescript (Array) into KM. I would then like to use the contents of the result in the Variable for an ‘if variable contains’ action, if variable contains ‘xyz’ then perform action etc…

I get an error message every time I try to convert the below result into a variable. I need KM to obtain everything in the Array and save it within a single Variable if possible.

Result:

{{“Receive DSAR responce - is it for all products client has as stated on the contract? are you sure?”, “Receive completed DSAR for all products of client”, “URGENT THIS PROJECT SHOULD HAVE BEEN COMPLETELY COMPLETED BY NOW!”, “Ali to follow up if not completed”}}

Please assist, thanks

Ali

Keyboard Maestro variables are strings. They cannot hold an array (except as a string).

In AppleScript, try adding “as string” to the end of the result. That should turn it in to a string which you can then return to Keyboard Maestro to go into a variable.

But don't do that without setting AppleScript's text item delimiters first.

Run this in Script Editor.app to see what it looks like.

set myOneDimensionalTextArray to {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}
set AppleScript's text item delimiters to linefeed
return myOneDimensionalTextArray as text

You can save the result of the Execute an AppleScript action to a variable.

You can also set/get KM variables from within AppleScript.

Get KM variable with AppleScript:

---------------------------------------------------------------------
# Keyboard Maestro — Set value of variable "<variable name>"
---------------------------------------------------------------------
tell application "Keyboard Maestro Engine"
  set kmVariableName to "myVariableName"
  try
    set value of variable kmVariableName to myText
  on error
    make new variable with properties {name:kmVariableName, value:myText}
  end try
end tell
---------------------------------------------------------------------

Set KM variable with AppleScript:

---------------------------------------------------------------------
# Keyboard Maestro — Get value of variable "<variable name>"
---------------------------------------------------------------------
tell application "Keyboard Maestro Engine"
  
  set kmVariableName to "X"
  
  if length of (get variables whose name is kmVariableName) = 1 then
    set myValue to value of variable kmVariableName
  else
    error "Variable '" & kmVariableName & "' does not exist!"
  end if
  
end tell
---------------------------------------------------------------------

-Chris

1 Like

Hi Chris,

I have an issue along these same lines.

I have a bunch of variables that are either ON or OFF / 1 or 0. The user sets them to ON or OFF via user input & checkboxes. For certain tasks the variable states are always the same, that is for Task one, VariableA = 1, VariableB = 0, for Task Two, VariableA = 1, VariableB = 1. So instead of the user having to re-check boxes when certain tasks are the same I am putting together a way to have a list of different tasks and their corresponding Variable states.

I know that I can just create a different user input prompt with the different variable states checked by default, but this is a lot of extra work if / when I ‘add’ a new variable - I have to add it to EVERY user input prompt.

More importantly, I don’t always want a user input prompt. In some cases, the Tasks are basically the same and will have the same variables, and so those Tasks will prompt the user - and the user will see the default Task Variables checked and will be able to uncheck / check what they want. But in other cases, certain tasks will always have the same variable states and so I don’t want the user to have to check anything. In these cases I need a way to set the Variable States outside of the user interaction.

The goal is to have one “Master List” of the Variables in question that I can add to in the future - so that I only have 1 place to go to change things.

Right now I have something like this:

set myVariables to {"A", "B", "C", "D", "E"}

tell application "Keyboard Maestro Engine"
   repeat with i in myVariables
      try
         set value of variable i to 0
      on error
         make new variable with properties {name:i, value:0}
      end try
   end repeat
end tell

set defaultVariables to {"B", "C"}

if defaultVariables ≠ false then
   tell application "Keyboard Maestro Engine"
      repeat with i in defaultVariables
         set value of variable i to 1
      end repeat
   end tell
end if

The main problem is that I cannot access this “myVariables” array outside of THIS AppleScript code.

I tried using the AppleScript - Save to Variable and I was able to convert the array to a list, but then I have trouble getting the list back into AppleScript so that my AppleScript code works. I’m assuming it’s because the array is converted into a list and so it would need to be converted back to an array?

set myVariables to {"A", "B", "C", "D", "E"}
set AppleScript's text item delimiters to linefeed
return myVariables as text
Save to variable: MasterVariableList

Also, is there a way to have the User Input prompt take there ‘defaults’ from the current variable value at the time that the prompt comes up? That is, if VariableA is set to 1 / ON / Checked … and then a user Prompt is shown with VariableA, can the Prompt show VariableA’s ‘current’ value?

Much appreciated!

Joe

Hey Joe,

As of Keyboard Maestro 7.2 the getvariable and setvariable verbs are available and make it possible to write cleaner code:

---------------------------------------------
set myVariables to {"A", "B", "C", "D", "E"}

tell application "Keyboard Maestro Engine"
   repeat with i in myVariables
      setvariable i to 0
   end repeat
end tell

set defaultVariables to {"B", "C"}

if defaultVariables ≠ false then
   tell application "Keyboard Maestro Engine"
      repeat with i in defaultVariables
         setvariable i to 1
      end repeat
   end tell
end if
---------------------------------------------

Where do you want to put it?

There are all sorts of ways to manage a simple list:

-----------------------------------------------------------------------
set kmVarValue to "A,B,C,D"

tell application "Keyboard Maestro Engine"
   setvariable "defaultsList" to kmVarValue
end tell

set AppleScript's text item delimiters to ","

tell application "Keyboard Maestro Engine"
   set myAppleScriptList to text items of (getvariable "defaultsList")
end tell
-----------------------------------------------------------------------

You can use variables in the prompt:

https://wiki.keyboardmaestro.com/action/Prompt_for_User_Input

HTH.

-Chris

Joe, this is easy if you know AppleScript. As Chris has shown, we can do this:

(1) Start with a text file, which is CSV:

var1,1
var2,0
var3,0
etc

(2) Read that into a KM Variable, that I'll call Master_List
(3) Read the Master_List into AppleScript as a list of lists,
OR, read directly in AppleScript -- it just depends on what you need to do where.

{{"var1",1}, {"var2",0}, {"var3",0}}

Process this data however you want, and then output back to KM in the same format the original data was in.

(4) From either KM or AppleScript, we can easily output the Master_List back to the file, if that is what you want.

What I'm not clear on , is exactly what your process is.
Can you write out the process, step by step?
Then we'll know how to proceed.

If you don't know AppleScript very well, then it is best to minimize its use so you can easily maintain and change the macro. Using the "For Each", Search and Replace, and other KM actions there is much that can be done directly in KM.

The biggest challenge may be the Prompt for user input.
Can you please describe in more detail exactly what you want done with it?
Is it all variables, only variables = 0, only variables = 1, or something else?

Joe -

For the user input, see if this will help:

1 Like