Skip to content

Commit 0d21de3

Browse files
committed
fix(tui): speed up clipboard image paste
Reading a clipboard image on macOS went through AppleScript's `the clipboard as "PNGf"` coercion, which passes the whole image through an Apple event descriptor. That costs about 200ms per megabyte, so a 12MB screenshot took 2.5s. Spawning osascript is only 40ms of it. Read the pasteboard through the ObjC bridge in JXA instead. It returns the bytes already on the pasteboard, so the cost no longer scales with the size of the image. Closes anomalyco#38879
1 parent 9f69463 commit 0d21de3

1 file changed

Lines changed: 24 additions & 27 deletions

File tree

‎packages/tui/src/clipboard.ts‎

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import { execFile, spawn } from "node:child_process"
2-
import { readFile, rm } from "node:fs/promises"
3-
import { platform, release, tmpdir } from "node:os"
4-
import path from "node:path"
5-
import { promisify } from "node:util"
6-
7-
const exec = promisify(execFile)
1+
import { spawn } from "node:child_process"
2+
import { platform, release } from "node:os"
83

94
function command(command: string, args: string[] = [], input?: string) {
105
return new Promise<Buffer>((resolve, reject) => {
@@ -27,28 +22,30 @@ function writeOsc52(text: string) {
2722
process.stdout.write(process.env.TMUX ? sequence + passthrough : process.env.STY ? passthrough : sequence)
2823
}
2924

25+
// AppleScript's `the clipboard as "PNGf"` coercion costs time in proportion to the image size.
26+
const readImage = `ObjC.import('AppKit')
27+
function toPng(data) {
28+
return $.NSBitmapImageRep.imageRepWithData(data).representationUsingTypeProperties($.NSBitmapImageFileTypePNG, $())
29+
}
30+
function png() {
31+
const pb = $.NSPasteboard.generalPasteboard
32+
const direct = pb.dataForType('public.png')
33+
if (!direct.isNil()) return direct
34+
const tiff = pb.dataForType('public.tiff')
35+
if (!tiff.isNil()) return toPng(tiff)
36+
const jpeg = pb.dataForType('public.jpeg')
37+
if (jpeg.isNil()) return jpeg
38+
return toPng(jpeg)
39+
}
40+
const data = png()
41+
data.isNil() ? '' : data.base64EncodedStringWithOptions(0)`
42+
3043
export async function read() {
3144
if (platform() === "darwin") {
32-
const file = path.join(tmpdir(), "opencode-clipboard.png")
33-
try {
34-
await exec("osascript", [
35-
"-e",
36-
'set imageData to the clipboard as "PNGf"',
37-
"-e",
38-
`set fileRef to open for access POSIX file "${file}" with write permission`,
39-
"-e",
40-
"set eof fileRef to 0",
41-
"-e",
42-
"write imageData to fileRef",
43-
"-e",
44-
"close access fileRef",
45-
])
46-
return { data: (await readFile(file)).toString("base64"), mime: "image/png" }
47-
} catch {
48-
// Fall through to text clipboard.
49-
} finally {
50-
await rm(file, { force: true }).catch(() => {})
51-
}
45+
const image = await command("osascript", ["-l", "JavaScript", "-e", readImage])
46+
.then((output) => output.toString().trim())
47+
.catch(() => "")
48+
if (image) return { data: image, mime: "image/png" }
5249
}
5350

5451
if (platform() === "win32" || release().includes("WSL")) {

0 commit comments

Comments
 (0)