macOS clipboard image paste: AppleScript never compiles, osascript path is dead code
Summary
The macOS branch of clipboard_image() in crates/jcode-tui/src/tui/app/helpers.rs has never worked. The AppleScript it builds fails to compile, so the entire osascript path is dead code and every image paste on macOS silently falls through to the arboard fallback.
I have a fix + regression tests already working on my fork (aphelion31), but pull_request_creation_policy on this repo is collaborators_only, so POST /repos/1jehuang/jcode/pulls returns aphelion31 does not have the correct permissions to execute CreatePullRequest. Filing this as an issue instead, per CONTRIBUTING.md. Branch is public if you want the diff: https://github.com/aphelion31/jcode/tree/fix/macos-clipboard-image-applescript
Environment
- macOS 15, aarch64
- jcode v0.84.0
Root cause
Three defects in the same string literal:
1. \" escapes inside a Rust raw string. The script is built with format!(r#"use framework \"AppKit\" ..."#). In a raw string the backslashes are literal, so osascript -e receives use framework \"AppKit\" and dies on line 1.
$ osascript -l AppleScript -e '<current script, verbatim>'
4:13: syntax error: Can't get framework. (-1728)
2. Unquoted properties: label. properties is a plural class name in AppleScript's grammar, so an unquoted properties: label in an ObjC-style message send aborts compilation before any statement runs:
$ osascript -l AppleScript -e 'use framework "AppKit"
set r to current application'"'"'s NSBitmapImageRep
set x to r'"'"'s representationUsingType:1 properties:(missing value)'
110:120: syntax error: Expected end of line, etc. but found plural class name. (-2741)
It has to be escaped as |properties|:.
3. missing value for the properties argument. representationUsingType:properties: requires a real dictionary and rejects missing value. An empty NSDictionary works.
There are also no nil guards on imgList / tiffData / bitmapRep / pngData, which would throw at runtime had the script ever compiled.
Verification of the fix
With the escapes removed, |properties|: escaped, and an empty NSDictionary passed, the script compiles and actually round-trips a real image:
$ sips -s format png /System/Library/CoreServices/DefaultDesktop.heic --out /tmp/src.png
$ osascript -e 'set the clipboard to (read (POSIX file "/tmp/src.png") as «class PNGf»)'
$ osascript -l AppleScript -e '<fixed script>'
ok
$ file /tmp/jcode_clipboard.png
PNG image data, 3840 x 2160, 8-bit/color RGB, non-interlaced
Empty clipboard correctly returns none instead of throwing.
What my branch does
- Extracts the script into
macos_clipboard_png_script(temp_path)so it is testable without a clipboard fixture. - Fixes the three defects above, adds the four nil guards.
- Adds two regression tests, both red against the current script and green against the fixed one:
macos_clipboard_script_escapes_the_properties_label— asserts the source shape (escaped label, nomissing value, no stray backslash escapes).macos_clipboard_script_compiles_and_runs_under_osascript— actually invokesosascriptso a future rewrite that changes the string shape is still covered. An empty clipboard is a valid fixture: it exercises the full parse plus theelsebranch.
$ cargo test -p jcode-tui macos_clipboard_script
test tui::app::helpers::helpers_tests::macos_clipboard_script_escapes_the_properties_label ... ok
test tui::app::helpers::helpers_tests::macos_clipboard_script_compiles_and_runs_under_osascript ... ok
test result: ok. 2 passed; 0 failed
Tradeoff worth flagging
Because arboard masked this, user-visible behavior barely changes on a normal Mac. The value is that the intended higher-fidelity path (NSPasteboard -> native PNG, no RGBA re-encode) becomes reachable, and the dead branch stops being a trap for the next person who edits it. Reasonable to also just delete the osascript branch and rely on arboard, if that is the direction you prefer.
Happy to open a PR if you enable fork PRs, or you are welcome to take the diff and rewrite it as you see fit.
Closing as a duplicate of #1195, which I filed a day earlier with the same root cause and a better repro (it reads the broken script straight out of the shipped binary with strings, no build required).
Sorry for the noise. Everything here is already covered there, including the branch link.
One detail worth carrying over: the fix is now verified end to end against a real image rather than just an empty clipboard.
$ sips -s format png /System/Library/CoreServices/DefaultDesktop.heic --out /tmp/src.png
$ osascript -e 'set the clipboard to (read (POSIX file "/tmp/src.png") as «class PNGf»)'
$ osascript -l AppleScript -e '<fixed script>'
ok
$ file /tmp/jcode_clipboard.png
PNG image data, 3840 x 2160, 8-bit/color RGB, non-interlaced
Empty clipboard still returns none instead of throwing, so the else branch is covered too.