The Execute a Swift Script action executes a specified Swift script, either from a file or text.
The results of the script can be:
Swift scripts can get (but not set) Keyboard Maestro Variables by using environment variables, for example:
Swift 2 (Xcode 7 and 8)
import Foundation print(NSProcessInfo.processInfo().environment["KMVAR_Variable_Name"]!)
Swift 3 (Xcode 8)
import Foundation print(ProcessInfo.processInfo.environment["KMVAR_Variable_Name"]!)
Swift scripts can get or set Keyboard Maestro Variables by using AppleScript, for example:
import Foundation let myString = "the Value" NSAppleScript(source: "tell application \"Keyboard Maestro Engine\" to setvariable \"fromSwift\" to \"\(myString)\"")? .executeAndReturnError(nil)
Note that if the value might contain backslashes or quotes, you will need to deal with quoting them, replacing every backslash with backslash-backslash, and every double quote with backslash-double quote.