User Tools

Site Tools


manual:Scripting

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
manual:Scripting [2017/08/25 05:37]
peternlewis [Controlling Keyboard Maestro via Scripting]
manual:Scripting [2023/09/12 22:43]
peternlewis [Controlling Keyboard Maestro Engine via Scripting]
Line 9: Line 9:
   * [[action:​Execute_a_JavaScript_For_Automation|Execute a JavaScript For Automation]]   * [[action:​Execute_a_JavaScript_For_Automation|Execute a JavaScript For Automation]]
   * [[action:​Execute_a_JavaScript_in_Custom_Prompt|Execute JavaScript in Custom HTML Prompt]]   * [[action:​Execute_a_JavaScript_in_Custom_Prompt|Execute JavaScript in Custom HTML Prompt]]
-  * [[action:Execute_a_JavaScript_in_Safari|Execute a JavaScript in Safari]] +  * [[actions:Execute_a_JavaScript_in_Browser|Execute a JavaScript in Browser]]
-  * [[action:​Execute_a_JavaScript_in_Google_Chrome|Execute a JavaScript in Google Chrome]]+
   * [[action:​Execute_a_Shell_Script|Execute a Shell Script]]   * [[action:​Execute_a_Shell_Script|Execute a Shell Script]]
   * [[action:​Execute_a_Swift_Script|Execute a Swift Script]]   * [[action:​Execute_a_Swift_Script|Execute a Swift Script]]
Line 19: Line 18:
 For example, the [[action:​Execute_an_AppleScript]] Action:\\ For example, the [[action:​Execute_an_AppleScript]] Action:\\
 {{:​km-7.3-execute-script.png?​nolink|}}\\ {{:​km-7.3-execute-script.png?​nolink|}}\\
 +
 +**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.\\ 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 Results and run //​Asynchronously//​ (the script runs while the macro continues on to the next //​Action//​). |
 +
  
 ==== Getting Script Results ==== ==== Getting Script Results ====
Line 30: Line 35:
 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://​developer.apple.com/​library/​mac/​documentation/​AppleScript/​Conceptual/​AppleScriptLangGuide/​reference/​ASLR_cmds.html)   * [AppleScript Clipboard Commands suite](https://​developer.apple.com/​library/​mac/​documentation/​AppleScript/​Conceptual/​AppleScriptLangGuide/​reference/​ASLR_cmds.html)
-  * [Shell Scripts Pasteboard Commands: ​pbcopy, pbpaste](https://​developer.apple.com/​legacy/​library/​documentation/​Darwin/​Reference/​ManPages/​man1/​pbpaste.1.html)+  * 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 43:
 ==== Web Page Interactions ==== ==== Web Page Interactions ====
  
- +The [[actions:Execute_a_JavaScript_in_Browser|Execute a JavaScript in Browser]] actions ​enables deep control over a web page, as well as extracting specific data from both the page contents and HTML.
-JavaScript in a Browser Actions ([[action:Execute_a_JavaScript_in_Google_Chrome|Google Chrome]] and [[action:​Execute_a_JavaScript_in_Safari|Safari]]enables deep control over a web page, as well as extracting specific data from both the page contents and HTML.+
  
 ==== Shell Scripts ==== ==== Shell Scripts ====
- 
  
 Shell scripts can execute any installed scripting language, such as perl, python, ruby or whatever. ​ Be aware that because shell scripts are executed in a non-interactive shell, typically none of the shell configuration files (like CODE{{{.login}}} or CODE{{{.profile}}}) will be executed, which may change the way the shell script behaves. Shell scripts can execute any installed scripting language, such as perl, python, ruby or whatever. ​ Be aware that because shell scripts are executed in a non-interactive shell, typically none of the shell configuration files (like CODE{{{.login}}} or CODE{{{.profile}}}) will be executed, which may change the way the shell script behaves.
- 
  
 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. ​ AppleScripts can also access the environment variables using the CODE{{{system attribute}}} command, but note that CODE{{{system attribute}}} is not safe for international characters. 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. ​ AppleScripts can also access the environment variables using the CODE{{{system attribute}}} command, but note that CODE{{{system attribute}}} is not safe for international characters.
Line 57: Line 59:
 AppleScripts ​ can perform many tasks on the Mac and control many applications. AppleScripts ​ can perform many tasks on the Mac and control many applications.
  
-You can read and write variables and control the Keyboard Maestro engine with AppleScript:​+You can read and write [[Variables|variables]], [[Dictionaries|dictionaries]] ​and control the Keyboard Maestro engine with AppleScript:​
  
 <code applescript>​ <code applescript>​
Line 130: Line 132:
 </​code>​ </​code>​
  
-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:​Return_Result|Return Result]] action can be used to return a result to the AppleScipt.
  
 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]].
Line 169: Line 171:
 </​code>​ </​code>​
  
-You can ask Keyboard Maestro Engine to process tokens ​for you with:+or process tokens with:
  
 <code applescript>​ <code applescript>​
 tell application "​Keyboard Maestro Engine"​ tell application "​Keyboard Maestro Engine"​
-  ​set clip to process tokens "%PastClipboard%3%"+  process tokens "%LongDate%"
 end tell end tell
 </​code>​ </​code>​
  
-===== URL Schemes =====+The `calculate` and `process tokens` commands can also take an `instance` parameter (v10.0+) to specify the instance for local variable access.
  
-==== Editor ====+You can ask Keyboard Maestro Engine to find strings in other scripts:
  
-Another way you can control ​Keyboard Maestro ​Editor is using the "`keyboardmaestro`" ​URL scheme, which supports the following formats:+<code applescript>​ 
 +tell application "Keyboard Maestro ​Engine"​ 
 +   count found in "The Source"​ for ".e" ​with regex 
 +end tell 
 +</​code>​
  
-^ Example Editor URL Command ^ Description ^ +And you can search ​and replace ​with:
-| `keyboardmaestro://​u=support%40stairways.com/​s=ABCDEFGH0123456789` | enter your username/​serial number. | +
-| `keyboardmaestro://​m=Activate%20Application%20Switcher`\\ `keyboardmaestro://​m=D2F427A1-51E3-4719-820B-4C25B6FF7329` | edit a specific macro or macro group.\\ You may used either the Macro Name, or UUID. | +
-| `keyboardmaestro://​q=Activate` | filter macros with this keyword. | +
-| `keyboardmaestro://​g=All%20Macros/​q=Activate` | select a macro group and filter macros ​with this keyword. | +
-| `keyboardmaestro://​a=Execute` | filter actions with this keyword. | +
-| `keyboardmaestro://​c=All%20Actions/​a=Execute` | select action category and filter actions with this keyword. |+
  
----+<code applescript>​ 
 +tell application "​Keyboard Maestro Engine"​ 
 + search "​3+4"​ for "​(\\d+)"​ replace "​%CalculateFormat%CALCULATE(\\1)%Currency%"​ with regex and process tokens 
 +end tell 
 +</​code>​
  
-==== Triggers ​====+===== Controlling Keyboard Maestro via Scripting =====
  
-You can trigger a macro (that is Active and Enabled) using the "​`kmtrigger`"​ URL scheme.\\ +Macro Groups, Smart Groups, Macros, Triggers, Actions are all available via AppleScript. ​ So you have deep control over controlling ​the Keyboard Maestro editor itself, allowing you to automate creating macros in a variety of different ways.
-The URL uses this format:\\ +
-`kmtrigger://​macro=<​Macro Name or UUID>​[&<​Trigger Value>]`+
  
-where +You can disable or enable a Macro Group (or similarly ​Macro) from AppleScript with:
-  * `<Macro Name or UUID>` is either the Macro Name, OR, its UUID\\ +
-  * `<​Trigger Value>` is the _optional_ Trigger Value +
-  * `[ ]` are _not_ included. ​ Used only to show _optional_ parameters +
-  * `&` must precede the Trigger Value, if it is provided.+
  
-Note both Macro Name and Trigger Value must be URL encoded.+<code applescript>​ 
 +tell application "​Keyboard Maestro"​ 
 +  set enabled of macro group "Macro Group Group Name or UID" to true/​false 
 +end tell 
 +</​code>​
  
-You can easily get the Trigger URL by clicking on the "Or by Script"​ Trigger option, and selecting "Or by URL".+Alternatively you can use the [[action:​Set_Macro_or_Group_Enable|Set Macro Enable action]].
  
-{{:​manual:​km-7.3-macro-trigger-url-setup.png?​nolink|}}+You can rename, create, delete, duplicate ​macro groups and macros. ​ You can add and remove actions. ​ You can get or set the selection.
  
-{{:manual:​km-7.3-macro-trigger-url-results.png?​nolink|}}+You can start editing a Macro or Macro Group from AppleScript with:
  
-^ Example Trigger URL Command ^ Description ^ +<code applescript>​ 
-| `kmtrigger://​macro=Your%20Macro%20Name` | Using the Macro Name | +tell application "​Keyboard Maestro"​ 
-| `kmtrigger://​macro=224AA8CB-07EB-4C92-8201-68FED82B6E9F` | Using the Macro UUID | +  ​editMacro "Macro/​Macro ​Group Name or UID" 
-| `kmtrigger://​macro=Your%20Macro%20Name&​value=Your%20Trigger%20Value` | Using Macro Name with Trigger Value | +end tell 
-| `kmtrigger://​macro=224AA8CB-07EB-4C92-8201-68FED82B6E9F&​value=Your%20Trigger%20Value` | Using Macro UUID with Trigger Value  |+</code>
  
 +See the Keyboard Maestro AppleScript dictionary for more information.
 +
 +===== Controlling Keyboard Maestro Engine via Scripting =====
 +
 +You can adjust Keyboard Maestro Engine windows with:
 +
 +<code applescript>​
 +tell application "​Keyboard Maestro Engine"​
 +    set bounds of window "​Clipboard History Switcher"​ to {1000, 50, 2000, 1200}
 +end tell
 +</​code>​
  
-See also the [[Calculations]] and [[Tokens]] sections. 
  
manual/Scripting.txt · Last modified: 2023/10/04 04:27 by peternlewis