Get AppleScript to trigger a KBM key, set a AS variable and a KBM variable

I’d like to be able to trigger a KBM key from AppleScript.
Also be able to set an AppleScript var from KBM
And lastly be able to set a KBM var from AppleScript
any help would be appreciated.
The equivalent Quickeys code is below.

|| Play QK –
tell Application "quickeys"
play Shortcut Named "xxx"
end Tell
|| set AS Var from QKs –
tell application "QuicKeys"
set ASVariable to the value of variable named "V01"
end tell
|| set QK Var from AS–
tell application "QuicKeys"
set value of the variable named “xxx” to VAptCostPreTax
end tell

In each macro, the code to execute from AppleScript is available from the “Or by Script” trigger option.

You can’t set an AppleScript variable from outside of the AppleScript. An AppleScript can read or write a Keyboard Maestro variable as described in the Variables section on the wiki.

You can "type" keys from AppleScript using keystroke and key code, but to trigger a KM macro, it would be best to trigger using the Macro UUID, like this:

As @peternlewis suggested, get the AppleScript from the "Or by Script" trigger option. Just click on it and you will see:

The UUID is used in the first example code provided.

You can NOT set an AppleScript variable from KM, but it is very easy to set an AppleScript variable to a KM Variable in a script:

tell application "Keyboard Maestro Engine" to set yourASName to getvariable "yourKMVarName"

Setting a KM variariable is just as easy:

tell application "Keyboard Maestro Engine"
   -- IF Variable does NOT exist, it will be created --
   setvariable "yourKMVarName" to yourASVarName
end tell

Of course, you put both of these AppleScripts in a KM Action "Execute AppleScript".

Peter, the wiki is out of date, still shows the old pre-Ver 7.1 code.
I’ll try to update soon when I have a few minutes.

thank you,
I’m doing a fair bit of movement from Filemaker Pro over to Safari. So I need to run the AS from within FMP.
thanx

That should work fine. The AppleScript to execute a macro can be run from anywhere.

I have a “display test” in the global macro group.
It runs if I click on it on the global palette.
It runs from Script Editor (AS native app")
but it doesn’t run from within FMP, I made it a single line of code
In Filemaker Pro -
Perform Applescript
with the following

– ignoring application responses
tell application "Keyboard Maestro Engine"
do script “23D9A5D8-FB30-4032-8B18-A35D971AE4B7”
– or: do script “z AS Postings”
– or: do script “23D9A5D8-FB30-4032-8B18-A35D971AE4B7” with parameter "Whatever"
end tell
– end ignoring

any help is appreciated

filemaker pro 15,
and an AppleScript dialog displays correctly from within FMP,
so AS is running fine, and I can trigger keys in other apps.
run the KBM key and it chokes, spinning wheel and then ‘releases’, never running the key
thanx

Upon further investigation -
From FMP I am calling a macro that has an AppleScript in it running back to FMP.
This worked in quickeys.
It does not in KBM.

So is it true that you can’t run an AS from an app. that calls a KBM macro that runs another AS? - that seems weird, no?

It is hard to help you without more information.
Can you please:

  • Post all scripts and macros involved
  • Be more specific when you say “It does not in KBM”.
    • Exactly what is not working?
    • At what point/script/process does it stop working?
    • Post any error msg or unexpected results
  • Provide an example that works with Quickkeys, but not with KM. Be sure to include all input/source data needed, and the expected result/output.

The old code should still be correct - it is the AppleScripty way of doing things. But you’re right, it should include the newer setvariable/getvariable method because, despite (or because) not being AppleScripy, it is much easier to use.

I am surprised it would work in Quickeys.

Basically, if FMP is locked up executing your AppleScript, then it will not be available to respond to your AppleScript. This is one of the reasons that Keyboard Maestro executes AppleScripts via osascript.

After running an AppleScript (with Parameter) from within Filemaker Pro to run a KM macro.
How do I extract the parameter on the KM side?

Use the TriggerValue token.

ah, thanx