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/08/25 09:43] – [Using Keyboard Maestro Facilities from AppleScript] peternlewis | manual:Scripting [2023/10/04 08:27] (current) – peternlewis | ||
|---|---|---|---|
| Line 9: | Line 9: | ||
| * [[action: | * [[action: | ||
| * [[action: | * [[action: | ||
| - | * [[action:Execute_a_JavaScript_in_Safari|Execute a JavaScript in Safari]] | + | * [[actions:Execute_a_JavaScript_in_Browser|Execute a JavaScript in Browser]] |
| - | * [[action: | + | |
| * [[action: | * [[action: | ||
| + | * [[action: | ||
| * [[action: | * [[action: | ||
| * [[action: | * [[action: | ||
| Line 17: | 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 38: | Line 40: | ||
| ==== Web Page Interactions ==== | ==== Web Page Interactions ==== | ||
| - | + | The [[actions:Execute_a_JavaScript_in_Browser|Execute a JavaScript in Browser]] actions enable | |
| - | JavaScript in a Browser Actions ([[action:Execute_a_JavaScript_in_Google_Chrome|Google Chrome]] and [[action: | + | |
| ==== Shell Scripts ==== | ==== Shell Scripts ==== | ||
| - | |||
| Shell scripts can execute any installed scripting language, such as perl, python, ruby or whatever. | Shell scripts can execute any installed scripting language, such as perl, python, ruby or whatever. | ||
| - | |||
| 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 | ||
| - | You can read and write variables and control the Keyboard Maestro engine with AppleScript: | + | You can read and write [[Variables|variables]], [[Dictionaries|dictionaries]] |
| <code applescript> | <code applescript> | ||
| Line 69: | 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 83: | 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 97: | 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 130: | 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 169: | 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 189: | 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 203: | 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 |
| - | + | ||
| - | Another way you can control | + | |
| - | + | ||
| - | ^ Example Editor URL Command ^ Description ^ | + | |
| - | | `keyboardmaestro:// | + | |
| - | | `keyboardmaestro:// | + | |
| - | | `keyboardmaestro:// | + | |
| - | | `keyboardmaestro:// | + | |
| - | | `keyboardmaestro:// | + | |
| - | | `keyboardmaestro:// | + | |
| - | + | ||
| - | --- | + | |
| - | + | ||
| - | ==== Triggers ==== | + | |
| - | + | ||
| - | You can trigger a macro (that is Active and Enabled) using the " | + | |
| - | The URL uses this format:\\ | + | |
| - | `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:// | + | |
| + | <code applescript> | ||
| + | tell application " | ||
| + | set bounds of window " | ||
| + | end tell | ||
| + | </ | ||
| - | See also the [[Calculations]] and [[Tokens]] sections. | ||
manual/Scripting.1503654232.txt.gz · Last modified: by peternlewis
