action:Execute_a_Swift_Script
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| action:Execute_a_Swift_Script [2016/09/13 18:36] – Corrected code examples per @Tom; ADDed sections, See Also JMichaelTX | action:Execute_a_Swift_Script [2023/09/13 03:03] (current) – [See Also] peternlewis | ||
|---|---|---|---|
| Line 17: | Line 17: | ||
| ===== How to Get Keyboard Maestro Variables ===== | ===== How to Get Keyboard Maestro Variables ===== | ||
| + | |||
| + | Keyboard Maestro sets the environment variables for the script to include all your variables, using a prefix of `KMVAR_` and your variable name with spaces changed in to underscores (‗). For example, your Keyboard Maestro “File Name” variable will be available as the environment variable `KMVAR_File_Name`. | ||
| + | |||
| + | By default, all variables are included, but you can select No Variables, or specific variables as desired using the popup menu next to the script (v11.0+). | ||
| Swift scripts can get (but not set) Keyboard Maestro Variables by using environment variables, for example: | Swift scripts can get (but not set) Keyboard Maestro Variables by using environment variables, for example: | ||
| Line 22: | Line 26: | ||
| **Swift 2 (Xcode 7 and 8)** | **Swift 2 (Xcode 7 and 8)** | ||
| - | < | + | < |
| import Foundation | import Foundation | ||
| print(NSProcessInfo.processInfo().environment[" | print(NSProcessInfo.processInfo().environment[" | ||
| </ | </ | ||
| - | **Swift 3 (currently beta, Xcode 8)** | + | **Swift 3 (Xcode 8)** |
| - | < | + | < |
| import Foundation | import Foundation | ||
| print(ProcessInfo.processInfo.environment[" | print(ProcessInfo.processInfo.environment[" | ||
| </ | </ | ||
| + | Swift scripts can get or set Keyboard Maestro Variables by using AppleScript, | ||
| - | ---- | + | <code swift> |
| + | import Foundation | ||
| + | |||
| + | let myString = "the Value" | ||
| + | NSAppleScript(source: | ||
| + | </ | ||
| + | |||
| + | 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. | ||
| ===== See Also ===== | ===== See Also ===== | ||
| + | |||
| + | === Actions === | ||
| + | |||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[: | ||
| ==== Related Wiki Articles ==== | ==== Related Wiki Articles ==== | ||
action/Execute_a_Swift_Script.1473791791.txt.gz · Last modified: by JMichaelTX
