manual:Scripting
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| manual:Scripting [2018/07/02 23:38] – Add setup options table JMichaelTX | manual:Scripting [2023/10/04 08:27] (current) – peternlewis | ||
|---|---|---|---|
| Line 11: | Line 11: | ||
| * [[actions: | * [[actions: | ||
| * [[action: | * [[action: | ||
| + | * [[action: | ||
| * [[action: | * [[action: | ||
| * [[action: | * [[action: | ||
| 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_. | You can specify the script to be executed either by reference to a file or as text entered directly in the _Execute Action_. | ||
| - | For example, the [[action: | + | For example, the [[action: |
| - | {{:km-7.3-execute-script.png? | + | |
| + | {{ execute-script-example.png?nolink& | ||
| **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 | + | | 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 |
| - | + | ||
| - | + | ||
| - | ==== 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. | + | |
| ==== 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" | + | 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" |
| 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:// | * [AppleScript Clipboard Commands suite](https:// | ||
| - | * [Shell Scripts Pasteboard Commands: | + | * 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: | + | The [[actions: |
| ==== 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. | 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. | ||
| - | 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 |
| Variables whose names start with " | Variables whose names start with " | ||
| - | ===== AppleScript | + | ==== AppleScript ==== |
| AppleScripts | AppleScripts | ||
| Line 71: | Line 68: | ||
| </ | </ | ||
| - | **For AppleScripts compatible with prior versions of Keyboard Maestro**, see [[: | + | ==== 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 [[: | For details on JXA, see [[: | ||
| - | ===== 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), | + | ==== JavaScript ==== |
| + | |||
| + | JavaScript in web browsers can access the variable values by using the kmvar dictionary, like kmvar.Variable‗Name (spaces are converted to underscores), | ||
| ===== User Interaction ===== | ===== User Interaction ===== | ||
| Line 99: | Line 94: | ||
| end tell | end tell | ||
| </ | </ | ||
| - | |||
| - | The CODE{{{osascript}}} tool will execute in 64-bit mode, which may be a problem if you have old versions of AppleScript extensions installed. | ||
| See also the [[Variables]] section. | See also the [[Variables]] section. | ||
| Line 132: | Line 125: | ||
| </ | </ | ||
| - | 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: |
| You can pass an optional parameter using the CODE{{{with parameter}}} clause, which you can read in the macro as the [[token: | You can pass an optional parameter using the CODE{{{with parameter}}} clause, which you can read in the macro as the [[token: | ||
| Line 170: | Line 163: | ||
| end tell | end tell | ||
| </ | </ | ||
| + | |||
| + | or process tokens with: | ||
| + | |||
| + | <code applescript> | ||
| + | tell application " | ||
| + | process tokens " | ||
| + | end tell | ||
| + | </ | ||
| + | |||
| + | 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: | ||
| Line 213: | Line 216: | ||
| See the Keyboard Maestro AppleScript dictionary for more information. | See the Keyboard Maestro AppleScript dictionary for more information. | ||
| - | ===== URL Schemes | + | ===== Controlling Keyboard Maestro Engine via Scripting |
| - | ==== Editor ==== | + | You can adjust Keyboard Maestro Engine windows with: |
| - | Another way you can control | + | <code applescript> |
| - | + | tell application "Keyboard Maestro | |
| - | ^ Example Editor URL Command ^ Description ^ | + | set bounds of window |
| - | | `keyboardmaestro:// | + | end tell |
| - | | `keyboardmaestro:// | + | </ |
| - | | `keyboardmaestro:// | + | |
| - | | `keyboardmaestro:// | + | |
| - | | `keyboardmaestro:// | + | |
| - | | `keyboardmaestro:// | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ==== Triggers ==== | + | |
| - | + | ||
| - | You can trigger a macro (that is Active and Enabled) using the "`kmtrigger`" | + | |
| - | + | ||
| - | `kmtrigger:// | + | |
| - | + | ||
| - | where | + | |
| - | * `<Macro Name or UUID>` is either the Macro Name, OR, its UUID | + | |
| - | * `< | + | |
| - | * `[ ]` are _not_ included. | + | |
| - | * `&` must precede the Trigger Value, if it is provided. | + | |
| - | + | ||
| - | Note both Macro Name and Trigger Value must be URL encoded. | + | |
| - | + | ||
| - | You can easily get the Trigger URL by clicking on the "Or by Script" | + | |
| - | + | ||
| - | {{: | + | |
| - | + | ||
| - | {{: | + | |
| - | ^ Example Trigger URL Command ^ Description ^ | ||
| - | | `kmtrigger:// | ||
| - | | `kmtrigger:// | ||
| - | | `kmtrigger:// | ||
| - | | `kmtrigger:// | ||
manual/Scripting.1530574732.txt.gz · Last modified: by JMichaelTX
