Triggering KM macro from AppleScript with variable

I've been triggering KM Macros via Alfred using simple AppleScript workflows, and that has been working fine.

I am trying to pass a variable to the KM Macro via the AppleScript, but it does not come through.

In Alfred, my AppleScript is:

on alfred_script(q)
-- ignoring application responses
set daycountDisplayType to "large"
tell application "Keyboard Maestro Engine"
	do script "E736AF24-E3D1-40BC-A365-6B48D44F1723" with parameter daycountDisplayType
end tell
-- end ignoring
end alfred_script

in my macro, I'm starting by saving the trigger value to a variable, and for debugging purposes, showing that variable as text:

Instead of displaying the word "large", it's showing:

Is there something obvious I'm doing wrong?

BTW, originally, I tried having the Alfred AppleScript set a variable in KM before triggering the macro script, but that did not work either. :frowning:

thanks!

1 Like

You are using the wrong token for %TriggerValue%

image
without the "%Variable"

omg, thank you! I didn't know it was that easy.

My mistake was I added it by using the "Insert Token" menu:

Yes, it's working fine now. BTW, just so I can learn, do you understand what the Variable TriggerValue is, compared to TriggerValue without the variable prefix? I don't quite get it.

The Variable "TriggerValue" is a variable the you have created somewhere.
I find it easier and faster to use the Insert Token by Name. Try it. When the popup menu appears, just type in "trigger".

By the way I’m wondering why you’re using a UUID instead of a macro name. Is this to avoid ambiguity? If so, that’s understandable, but it’s at the expense of clarity.

There are two reasons to use UUID:

  1. Ensures you access the correct Macro, since Macro names do not have to be unique.
  2. Allows you to change/tweak the Macro Name and not break the trigger.
1 Like

Weird, I just searched all my macros and I don't see it. So strange!

I just tried it. works like a charm. I guess I don't really understand the difference between a token and a variable. I know a variable can not be a token, but I thought all tokens were by definition variables!

Martin - Yes, I have a bad habit of renaming macros down the line and forgetting that I have then broken links getting to them!

1 Like

Just to be clear for all readers, somehow you had to have used a Variable with the name of "TriggerValue" in order for it to show up in the Variable list. Maybe it came from an imported Macro.

IAC, here is a screenshot of the "Insert Variable by Name" tool in my KM account, which shows no such variable:

image

Note that all of the Variables that start with "Local__Trigger" are variables that I created to explore/document the various Trigger tokens.

There is often confusion between KM Tokens and KM Variables.
Please see

TIP: Understanding KM Variables, Tokens, & Functions

The bogus TriggerValue variable either exists somewhere in your macros, or alternatively has a value.

Check the Keyboard Maestro Preferences, Variables, and remove any value from it, and search your macros (All Macros smart group or press Command-F twice) for TriggerValue and look for a macro that is using a variable named TriggerValue.

FWIW, I didn't have TriggerValue in my list of Variables, under preferences. I did have the variable "vorut_Trigger_Value" from this macro I imported a few years ago:
MACRO or PLUGIN: Open or Refresh a URL in a Tab (Safari & Chrome)
which has actually not worked right for a few years, so I can delete it anyway.

either way, the real push is for me to read the documentation on the difference between tokens and variables!

To avoid this type of problem, I prefix all of my Global Variables with "DND_" that I want to keep and "Do Not Delete". I then periodically delete all KM Variables except those with the "DND" prefix.

See

Macro: Clean-up DELETE KM Variables @KM8

Better still is to always use Local or Instance Variables, which are auto-deleted when the Macro terminates, unless it is absolutely necessary to use a Global Variable.