Created
November 25, 2024 20:24
-
-
Save Strajk/fd07ae3071c2f5aca38086212b7c7537 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Name: Keyboard Maestro | |
| // Description: Run or edit a Keyboard Maestro macro | |
| // Author: Pavel 'Strajk' Dolecek | |
| // Twitter: @straaajk | |
| // Cache: true | |
| import "@johnlindquist/kit" | |
| import plist from 'plist' | |
| import dedent from "dedent" | |
| export type TMacro = Partial<{ | |
| name: string | |
| uid: string | |
| active: boolean | |
| created: number | |
| used: number | |
| enabled: boolean | |
| lastused: number | |
| modified: number | |
| saved: number | |
| sort: string | |
| triggers: Array<Partial<{ description: string, short: string, type: string }>> | |
| }> | |
| export type TMacroGroup = Partial<{ | |
| uid: string | |
| enabled: boolean | |
| name: string | |
| sort: string | |
| macros: TMacro[] | |
| }> | |
| let kmMacrosRaw = await applescript(dedent` | |
| tell application "Keyboard Maestro Engine" | |
| getmacros with asstring | |
| end tell | |
| `) | |
| let kmMacrosParsed = plist.parse(kmMacrosRaw) as TMacroGroup[] | |
| const choices = kmMacrosParsed.flatMap(group => group.macros.map(macro => ({ | |
| name: `${group.name} - ${macro.name}`, | |
| value: macro.uid | |
| }))) | |
| let chosen = await arg({ | |
| placeholder: "Choose a macro", | |
| choices: choices, | |
| actions: [ | |
| { | |
| shortcut: `${cmd}+e`, | |
| name: "Edit Macro", | |
| visible: true, | |
| flag: "edit" | |
| } | |
| ] | |
| }) | |
| if (flag.edit) { // Note that flag is a global | |
| await applescript(dedent` | |
| tell application "Keyboard | |
| Maestro" | |
| editMacro "${chosen}" | |
| activate | |
| end tell | |
| `) | |
| } else { | |
| await applescript(dedent` | |
| tell application "Keyboard Maestro Engine" | |
| do script "${chosen}" | |
| end tell | |
| `) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment