What am I doing wrong? Feels like this should work, but it's not

You have to pass local and instance variables to AppleScript in a particular manner; see the details here:

https://wiki.keyboardmaestro.com/AppleScript

Basically, you have to make AppleScript variables out of KM variables, and vice-versa. For local and instance variables, you also need to do the kmInst stuff. Please ask if you have additional questions.

-rob.

I like coming up with alternative solutions, even if they aren't always better. Since his duration value appears to be a small integer value, he would also create a Switch statement with varying hours in each Switch clause, without having to resort to variables at all.

Hmmm that's a little confusing, since KM could just add in the variables before runtime.

How would you amend the AppleScript to work then? I can then walk backwards to figure it out (unless it would take you some time to run that through)

It can't -- check that "text entry box" carefully and you'll see there's no T icon in the top-right corner. It isn't a KM "Text Field" and so doesn't process text tokens.

As a "Script Field" it must "access variables through other means" (to quote the Wiki).

I've got this, saved as a Favourite action, giving me boilerplate to pass variables in to and out of the AppleScript:

Execute an AppleScript (fav).kmactions (1.0 KB)

Execute an AppleScript (fav)

1 Like

Something lke this:

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set theDuration to getvariable "Local__duration" instance kmInst
end tell

tell application "Amphetamine"
	start new session with options {duration:theDuration, interval:hours, displaySleepAllowed:true}
end tell

I installed Amphetamine, and the above script worked fine with the Prompt for User Input you have above.

-rob.

1 Like