[SOLVED] New user having trouble using variables in AppleScript

Hello,

I'm very new to Keyboard Maestro, having only been using it for a couple of days. I've been able to find information on almost everything that I've tried to do so far, but there are a couple of issues that I feel like should be really simple but I've so far been unable to work out on my own.

Using variables in AppleScript is one of them. This works, for example:

But the following in an "Execute AppleScript" action does not:

tell application "Keyboard Maestro Engine"
	getvariable timePlus25
end tell

It just presents an error saying that the variable is not defined. I've clicked the down arrow next to the text box where the script is entered and tried selecting the "timePlus25" variable in the menu that appears. I've also tried selecting "Include All Variables" and also "Include No Variables". Always with the same result.

What am I missing to be able to use KB Maestro variables in AppleScript?

Sorry for such a simple question and thank you in advance!

You need to set an AppleScript variable to use the variable you're getting:

tell application "Keyboard Maestro Engine"
	set myTimeVar to getvariable "timePlus25"
end tell

Then you can use myTimeVar in the AppleScript as you need. And you need quotes around the KM variable.

-rob.

Hi @sfam, and welcome to the forum!

If you want to learn more about what's going on in @griffman's answer, check out the wiki page for the Execute an AppleScript action:
https://wiki.keyboardmaestro.com/action/Execute_an_AppleScript

It may also be helpful to know that right-clicking on any action in the Keyboard Maestro Editor and choosing "Help" at the bottom will take you directly to the wiki page for that action.

2 Likes

A very good link I forgot to add. I reference those pages so often I made a little macro to pop them up in a temporary window (or open their "real" web pages):

-rob.

2 Likes

D'oh! I knew it had to be something silly that I was just overlooking.

I'll definitely check out that macro of yours also, @griffman. Seems like something I'd get a lot of use out of. Hah!

Thank you both very much!

1 Like