Run Swift script
Runs Swift source with the swift interpreter and captures what it prints.
Run Swift script
Keyboard Maestro: ExecuteSwift
Details #
Sends script to /usr/bin/swift - on standard input. Whatever the program prints to standard output is the step's output, with trailing newlines removed, and goes into into if set. The output object has stdout, exit and, if any, stderr. A compile error or non-zero exit fails the step with the first 300 characters of standard error.
Parameters #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
script | text | Yes | Required. Swift source. Print what you want back with print(...). | |
into | text | The variable that receives the printed text. | ||
sample | any value | What into holds during a dry run. | ||
timeout | number | Seconds before the step is cancelled. |
Every step also accepts label, onFailure, timeout and dryRunSafe.
Examples #
- Run Swift script “import Foundation”
- Show “{{cores}} cores”
Gotchas #
Needs the Xcode command line tools.
/usr/bin/swiftis only a working interpreter once Xcode or the Command Line Tools are installed.Slow. Swift compiles the script on every run, often for several seconds. The default 10 s step limit is easy to hit, so set
timeout(30 to 60 is typical). For quick work prefer Run JavaScript or Run shell script.Dry run. Not run:
intogetssampleor an empty string.{{ }}values are pasted into the source before it compiles. Untrusted text can break out of a string literal.Do not put secrets in the script text: it is saved with the macro and shown in the trace.