manual:Scripting
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| manual:Scripting [2017/09/08 08:26] – peternlewis | 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: |
| - | {{: | + | |
| - | ==== Getting Script Results ==== | + | {{ execute-script-example.png? |
| - | The results of scripts can be displayed, | + | **There are two setup options: |
| + | ^ Setup Option ^ Choices\\ (Default shown first) ^ | ||
| + | | 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.\\ Displayed large across the screen.\\ Typed in the current | ||
| ==== 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 37: | Line 40: | ||
| ==== Web Page Interactions ==== | ==== Web Page Interactions ==== | ||
| - | The [[actions: | + | The [[actions: |
| ==== Shell Scripts ==== | ==== Shell Scripts ==== | ||
| Line 45: | 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 65: | 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 79: | 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 93: | 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 126: | 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 165: | Line 164: | ||
| </ | </ | ||
| - | You can ask Keyboard Maestro Engine to process tokens | + | or process tokens with: |
| <code applescript> | <code applescript> | ||
| tell application " | tell application " | ||
| - | | + | process tokens "%LongDate%" |
| + | 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: | ||
| + | |||
| + | <code applescript> | ||
| + | tell application " | ||
| + | count found in "The Source" | ||
| + | end tell | ||
| + | </ | ||
| + | |||
| + | And you can search and replace with: | ||
| + | |||
| + | <code applescript> | ||
| + | tell application " | ||
| + | search "3+4" for " | ||
| end tell | end tell | ||
| </ | </ | ||
| Line 185: | Line 202: | ||
| </ | </ | ||
| - | Alternatively you can use the [[/action/ | + | Alternatively you can use the [[action: |
| You can rename, create, delete, duplicate macro groups and macros. | You can rename, create, delete, duplicate macro groups and macros. | ||
| Line 199: | 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.1504859167.txt.gz · Last modified: by peternlewis
