05-03-26

Keyboard Maestro: App & Keystroke Automation

Automate application launches and keystroke sequences with a Lua script for your hardware input device. This script allows you to chain actions together, such as opening apps and typing text, for effi

rebindlua scriptautomationkeyboard maestroapp launchkeystrokesscript

Question

Keyboard maestro: chain apps and keystrokes together

Rebind AI

Keyboard Maestro: Chain Apps & Keystrokes

luau
-- rebind: min_sdk=0.1.0
-- rebind: name=Keyboard Maestro
-- rebind: description=Chain application launches and keystroke sequences

local cfg = UI.Schema({
    hotkey = UI.Keybind("F12", { label = "Maestro Hotkey" }),
    enabled = UI.Toggle(true, { label = "Enable" }),
})

-- Define chains: each entry launches an app and/or runs keystrokes
local chains = {
    {
        name = "Email Workflow",
        hotkey = "F9",
        steps = {
            { action = "app", path = "explorer shell:appsFolder\\Microsoft.OutlookforWindows_8wekyb3d8bbwe!App", wait = 3000 },
            { action = "keys", sequence = "LCtrl+N" },
            { action = "wait", ms = 500 },
            { action = "type", text = "Subject: Report\n\nHello,\n\nPlease find the report attached.\n\nBest regards" },
        },
    },
    {
        name = "Browser Search",
        hotkey = "F10",
        steps = {
            { action = "app", path = "chrome.exe", wait = 2000 },
            { action = "keys", sequence = "LCtrl+T" },
            { action = "wait", ms = 300 },
            { action = "type", text = "rebind keyboard automation" },
            { action = "keys", sequence = "Enter" },
        },
    },
    {
        name = "Code Session",
        hotkey = "F11",
        steps = {
            { action = "app", path = "code.exe", wait = 3000 },
            { action = "keys", sequence = "LCtrl+K LCtrl+O" },
            { action = "wait", ms = 1000 },
            { action = "type", text = "C:\\Projects" },
            { action = "keys", sequence = "Enter" },
        },
    },
}

local function executeChain(chain)
    Log.Info("Executing chain: " .. chain.name)

    Run(function()
        for _, step in ipairs(chain.steps) do
            if step.action == "app" then
                System.Exec('start "" "' .. step.path .. '"')
                if step.wait then
                    Sleep(step.wait)
                end

            elseif step.action == "keys" then
                -- Parse multiple keys separated by space
                for key in step.sequence:gmatch("%S+") do
                    HID.Press(key)
                    Sleep(50)
                end

            elseif step.action == "type" then
                HID.Type(step.text, 20)

            elseif step.action == "wait" then
                Sleep(step.wait or 500)
            end
        end

        UI.Notify("Chain complete: " .. chain.name, "success")
    end)
end

-- Register hotkeys for each chain
function OnStart()
    for _, chain in ipairs(chains) do
        local bindHotkey = chain.hotkey
        Bind(bindHotkey, function()
            if not cfg.enabled then
                return true
            end
            executeChain(chain)
            return false
        end)
    end

    Log.Info("Keyboard Maestro ready")
    for _, chain in ipairs(chains) do
        Log.Info("  " .. chain.hotkey .. " → " .. chain.name)
    end
end

function OnStop()
    Audio.StopAll()
end

function OnBlur()
    Audio.StopAll()
end

How It Works

Chains are sequences of steps that combine:

  • app — Launch an application with optional wait time
  • keys — Send keyboard shortcuts (space-separated, e.g. "LCtrl+N" or "F1 LCtrl+S")
  • type — Type multi-line text with \n for newlines
  • wait — Pause between steps

Each chain is bound to a hotkey (F9-F11 in this example). Press the hotkey and the entire sequence executes in order.

Example chains included:

  1. Email Workflow (F9) — Opens Outlook, creates new email, types a formatted message
  2. Browser Search (F10) — Launches Chrome, opens new tab, searches a term
  3. Code Session (F11) — Opens VS Code, opens a folder

You can add as many chains as you want by extending the chains table. Each step runs sequentially with full control over timing.

Customization

Replace path values with your actual executables:

  • explorer shell:appsFolder\... for Windows Store apps (get the path from Start menu shortcuts)
  • Direct exe paths like C:\Program Files\Google\Chrome\chrome.exe
  • Or just the app name if it's in your PATH: "notepad"

Adjust wait times based on how long apps take to launch on your system.

Add more steps to any chain, or create entirely new chains for your workflows.


Learn more: