Skip to content

Commit 551aa13

Browse files
lacherogwuclaude
andcommitted
fix(tui): bound the pasteboard file read and skip it for text pastes
Only read Finder file URLs when the clipboard holds a bitmap, since a Finder copy always reads as its file icon. Time out a stuck osascript after 1s and treat any failure as no files so it never breaks or blocks the paste. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 638cc42 commit 551aa13

2 files changed

Lines changed: 40 additions & 17 deletions

File tree

‎packages/tui/src/clipboard.ts‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ const PASTEBOARD_FILES_SCRIPT = `ObjC.import("AppKit"); const urls = $.NSPastebo
2727
// file icon and the bare file names.
2828
export async function readPasteboardFiles() {
2929
if (process.platform !== "darwin") return []
30-
const proc = Bun.spawn(["osascript", "-l", "JavaScript", "-e", PASTEBOARD_FILES_SCRIPT], {
31-
stdout: "pipe",
32-
stderr: "ignore",
33-
})
34-
const [output, code] = await Promise.all([new Response(proc.stdout).text(), proc.exited])
35-
if (code !== 0) return []
36-
return output.split("\n").filter(Boolean)
30+
const read = async () => {
31+
const proc = Bun.spawn(["osascript", "-l", "JavaScript", "-e", PASTEBOARD_FILES_SCRIPT], {
32+
stdout: "pipe",
33+
stderr: "ignore",
34+
timeout: 1000,
35+
})
36+
const [output, code] = await Promise.all([new Response(proc.stdout).text(), proc.exited])
37+
return code === 0 ? output.split("\n").filter(Boolean) : []
38+
}
39+
// A failed or stuck osascript must not break or block the paste.
40+
return read().catch(() => [])
3741
}
3842

3943
export function createClipboardAdapter(
@@ -42,16 +46,16 @@ export function createClipboardAdapter(
4246
): OwnedClipboardService {
4347
return {
4448
async read(): Promise<ClipboardContent | undefined> {
45-
const [files, result] = await Promise.all([
46-
pasteboardFiles(),
47-
clipboard.read({
48-
preferredTypes: ["image/png", "text/plain"],
49-
selection: "clipboard",
50-
}),
51-
])
52-
// Hand copied files to the paste path resolver instead of their icon bitmap.
53-
if (files.length > 0)
54-
return { data: files.map((file) => pathToFileURL(file).href).join("\n"), mime: "text/plain" }
49+
const result = await clipboard.read({
50+
preferredTypes: ["image/png", "text/plain"],
51+
selection: "clipboard",
52+
})
53+
// A Finder copy reads as its file icon bitmap; hand the copied files to the paste path resolver instead.
54+
if (result.status === "read" && result.representation.mimeType === "image/png") {
55+
const files = await pasteboardFiles()
56+
if (files.length > 0)
57+
return { data: files.map((file) => pathToFileURL(file).href).join("\n"), mime: "text/plain" }
58+
}
5559
if (result.status !== "read") {
5660
if (result.status === "failed") throw result.error
5761
if (result.status === "timed-out") throw new Error("Clipboard read timed out")

‎packages/tui/test/clipboard.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ test("reads files copied in Finder as file URLs instead of their icon bitmap", a
8383
})
8484
})
8585

86+
test("reads copied files only when the clipboard holds a bitmap", async () => {
87+
let reads = 0
88+
const clipboard = createClipboardAdapter(
89+
coreClipboard({
90+
read: {
91+
status: "read",
92+
representation: { mimeType: "text/plain", bytes: new TextEncoder().encode("hello") },
93+
},
94+
}),
95+
async () => {
96+
reads++
97+
return ["/tmp/one.png"]
98+
},
99+
)
100+
101+
expect(await clipboard.read()).toEqual({ data: "hello", mime: "text/plain" })
102+
expect(reads).toBe(0)
103+
})
104+
86105
test("uses all available routes but skips the process host remotely", async () => {
87106
const writes = { host: 0, terminal: 0 }
88107
const host: HostClipboardService = {

0 commit comments

Comments
 (0)