User Tools

Site Tools


action:Execute_a_Swift_Script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
action:Execute_a_Swift_Script [2015/07/15 03:05]
peternlewis created
action:Execute_a_Swift_Script [2016/09/13 22:08] (current)
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 ​by using environment variables, for example:+===== How to Get Keyboard Maestro Variables ===== 
 + 
 +Swift scripts can get (but not set) Keyboard Maestro Variables ​by using environment variables, for example: 
 + 
 +**Swift 2 (Xcode 7 and 8)** 
 + 
 +<code swift> 
 +import Foundation 
 +print(NSProcessInfo.processInfo().environment["​KMVAR_Variable_Name"​]!) 
 +</​code>​ 
 + 
 +**Swift 3 (Xcode 8)** 
 + 
 +<code swift> 
 +import Foundation 
 +print(ProcessInfo.processInfo.environment["​KMVAR_Variable_Name"​]!) 
 +</​code>​ 
 + 
 +Swift scripts can get or set Keyboard Maestro Variables by using AppleScript,​ for example: 
 + 
 +<code swift> 
 +import Foundation 
 + 
 +let myString = "the Value"​ 
 +NSAppleScript(source:​ "tell application \"​Keyboard Maestro Engine\"​ to setvariable \"​fromSwift\"​ to \"​\(myString)\""​)?​.executeAndReturnError(nil) 
 +</​code>​ 
 + 
 +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 ===== 
 + 
 +==== Related Wiki Articles ==== 
 + 
 +* [[:​Scripting|Scripting on OS X Using Keyboard Maestro]] 
 + 
 +==== Forum Topics ==== 
 + 
 +* [MACRO Example: Swift Script returning list of variable names from KME](https://​forum.keyboardmaestro.com/​t/​macro-example-swift-script-returning-list-of-variable-names-from-kme/​4052) 
 +* [Swift example script: Get macros and uuids from clipboard](https://​forum.keyboardmaestro.com/​t/​swift-example-script-get-macros-and-uuids-from-clipboard/​4037) 
 +* [Keyboard Maestro supports Swift](https://​forum.keyboardmaestro.com/​t/​keyboard-maestro-supports-swift/​4015)
  
-    import Foundation 
-    println( NSProcessInfo.processInfo().environment["​KMVAR_Variable_Name"​]! ) 
  
  
action/Execute_a_Swift_Script.1436943937.txt.gz · Last modified: 2015/07/15 03:05 by peternlewis