Created
April 17, 2024 17:56
-
-
Save mxstbr/a2c43584734fdd6e5c55fe108ed7c153 to your computer and use it in GitHub Desktop.
Raycast snippets > iOS/MacOS text replacements sync Raycast command
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
import { useEffect } from "react"; | |
import { Detail, open } from "@raycast/api"; | |
import { useExec } from "@raycast/utils"; | |
import Papa from "papaparse"; | |
type Row = { | |
// "On my way!" | |
ZPHRASE: string; | |
// omw | |
ZSHORTCUT: string; | |
ZWASDELETED: "0" | "1"; | |
// Unknown | |
Z_PK: string; | |
Z_ENT: string; | |
Z_OPT: string; | |
ZNEEDSSAVETOCLOUD: string; | |
ZTIMESTAMP: string; | |
ZUNIQUENAME: string; // ID | |
ZREMOTERECORDINFO: string; | |
}; | |
export default function Command() { | |
// Load MacOS text replacement entries from local db | |
const { isLoading, data } = useExec( | |
"/usr/bin/sqlite3 -csv ~/Library/KeyboardServices/TextReplacements.db -header 'SELECT * FROM ZTEXTREPLACEMENTENTRY'", | |
{ | |
shell: true, | |
} | |
); | |
// Once it's loaded, parse the CSV result into JSON rows | |
const rows = data | |
? Papa.parse<Row>(data, { header: true }).data.filter( | |
(row) => row.ZWASDELETED !== "1" | |
) | |
: null; | |
useEffect(() => { | |
if (!rows) return; | |
const params = rows | |
.map( | |
(row) => | |
`snippet=${encodeURIComponent( | |
JSON.stringify({ | |
name: row.ZSHORTCUT, | |
text: row.ZPHRASE, | |
keyword: row.ZSHORTCUT, | |
}) | |
)}` | |
) | |
.join("&"); | |
open(`raycast://snippets/import?${params}`); | |
}, [JSON.stringify(rows)]); | |
// Just a placeholder view so it renders something | |
return <Detail isLoading={isLoading} markdown="# You can close this now!" />; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment