Browse the docs

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 #

ParameterTypeRequiredValuesWhat it does
scripttextYesRequired. Swift source. Print what you want back with print(...).
intotextThe variable that receives the printed text.
sampleany valueWhat into holds during a dry run.
timeoutnumberSeconds before the step is cancelled.

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

Print the number of CPU cores (runs Swift)
Do
  1. Run Swift script “import Foundation”
  2. Show “{{cores}} cores”
Open in Switchboard to try it

Gotchas #

  • Needs the Xcode command line tools. /usr/bin/swift is 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: into gets sample or 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.

See also #

Run shell script, Run Python script