Faster method of loading KM Variables to Applescript?


Is there a faster way of using less code to load KM Variables into Applescript? I am pulling out about 20/30 variables everytime I execute this Function. Then in the same applescript I run some other code.....using Fscript.

Hey Ali,

Since Keyboard Maestro 7 you can do this:

tell application "Keyboard Maestro Engine"
   # Set
   setvariable "kmVarName" to "whatever"
   # Get
   set myVar to getvariable "kmVarName"
end tell



Hi Chris,
Many thanks for this, I will try it out and let you know the difference in performance.

Could you provide one recipe example based upon my variables in the image so that I can fully understand how I would go about setting them. Thank you again for your understanding :).


Hey Ali,

Does this clarify things?

tell application "Keyboard Maestro Engine"
   setvariable "Address 3 Post Code" to "stub"
   set myAppleScriptVariable to getvariable "Address 3 Post Code"
end tell

Remember that all Keyboard Maestro variable names are text in AppleScript.

All Keyboard Maestro variable contents is also text, so you can confuse yourself if you’re not careful.

You can represent a Keyboard Maestro variable with an AppleScript variable, although I’ve seen that confuse users too.

set address3PostalCode to "Address 3 Post Code"
set address3PostalCodeVALUE to "stub"

tell application "Keyboard Maestro Engine"
   setvariable address3PostalCode to address3PostalCodeVALUE
   set myAppleScriptVariable to getvariable address3PostalCode
end tell


1 Like

Hi Chris,
Now I am super confused, I will look into it further to understand this. However pulling 2 KM variables into Applescript takes 0.2 seconds with your method how long does it take? trying to get a rough idea of how long it takes when I pull 20 variables into Applescript.
Thanks a lot

You can do the timing tests for yourself using this script:

###Example Results

(*START: Sun, Apr 9, 2017 at 4:43:04 PM*)
    β€’ Sun, Apr 9, 2017 at 4:43:04 PM
    β€’ EXECUTION TIME: 0.029 sec*)

###AppleScript Timer

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"


tell application "Keyboard Maestro Engine"
  --- Add as many "getvariable" statements as you like
  set Test1 to getvariable "Test1"
end tell

--repeat with i from 1 to 10
--  delay 0.123
--end repeat


--~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~

on timer(pAction)
  #      timer(pAction)    Calculate and Log Execution Time
  #      Ver 1.1    2016-02-21
  #      REF:  The base ASObjC code was provided by Shane Stanley
  #      HOW TO USE:
  #        β€’ You may want to run the script at least 3 times
  #        β€’ The first run may be high
  #        * For more extensive/exhaustive testing, see:
  #            Script Geek app by Shane Stanley
  #      REQUIRES:
  #        β€’ These two statements at top of main script:
  #             use scripting additions
  #             use framework "Foundation"
  #        β€’ Yosemite+
  #        β€’ function formatSeconds(totalSeconds)
  #          (provided below)
  global gTimerStartDate
  if (pAction = "start") then -- START CASE
    set gTimerStartDate to current application's NSDate's |date|()
    log "START: " & ((current date) as text)
    set durationNum to -(gTimerStartDate's timeIntervalSinceNow())
    --- IF β‰₯ 60 SEC, FORMAT AS HR MIN SEC ---
    if durationNum β‰₯ 60 then
      set durationStr to formatSeconds(durationNum)
      set durationStr to (round (durationNum) * 1000) / 1000.0 & " sec"
    end if -- durationNum β‰₯ 60
    log pAction & ":  
    β€’ " & ((current date) as text) & "
    β€’ EXECUTION TIME: " & durationStr
  end if -- (pAction = "start")
end timer

#      formatSeconds(totalSeconds)      Convert Seconds to HR MIN SEC format
#      Ver 1.1    2016-02-21
# REF:

on formatSeconds(totalSeconds)
  set theHours to (totalSeconds div hours)
  set theRemainderSeconds to (totalSeconds mod hours)
  set theMinutes to (theRemainderSeconds div minutes)
  set theRemainderSeconds to (theRemainderSeconds mod minutes)
  set theRemainderSeconds to (round (theRemainderSeconds * 100)) / 100.0
  # set theTimeString to theHours & ":" & theMinutes & ":" & theRemainderSeconds as text
  set theTimeString to theHours & " hr  " & theMinutes & " min  " & theRemainderSeconds & " sec" as text
  return theTimeString
end formatSeconds

How can I get the β€œreturn theTimeString” into a variable to display in AppleScript?
I can’t get it in the usual way. I’m not familiar with the β€˜on’ commands etc. etc and can’t simply perform a
display dialog theTimeString -
thanx in advance

This can be done, but . . .

Did you notice that ALL of the timer data is being output to the Script Editor log panel?

yes I did see it in the messages section of the Log panel, but would like to get it to a variable.

What is the purpose of this?

I’ve given you a tool to time a script, with the results neatly shown in the log panel. I don’t have time now to modify the timer script to return the results to a variable. Not that hard – I’m just very busy at the moment.

Maybe this is an opportunity for you to learn a bit of AppleScript? :wink:

1 Like

I know a bit of AppleScript as expressed in my initial reply to you, thank you.
I spent time and searched to see if I could modify the script to get the results. I could not.
If it was easy or I found it I would not be bothering you.
Simple request, no worries, appreciate your time.