action:Execute_a_Swift_Script
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| action:Execute_a_Swift_Script [2015/07/15 07:05] – created peternlewis | action:Execute_a_Swift_Script [2023/09/13 03:03] (current) – [See Also] peternlewis | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Execute Swift Script ====== | ||
| + | |||
| The //Execute a Swift Script action// executes a specified Swift script, either from a file or text. | The //Execute a Swift Script action// executes a specified Swift script, either from a file or text. | ||
| + | |||
| + | ===== Using the Results ===== | ||
| The results of the script can be: | The results of the script can be: | ||
| Line 12: | Line 16: | ||
| *Asynchronously ignored — the action runs while the macro continues on. | *Asynchronously ignored — the action runs while the macro continues on. | ||
| - | Swift scripts can access 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 | ||
| + | |||
| + | **Swift 2 (Xcode 7 and 8)** | ||
| + | |||
| + | <code swift> | ||
| + | import Foundation | ||
| + | print(NSProcessInfo.processInfo().environment[" | ||
| + | </ | ||
| + | |||
| + | **Swift 3 (Xcode 8)** | ||
| + | |||
| + | <code swift> | ||
| + | import Foundation | ||
| + | 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, | ||
| + | |||
| + | |||
| + | ===== See Also ===== | ||
| + | |||
| + | === Actions === | ||
| + | |||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[: | ||
| + | |||
| + | ==== Related Wiki Articles ==== | ||
| + | |||
| + | * [[: | ||
| + | |||
| + | ==== Forum Topics ==== | ||
| + | |||
| + | * [MACRO Example: Swift Script returning list of variable names from KME](https:// | ||
| + | * [Swift example script: Get macros and uuids from clipboard](https:// | ||
| + | * [Keyboard Maestro supports Swift](https:// | ||
| - | import Foundation | ||
| - | println( NSProcessInfo.processInfo().environment[" | ||
action/Execute_a_Swift_Script.1436943937.txt.gz · Last modified: by peternlewis
