Preserve rich formatting when restoring selected text

This commit is contained in:
muena
2026-09-27 10:23:37 +02:00
parent 94806aa049
commit ec1e2d27e0
6 changed files with 463 additions and 56 deletions

27
src/rich-clipboard.ts Normal file
View File

@@ -0,0 +1,27 @@
import { execFile } from "node:child_process";
import { promisify } from "node:util";
const execFileAsync = promisify(execFile);
// Raycast 2.0 Beta writes only plain text for Clipboard.copy({ html, text }).
// Set both pasteboard types directly; environment variables avoid shell quoting.
const RICH_COPY_SCRIPT = [
"ObjC.import('AppKit');",
"const env = $.NSProcessInfo.processInfo.environment;",
"const html = env.objectForKey('VELUM_HTML');",
"const text = env.objectForKey('VELUM_TEXT');",
"const pb = $.NSPasteboard.generalPasteboard;",
"pb.clearContents;",
"pb.setStringForType(html, 'public.html');",
"pb.setStringForType(text, 'public.utf8-plain-text');",
].join("\n");
export async function copyRichText(html: string, text: string): Promise<void> {
await execFileAsync(
"osascript",
["-l", "JavaScript", "-e", RICH_COPY_SCRIPT],
{
env: { ...process.env, VELUM_HTML: html, VELUM_TEXT: text },
},
);
}