User Tools

Site Tools


manual:Scripting

**This is an old revision of the document!**

Scripting

Executing Scripts

The following Execute Script Actions are provided to execute a variety of script languages:

You can specify the script to be executed either by reference to a file or as text entered directly in the Execute Action. After pasting or typing script text into the Execute Script Action, press the Enter key to compile and format the script.

For example, the Execute_an_AppleScript Action:

Getting Script Results

The results of scripts can be displayed, or they can be typed or pasted in to the current application document, or saved into a Keyboard Maestro Variable or the System Clipboard. This allows you to insert text that depends on many factors, such as date calculations, file listings, SQL queries, web pages, or anything else you can imagine. Alternatively, the results can be displayed in Notification Center, in a window, or in large type across the screen. Or you can ignore the results, or execute the script asynchronously, leaving it running in the background.

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”. This works only for plain text returned by the script. For more complex formats, you will need to set the Clipboard directly in the script.

For more information about using the Clipboard in scripts, see:

Automating Applications and Adding Functionality to Keyboard Maestro

AppleScripts, JavaScript for Automation scripts, Shell scripts, and Swift scripts, give you a powerful way of adding new facilities we have not specifically provided for, as well as controlling other applications.

Web Page Interactions

JavaScript in a Browser Actions (Google Chrome and 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 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 .login or .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 system attribute command, but note that system attribute is not safe for international characters.

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 “ENV_” override regular environment variables (eg “ENV_PATH” overrides the regular “PATH” environment variable).

AppleScript

For most recent methods and examples, see Using AppleScript to Get and Set Keyboard Maestro Variables

Get or Set Variables Using Version 7.1+

Keyboard Maestro 7.1+ Provides the Most Compact Methods of Using AppleScript

tell application "Keyboard Maestro Engine"
  --- GET VARIABLE ---
  -- IF KM Variable does NOT exist, the AS Variable will be set to empty string --
  set calcResult to getvariable "Calculation Result"
 
  --- SET VARIABLE ---
  -- IF KM Variable does NOT exist, it will be created --
  setvariable "Calculation Result" to (calcResult + 10)
end tell

For AppleScripts compatible with prior versions of Keyboard Maestro, see Using AppleScript with Ver 6+.

JXA

JavaScript for Automation (JXA) can access Variables with:

var kme = Application("Keyboard Maestro Engine");
var oldValue = kme.getvariable('Calculation Result');
kme.setvariable('Calculation Result', { to: 10 });

For details on JXA, see JavaScript for Automation. This provides an introduction to JXA and comparison with AppleScript.

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).

User Interaction

AppleScripts and JavaScript For Automation scripts are executed in the background via osascript. This means they are not allowed to do user interaction. You can work around this by asking an application like System Events to do the user interaction for you, for example:

tell application "System Events"
  activate
  display dialog "Hello"
end tell

The osascript tool will execute in 64-bit mode, which may be a problem if you have old versions of AppleScript extensions installed. However, you can set the command line tool that is used to execute AppleScripts as described in Other Hidden Preferences.

See also the Variables section.

Controlling Keyboard Maestro via Scripting

The primary scripting interface to Keyboard Maestro is the Keyboard Maestro Engine’s do script support. You can ask Keyboard Maestro to:

  • 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:

tell application "Keyboard Maestro Engine"
  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.

tell application "Keyboard Maestro Engine"
  do script "D0C150C7-8A0C-4837-918A-427E2BCFB6B9"
end tell

The do script will not return until the macro is finished executing.

You can pass an optional parameter using the with parameter clause, which you can read in the macro as the %TriggerValue% token.

You can determine a macro’s UID by selecting it and choosing Copy UID command in the Copy as sub-menu in the Edit menu.

You can trigger a macro using the kmtrigger: scheme with a URL like kmtrigger://macro=MacroName&value=Value (the value is accessible via the %TriggerValue% Token).

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. A simple example would be:

tell application "Keyboard Maestro Engine"
  do script "<dict><key>MacroActionType</key><string>SwitchToLastApplication</string></dict>"
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 gethotkeys and getmacros commands. See the Keyboard Maestro Engine AppleScript dictionary for more information.

You can disable or enable a Macro or Macro Group from AppleScript with:

tell application "Keyboard Maestro"
  setMacroEnable "Macro/Macro Group Group Name or UID" with/without enable
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 Set Macro Enable action.

You can start editing a Macro or Macro Group from AppleScript with:

tell application "Keyboard Maestro"
  editMacro "Macro/Macro Group Name or UID"
end tell

You can ask Keyboard Maestro Engine to reload the macros with:

tell application "Keyboard Maestro Engine"
  reload
end tell

You can also import macros, get the selected macros, or delete a macro or macro group from AppleScript. See the Keyboard Maestro AppleScript dictionary for more information.

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:

tell application "Keyboard Maestro Engine"
  play sound alias "Harddisk:System:Library:Sounds:Glass.aiff"
end tell

You can ask Keyboard Maestro Engine to perform a calculation for you with:

tell application "Keyboard Maestro Engine"
  set n to calculate "JULIANDATE()"
end tell

You can ask Keyboard Maestro Engine to process tokens for you with:

tell application "Keyboard Maestro Engine"
  set clip to process tokens "%PastClipboard%3%"
end tell

URL Schemes

Editor

Another way you can control Keyboard Maestro Editor is using the “keyboardmaestroURL scheme, which supports the following formats:

Example Editor URL Command Description
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.

Triggers

You can trigger a macro (that is Active and Enabled) using the “kmtriggerURL scheme.
The URL uses this format:
kmtrigger://macro=<Macro Name or UUID>[&<Trigger Value>]

where

  • <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.

You can easily get the Trigger URL by clicking on the “Or by Script” Trigger option, and selecting “Or by URL”.

Example Trigger URL Command Description
kmtrigger://macro=Your%20Macro%20Name Using the Macro Name
kmtrigger://macro=224AA8CB-07EB-4C92-8201-68FED82B6E9F Using the Macro UUID
kmtrigger://macro=Your%20Macro%20Name&value=Your%20Trigger%20Value Using Macro Name with Trigger Value
kmtrigger://macro=224AA8CB-07EB-4C92-8201-68FED82B6E9F&value=Your%20Trigger%20Value Using Macro UUID with Trigger Value

See also the Calculations and Tokens sections.

manual/Scripting.1503652849.txt.gz · Last modified: 2017/08/25 05:20 by peternlewis