User Tools

Site Tools


manual:Scripting

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
manual:Scripting [2019/07/15 03:33]
peternlewis [Controlling Keyboard Maestro via Scripting]
manual:Scripting [2023/10/04 04:27] (current)
peternlewis
Line 11: Line 11:
   * [[actions:​Execute_a_JavaScript_in_Browser|Execute a JavaScript in Browser]]   * [[actions:​Execute_a_JavaScript_in_Browser|Execute a JavaScript in Browser]]
   * [[action:​Execute_a_Shell_Script|Execute a Shell Script]]   * [[action:​Execute_a_Shell_Script|Execute a Shell Script]]
 +  * [[action:​Execute_Shortcut|Execute Shortcut]]
   * [[action:​Execute_a_Swift_Script|Execute a Swift Script]]   * [[action:​Execute_a_Swift_Script|Execute a Swift Script]]
   * [[action:​Apply_a_BBEdit_Text_Factory|Apply a BBEdit Text Factory to the Clipboard]]   * [[action:​Apply_a_BBEdit_Text_Factory|Apply a BBEdit Text Factory to the Clipboard]]
Line 16: Line 17:
 You can specify the script to be executed either by reference to a file or as text entered directly in the _Execute Action_. ​ After pasting or typing script text into the Execute Script Action, press the <​key>​Enter</​key>​ key to compile and format the script. You can specify the script to be executed either by reference to a file or as text entered directly in the _Execute Action_. ​ After pasting or typing script text into the Execute Script Action, press the <​key>​Enter</​key>​ key to compile and format the script.
  
-For example, the [[action:​Execute_an_AppleScript]] ​Action:\\ +For example, the [[action:​Execute_an_AppleScript|Execute an AppleScript]] action: 
-{{:km-7.3-execute-script.png?​nolink|}}\\+ 
 +{{ execute-script-example.png?nolink&​424x400 ​Execute AppleScript Example ​}}
  
 **There are two setup options:** **There are two setup options:**
 ^ Setup Option ^ Choices\\ (Default shown first) ^ ^ Setup Option ^ Choices\\ (Default shown first) ^
-| Script location | Execute text script (type or paste script into Action text field)\\ Execute script file (This is faster if it is a compiled script file `.scpt`) | +| Script location | Execute text script (type or paste script into Action text field)\\ Execute script file (This may be faster if it is a compiled script file `.scpt`) | 
-| Script Results | Ignored.\\ Displayed in a floating window.\\ Displayed briefly in a Notification.\\ Typed in the current text field that has focus.\\ Pasted in text field that has focus.\\ Saved to a Keyboard Maestro Variable.\\ Saved to the System or Named Clipboard.\\ Ignore ​Results ​and run //​Asynchronously//​ (the script runs while the macro continues on to the next //Action//.| +| Script Results | Ignored.\\ Displayed in a floating window.\\ Displayed briefly in a Notification.\\ Displayed large across the screen.\\ Typed in the current text field that has focus.\\ Pasted in text field that has focus.\\ Saved to a Keyboard Maestro Variable.\\ Appended ​to a Keyboard Maestro Variable.\\ Saved to the System or Named Clipboard.\\ Saved to a file.\\ Ignore ​the results ​and run //​Asynchronously//​ (the script runs while the macro continues on to the next //Action//). |
- +
- +
-==== Getting Script Results ==== +
- +
-The results of scripts can be displayed, or they can be typed or pasted in to the current application document, or saved into a Keyboard Maestro Variable or the System Clipboard. ​ This allows you to insert text that depends on many factors, such as date calculations,​ file listings, SQL queries, web pages, or anything else you can imagine. ​ Alternatively,​ the results can be displayed in Notification Center, in a window, or in large type across the screen. ​ Or you can ignore the results, or execute the script asynchronously,​ leaving it running in the background.+
  
 ==== Using the Clipboard ==== ==== Using the Clipboard ====
  
