Get a KM variable in Applescript in Keyboard Maestro 6?

Hi there,
I am trying to get a Keyboard Maestro variable into Apple Script while using Keyboard Maestro 6. I have of course come across the Wiki article Using AppleScript to Get and Set Keyboard Maestro Variables, but this article only shows methods for KM 7.1+ and KM 7.0.2+

My question: is it at all possible to pass a KM variable to AppleScript in Keyboard Maestro 6?

Thank you!
trych

Although it states this script requires KM 7.0.2+, I believe it will work with KM 6.
I don't have a copy of KM 6 to these, so could you please test and let us know if it works:

Get Existing Variable (KM Wiki)

I just tested this and it returns the following error message (where name would be the name of my variable in KM):

/var/folders/f6/rp4n1n6j56lf847cvp6_wfdw0000gn/T/Keyboard-Maestro-Script-48E0C2BA-9095-4AD4-967D-20244C3075C9:232:255: execution error: Keyboard Maestro Engine got an error: variable "name" doesn’t understand the exists message. (-1708)

Any way to work around this?

Aha! The basic principle seems to work though. I think just the exists keyword itself is causing trouble with KM 6, if I skip the whole if statement and do something like this instead

tell application "Keyboard Maestro Engine"
    set myASVar to value of variable "name"
    return myASVar
end tell

then it works. Perfect. Thanks for pointing me in the right direction Michael, I consider this solved.

trych

Yep. If you want to mimic KM 7 behavior, you can use this:

EDIT: 2017-06-04 5:50 PM CT

Refactor into an AppleScript handler:

###Get KM Variable for KM Ver 6+


set myKMVar to my getKMVar("MY_KM_Variable")

on getKMVar(pKMVarNameStr)
  tell application "Keyboard Maestro Engine"
    try
      set kmVar to value of variable pKMVarNameStr
      
    on error errMsg number errNum
      if (errNum = -1728) then
        --- KM Variable NOT Found ---
        set kmVar to ""
      else
        error errMsg
      end if
    end try
  end tell
  
  return kmVar
  
end getKMVar
1 Like

Got it. Thanks again, Michael!

@trych, I just updated my above post to make the script easier to use, as a AppleScript handler (function).

@peternlewis, @ccstone, et al:

I have updated the KM Wiki to reflect this:
AppleScript [Keyboard Maestro Wiki]

The last one is titled “Get Existing Variable”. I think it was designed to be simple code in the case where you know the variable exists.

The second last script uses “exists” which apparently does not work in the v6 AppleScript. My memory does not stretch back that far, but I thought you just do make variable "Whatever" to create a new variable and it doesn’t overwrite the value if it exists. But maybe it does.

Should we change it to just "Get Variable"?

Maybe I should write a on setKMVar() handler like this one that mimics the KM 7 setvariable behavior?

Probably it needs both options. You often know the variable exists already (eg the macro sets a variable and then executes the AppleScript) and so it takes only a single line.

Someone with KM6 installed needs to see what the results of:

set kmv to make new variable with properties {name:"Variable"}
value of kmv

returns if the variable exists or does not exist.

As far as I can see (testing only KM8d) that will return the value of the variable or “” if the variable does not exist.

Thank you Peter and Michael.

Although my problem has been solved I would like to take the opportunity to let you know that I am very impressed how users of an older version of KM are treated here. From other software support forums, often with much more costly software I usually hear responses like this: “OMG, you’re still with 10.8.5, why don’t you update already!? WHAT, you’re not using the latest version of our software, we can’t help you at all, if you’re not with the latest version. It just doesn’t work with the old version, you HAVE to update!” etc.

There seems a complete lack of understanding (in these other forums) that some users need to work in an older environment, because they have a certain constellation of software they depend on. And that is not because I’m a noob, but rather the opposite, because I use some specialized professional software that does not necessarily run in the newest OS.

So again, thanks for being so really patient and taking the time to help out a user who is new here and does not even use the latest version of KM (not on my main machine anways).

trych

3 Likes