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

Both sides previous revision Previous revision
Last revision Both sides next revision
action:Execute_a_Swift_Script [2016/09/13 14:36]
JMichaelTX Corrected code examples per @Tom; ADDed sections, See Also
action:Execute_a_Swift_Script [2016/09/13 22:08]
peternlewis
Line 22: Line 22:
 **Swift 2 (Xcode 7 and 8)** **Swift 2 (Xcode 7 and 8)**
  
-<​code ​javascript>+<​code ​swift>
 import Foundation import Foundation
 print(NSProcessInfo.processInfo().environment["​KMVAR_Variable_Name"​]!) print(NSProcessInfo.processInfo().environment["​KMVAR_Variable_Name"​]!)
 </​code>​ </​code>​
  
-**Swift 3 (currently beta, Xcode 8)**+**Swift 3 (Xcode 8)**
  
-<​code ​javascript>+<​code ​swift>
 import Foundation import Foundation
 print(ProcessInfo.processInfo.environment["​KMVAR_Variable_Name"​]!) print(ProcessInfo.processInfo.environment["​KMVAR_Variable_Name"​]!)
 </​code>​ </​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.
  
 ---- ----
action/Execute_a_Swift_Script.txt ยท Last modified: 2016/09/13 22:08 by peternlewis