-You can also read and set the clipboard in a script, although the easiest way to set the clipboard to the results of a script is to select the Execute Script Action option to "save results to clipboard"​. ​ This works _only_ ​for plain text returned by the script. ​ For more complex formats, you will need to set the Clipboard directly in the script.+You can also read and set the clipboard in a script, although the easiest way to set the clipboard to the results of a script is to select the Execute Script Action option to "save results to clipboard"​. ​ This works for plain text and some images ​returned by the script. ​ For more complex formats, you will need to set the Clipboard directly in the script.
  
 For more information about using the Clipboard in scripts, see: For more information about using the Clipboard in scripts, see:
   * [AppleScript Clipboard Commands suite](https://​developer.apple.com/​library/​mac/​documentation/​AppleScript/​Conceptual/​AppleScriptLangGuide/​reference/​ASLR_cmds.html)   * [AppleScript Clipboard Commands suite](https://​developer.apple.com/​library/​mac/​documentation/​AppleScript/​Conceptual/​AppleScriptLangGuide/​reference/​ASLR_cmds.html)
-  * [Shell Scripts Pasteboard Commands: ​pbcopy, pbpaste](https://​developer.apple.com/​legacy/​library/​documentation/​Darwin/​Reference/​ManPages/​man1/​pbpaste.1.html)+  * The unix man pages for pbcopy, pbpaste.
  
 ==== Automating Applications and Adding Functionality to Keyboard Maestro ==== ==== Automating Applications and Adding Functionality to Keyboard Maestro ====
Line 43: Line 40:
 ==== Web Page Interactions ==== ==== Web Page Interactions ====
  
-The [[actions:​Execute_a_JavaScript_in_Browser|Execute a JavaScript in Browser]] actions ​enables ​deep control over a web page, as well as extracting specific data from both the page contents and HTML.+The [[actions:​Execute_a_JavaScript_in_Browser|Execute a JavaScript in Browser]] actions ​enable ​deep control over a web page, as well as extracting specific data from both the page contents and HTML.
  
 ==== Shell Scripts ==== ==== Shell Scripts ====
Line 51: Line 48:
 Variables can be accessed from shell scripts via the environment variables in the form $KMVAR‗Variable‗Name where KMVAR‗ is prefixed, and spaces are converted to underscores. ​ AppleScripts can also access the environment variables using the CODE{{{system attribute}}} command, but note that CODE{{{system attribute}}} is not safe for international characters. Variables can be accessed from shell scripts via the environment variables in the form $KMVAR‗Variable‗Name where KMVAR‗ is prefixed, and spaces are converted to underscores. ​ AppleScripts can also access the environment variables using the CODE{{{system attribute}}} command, but note that CODE{{{system attribute}}} is not safe for international characters.
  
-Note that the total size of the variables stored in the environment is limited to 100K, so larger variables may be excluded to ensure the variables do not take up excessive amounts of environment space since this is limited by the system.+Note that the total size of the variables stored in the environment is limited ​by the system ​to 100K, so larger variables may be excluded to ensure the variables do not take up excessive amounts of environment space.
  
 Variables whose names start with "​ENV‗"​ override regular environment variables (eg "​ENV‗PATH"​ overrides the regular "​PATH"​ environment variable). Variables whose names start with "​ENV‗"​ override regular environment variables (eg "​ENV‗PATH"​ overrides the regular "​PATH"​ environment variable).
  
-===== AppleScript ​=====+==== AppleScript ====
  
 AppleScripts ​ can perform many tasks on the Mac and control many applications. AppleScripts ​ can perform many tasks on the Mac and control many applications.
Line 71: Line 68:
 </​code>​ </​code>​
  
-**For AppleScripts compatible with prior versions of Keyboard Maestro**, see  [[:​AppleScript#​Prior_to_Ver_71|Using AppleScript prior to version 7.1]]. +==== JavaScript for Automation ​====
- +
- +
-===== JXA =====+
  
 JavaScript for Automation (JXA) can access Variables with: JavaScript for Automation (JXA) can access Variables with:
Line 85: Line 79:
  
 For details on JXA, see [[:​JavaScript for Automation]]. This provides an introduction to JXA and comparison with AppleScript. For details on JXA, see [[:​JavaScript for Automation]]. This provides an introduction to JXA and comparison with AppleScript.
-===== JavaScript ===== 
  
-JavaScript in web browsers can access the variable values by using the document.kmvar dictionary, like document.kmvar.Variable‗Name (spaces are converted to underscores),​ but they have no way to write values back to variables, except by returning a result from the script.+==== JavaScript ==== 
 + 
 +JavaScript in web browsers can access the variable values by using the kmvar dictionary, like kmvar.Variable‗Name (spaces are converted to underscores),​ but they have no way to write values back to variables, except by returning a result from the script.
  
 ===== User Interaction ===== ===== User Interaction =====
Line 99: Line 94:
 end tell end tell
 </​code>​ </​code>​
- 
-The CODE{{{osascript}}} tool will execute in 64-bit mode, which may be a problem if you have old versions of AppleScript extensions installed. ​ However, you can set the command line tool that is used to execute AppleScripts as described in [[Preferences#​Other_Hidden_Preferences|Other Hidden Preferences]]. 
  
 See also the [[Variables]] section. See also the [[Variables]] section.
Line 132: Line 125:
 </​code>​ </​code>​
  
-The CODE{{{do script}}} will not return until the macro is finished executing.+The CODE{{{do script}}} will not return until the macro is finished executing. The [[action:​Return_Result|Return Result]] action can be used to return a result to the AppleScipt.
  
 You can pass an optional parameter using the CODE{{{with parameter}}} clause, which you can read in the macro as the [[token:​TriggerValue|%TriggerValue% token]]. You can pass an optional parameter using the CODE{{{with parameter}}} clause, which you can read in the macro as the [[token:​TriggerValue|%TriggerValue% token]].
Line 170: Line 163:
 end tell end tell
 </​code>​ </​code>​
 +
 +or process tokens with:
 +
 +<code applescript>​
 +tell application "​Keyboard Maestro Engine"​
 +  process tokens "​%LongDate%"​
 +end tell
 +</​code>​
 +
 +The `calculate` and `process tokens` commands can also take an `instance` parameter (v10.0+) to specify the instance for local variable access.
  
 You can ask Keyboard Maestro Engine to find strings in other scripts: You can ask Keyboard Maestro Engine to find strings in other scripts:
manual/Scripting.1563175984.txt.gz · Last modified: 2019/07/15 03:33 by peternlewis