Generating .kmmacros files programmatically with Python/plistlib — gotchas

If you generate Keyboard Maestro macros programmatically (e.g. with Python's plistlib), here are some non-obvious things I learned the hard way:

1. ExecuteShellScript needs UseText: True Without this key, KM shows the action as empty even though the script text is present in the plist. Required keys for an inline shell script:

{    'MacroActionType': 'ExecuteShellScript',
    'UseText': True,
    'Source': 'Nothing',
    'Text': '... your script ...',
    'TrimResults': True,
    'TrimResultsNew': True,
    'HonourFailureSettings': True,
    'IncludeStdErr': False,
    'IncludedVariables': ['9999'],  # pass all KM variables to the script
    'Path': '',    
    'Variable': 'MyResultVariable',  # stores stdout directly — no separate Set Variable needed
}

2. Cancel This Macro: use CancelThisMacro, not CancelThis Action: 'CancelThis' looks right but KM silently imports it as "Cancel All Macros". The correct value is Action: 'CancelThisMacro' — that's what KM stores internally.

3. Numeric equality in IfThenElse conditions For "Variable equals 0" (numeric), the correct structure is:

{'ConditionType': 'Variable', 'Variable': 'MyVar', 'VariableConditionType': 'Equal', 'VariableValue': '0'}

Not VariableOperator: 'Is' — that's for string matching.

3 Likes