Created
March 22, 2024 13:11
-
-
Save nicoandmee/781e5dbd1b96a87dc897f67ed48db857 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env -S npx zx@dev --install --experimental --shell=/bin/zsh --quiet | |
import 'zx/globals'; | |
import fs from 'fs-extra'; | |
import { json2ts } from 'json-ts'; | |
import plist from 'plist'; | |
import _ from 'lodash'; | |
import { runAppleScript } from 'run-applescript'; | |
import {execa} from 'execa' | |
const macrosFile = `/Users/nico/dotfiles/.config/karabiner/ts/src/utils/.kmacros`; | |
const typesFile = `/Users/nico/dotfiles/.config/karabiner/ts/src/utils/.kmacros.d.ts`; | |
const homeDir = os.homedir(); | |
// export type TypeMacro = 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: triggerType }>> }>; | |
// export type TypeMacroGroup = Partial<{ uid: string; enabled: boolean; name: string; sort: string; macros: TypeMacro[] }>; | |
// export type triggerType = 'Typed String Trigger' | 'Hot Key Trigger'; | |
const logInfo = (msg) => console.log(chalk.green(msg)); | |
const logError = (msg) => console.log(chalk.red(msg)); | |
const logDebug = (msg) => console.log(chalk.blue(msg)); | |
const script = ` | |
tell application "Keyboard Maestro Engine" | |
set macroList to getmacros with asstring | |
end tell | |
return macroList | |
`; | |
const osascript = async (script) => { | |
const {stdout} = await execa('osascript', ['-e', script]); | |
return stdout; | |
} | |
const scriptResult = await osascript(script); | |
console.debug(`[listMacros] scriptResult: ${scriptResult}`); | |
const data = plist.parse(scriptResult); | |
console.dir(data, { depth: 3 }); | |
// only enabled macros | |
let filteredData = data.map((group) => { | |
const macros = group.macros?.filter((macro) => macro.enabled); | |
return { ...group, macros }; | |
}); | |
// sort by group name | |
// filteredData = _.sortBy(filteredData, 'name'); | |
await fs.writeJSON(macrosFile, data, { spaces: 2 }); | |
// todo: generate ts defs? | |
// const ts = json2ts(data, { rootName: '', flow: false, prefix: '' }); | |
// console.debug(`[listMacros] types: ${ts}`); | |
// await fs.writeFile(typesFile, ts, 'utf-8'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment