[Bug] Keyboard Maestro Swift Scripts Read Null When Trying To Get Variable Value

Keyboard Maestro Swift on XCode macOS Big Sur 11.4.

When trying to get a variable in Swift via ProcessInfo.processInfo.environment["KMVAR_Variable_Name"]!

The returned value is nil.

This worked for me:

image

... but I've never used Swift, so I'm just making a wild guess what the issue is.

I just followed the advice on the KM wiki:

https://wiki.keyboardmaestro.com/action/Execute_a_Swift_Script

It’s a compiled script, but the variables don’t produce a result in playground either. Maybe only inline text scripts work?

Hey Joe,

Correct.

Keyboard Maestro shell variables are ONLY available in a script run from the KM environment – e.g. a text script or a script file.

You can access Keyboard Maestro Global variables from anywhere by using AppleScript.

tell application "Keyboard Maestro Engine"
   set asVarName to getvariable "KM_Var_Name" instance kmInstance
   setvariable "KM_Var_Name" to asVarName
end tell

-Chris

Odd thing is though, this used to work, with both external swift scripts and compiled swift binaries. It also worked in Playground.

Reading variables from within the code is essential in my opinion. Not just being able to set them via AppleScript.