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:31] – [JavaScript] 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. | ||
| - | ===== Controlling Keyboard Maestro via Scripting ===== | + | ===== Controlling Keyboard Maestro |
| - | The primary scripting interface to Keyboard Maestro is the Keyboard Maestro Engine’s CODE{{{do script}}} support. | + | The primary scripting interface to Keyboard Maestro |
| * execute a macro by name | * execute a macro by name | ||
| Line 110: | Line 105: | ||
| * execute an action given its XML code | * execute an action given its XML code | ||
| - | Note in most cases you must ask “Keyboard Maestro Engine”, not “Keyboard Maestro”. | + | Note in most cases you must tell “Keyboard Maestro Engine”, not “Keyboard Maestro”. |
| The easiest way is to use the name, for example: | The easiest way is to use the name, for example: | ||
| 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/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]]. |
| You can determine a macro’s UID by selecting it and choosing [[Menus# | You can determine a macro’s UID by selecting it and choosing [[Menus# | ||
| - | |||
| - | You can trigger a macro using the kmtrigger: scheme with a URL like `kmtrigger:// | ||
| An even more powerful way to script Keyboard Maestro is to execute specific actions based on their XML code. This allows you to construct any action, including changing the action on the fly, without having to create a macro first. | An even more powerful way to script Keyboard Maestro is to execute specific actions based on their XML code. This allows you to construct any action, including changing the action on the fly, without having to create a macro first. | ||
| Line 146: | Line 139: | ||
| </ | </ | ||
| - | The easiest way to determine the appropriate XML is to create an example action in an example macro and then export | + | The easiest way to determine the appropriate XML is to create an example action in an example macro and then choose [[Menus# |
| You can read information about the existing macros using the CODE{{{gethotkeys}}} and CODE{{{getmacros}}} commands. | You can read information about the existing macros using the CODE{{{gethotkeys}}} and CODE{{{getmacros}}} commands. | ||
| - | You can disable or enable a Macro or Macro Group from AppleScript with: | ||
| - | <code applescript> | + | ===== Using Keyboard Maestro |
| - | tell application "Keyboard Maestro" | + | |
| - | setMacroEnable " | + | |
| - | end tell | + | |
| - | </ | + | |
| - | This actually asks the editor | + | Keyboard Maestro Engine makes several of its facilities available |
| - | Alternatively you can use the [[/ | + | You can ask it to play a sound with: |
| - | + | ||
| - | You can start editing | + | |
| <code applescript> | <code applescript> | ||
| - | tell application " | + | tell application " |
| - | | + | |
| end tell | end tell | ||
| </ | </ | ||
| - | You can ask Keyboard Maestro Engine to reload the macros | + | You can ask Keyboard Maestro Engine to perform a calculation for you with: |
| <code applescript> | <code applescript> | ||
| tell application " | tell application " | ||
| - | | + | |
| end tell | end tell | ||
| </ | </ | ||
| - | You can also import macros, get the selected macros, | + | or process tokens |
| - | + | ||
| - | ===== Using Keyboard Maestro Facilities from AppleScript ===== | + | |
| - | + | ||
| - | Keyboard Maestro Engine makes several of its facilities available to AppleScript. | + | |
| - | + | ||
| - | You can ask it to play a sound with: | + | |
| <code applescript> | <code applescript> | ||
| tell application " | tell application " | ||
| - | | + | |
| end tell | end tell | ||
| </ | </ | ||
| - | You can ask Keyboard Maestro Engine to perform a calculation for you with: | + | 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> | <code applescript> | ||
| tell application " | tell application " | ||
| - | set n to calculate | + | count found in "The Source" |
| end tell | end tell | ||
| </ | </ | ||
| - | You can ask Keyboard Maestro Engine to process tokens for you with: | + | And you can search and replace |
| <code applescript> | <code applescript> | ||
| tell application " | tell application " | ||
| - | set clip to process tokens | + | search " |
| end tell | end tell | ||
| </ | </ | ||
| - | ===== URL Schemes | + | ===== Controlling Keyboard Maestro via Scripting |
| - | ==== Editor ==== | + | Macro Groups, Smart Groups, Macros, Triggers, Actions are all available via AppleScript. |
| - | Another way you can control Keyboard Maestro Editor is using the " | + | You can disable or enable a Macro Group (or similarly Macro) from AppleScript with: |
| - | ^ Example Editor URL Command ^ Description ^ | + | <code applescript> |
| - | | `keyboardmaestro:// | + | tell application " |
| - | | `keyboardmaestro:// | + | set enabled of macro group "Macro Group Group Name or UID" to true/false |
| - | | `keyboardmaestro: | + | end tell |
| - | | `keyboardmaestro:// | + | </code> |
| - | | `keyboardmaestro:// | + | |
| - | | `keyboardmaestro:// | + | |
| - | --- | + | Alternatively you can use the [[action: |
| - | ==== Triggers ==== | + | You can rename, create, delete, duplicate macro groups and macros. |
| - | You can trigger | + | You can start editing |
| - | The URL uses this format:\\ | + | |
| - | `kmtrigger:// | + | |
| - | where | + | <code applescript> |
| - | | + | tell application " |
| - | * `<Trigger Value>` is the _optional_ Trigger Value | + | |
| - | * `[ ]` are _not_ included. | + | end tell |
| - | * `&` must precede the Trigger Value, if it is provided. | + | </code> |
| - | Note both Macro Name and Trigger Value must be URL encoded. | + | See the Keyboard Maestro AppleScript dictionary for more information. |
| - | You can easily get the Trigger URL by clicking on the "Or by Script" | + | ===== Controlling Keyboard Maestro Engine via Scripting ===== |
| - | {{: | + | You can adjust Keyboard Maestro Engine windows |
| - | + | ||
| - | {{: | + | |
| - | + | ||
| - | ^ 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.1503653460.txt.gz · Last modified: by peternlewis
