documentation:Scripting
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| documentation:Scripting [2016/05/30 06:40] – created peternlewis | documentation:Scripting [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Scripting ====== | ||
| - | |||
| - | ===== Executing Scripts ===== | ||
| - | |||
| - | You can execute shells scripts, AppleScripts, | ||
| - | |||
| - | AppleScripts and shell scripts, JavaScript for Automation and Swift scripts, give you a powerful way of adding new facilities we have not specifically provided for, as well as controlling other applications. | ||
| - | |||
| - | Shell scripts can execute any installed scripting language, such as perl, python, ruby or whatever. | ||
| - | |||
| - | The results of scripts can be displayed, or they can be typed or pasted in to the current selection, or saved into a variable or clipboard. | ||
| - | |||
| - | You can also use the clipboard to pass data between actions. | ||
| - | |||
| - | 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. | ||
| - | |||
| - | Variables whose names start with " | ||
| - | |||
| - | AppleScript can create variables with: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | make variable with properties {name:" | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | or read and write variables with: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | set kmVarRef to make variable with properties {name:" | ||
| - | set oldValue to value of kmVarRef | ||
| - | set value of kmVarRef to 10 | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | or the less AppleScripty way (7.1+) with: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | set oldValue to getvariable " | ||
| - | setvariable " | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | and JavaScript for Automation (JXA) can access variables with: | ||
| - | |||
| - | <code javascript> | ||
| - | var kme = Application(" | ||
| - | var oldValue = kme.getvariable(' | ||
| - | kme.setvariable(' | ||
| - | </ | ||
| - | |||
| - | and delete variables with: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | delete variable " | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | 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). | ||
| - | |||
| - | AppleScripts and JavaScript For Automation scripts are executed in the background via CODE{{{osascript}}}. | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | activate | ||
| - | display dialog " | ||
| - | 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. | ||
| - | |||
| - | ===== Controlling Keyboard Maestro via Scripting ===== | ||
| - | |||
| - | The primary scripting interface to Keyboard Maestro is the Keyboard Maestro Engine’s CODE{{{do script}}} support. | ||
| - | |||
| - | * execute a macro by name | ||
| - | * execute a macro by unique ID | ||
| - | * execute an action given its XML code | ||
| - | |||
| - | Note in most cases you must ask “Keyboard Maestro Engine”, not “Keyboard Maestro”. | ||
| - | |||
| - | The easiest way is to use the name, for example: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | do script "[Name of Your Macro]" | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | The macro must be defined and enabled, and the macro group must be enabled and currently active. | ||
| - | |||
| - | If there is more than one macro with the same name, you will get an error, so you can use a UID instead of a name. | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | do script " | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | The CODE{{{do script}}} will not return until the macro is finished executing. | ||
| - | |||
| - | You can pass an optional parameter using the CODE{{{with parameter}}} clause, which you can read in the macro as the [[/ | ||
| - | |||
| - | 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. | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | do script "< | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | The easiest way to determine the appropriate XML is to create an example action in an example macro and then export the macro. | ||
| - | |||
| - | 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> | ||
| - | tell application " | ||
| - | setMacroEnable " | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | This actually asks the editor to disable or enable the macro or macro group, so the change is both visible and permanent. | ||
| - | |||
| - | Alternatively you can use the [[/ | ||
| - | |||
| - | You can start editing a Macro or Macro Group from AppleScript with: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | editMacro " | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | You can ask Keyboard Maestro Engine to reload the macros with: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | reload | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | You can also import macros, get the selected macros, or delete a macro or macro group from AppleScript. | ||
| - | |||
| - | ===== Enhancing AppleScript ===== | ||
| - | |||
| - | Keyboard Maestro Engine makes several of its facilities available to AppleScript. | ||
| - | |||
| - | You can ask it to play a sound with: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | play sound alias " | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | You can ask Keyboard Maestro Engine to perform a calculation for you with: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | set n to calculate " | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | You can ask Keyboard Maestro Engine to process tokens for you with: | ||
| - | |||
| - | <code applescript> | ||
| - | tell application " | ||
| - | set clip to process tokens " | ||
| - | end tell | ||
| - | </ | ||
| - | |||
| - | ===== The keyboardmaestro URL Scheme ===== | ||
| - | |||
| - | Another way you can control Keyboard Maestro us using the keyboardmaestro URL scheme, which supports the following formats: | ||
| - | |||
| - | * keyboardmaestro:// | ||
| - | * keyboardmaestro:// | ||
| - | * keyboardmaestro:// | ||
| - | * keyboardmaestro:// | ||
| - | * keyboardmaestro:// | ||
| - | * keyboardmaestro:// | ||
| - | |||
| - | See also the [[Calculations]] and [[Text Tokens]] sections. | ||
documentation/Scripting.1464590452.txt.gz · Last modified: by peternlewis
