on
CC-BY-NC-SA-4.0 Apache-2.0
1 mins
π€ This post includes some LLM-derived content π€
Getting access to rich-text data from the clipboard (on Mac)
This post's featured URL for sharing metadata is https://www.jvt.me/img/profile.jpg.
Related to my post from last year around doing this on Linux, I've been trying to do the same on my work Mac.
As ever, I find that Mac makes things so much more difficult than they need to be, but alas.
Although it would be nice to have a single command-line tool to invoke, it appears that osascript doesn't seem to be able to export the raw HTML, so with thanks to GPT-4.1 (GitHub Copilot), we worked out the following Swift code is what we need:
#!/usr/bin/env swift
import Cocoa
// Co-authored-by: GPT-4.1 (GitHub Copilot)
let pasteboard = NSPasteboard.general
if let html = pasteboard.string(forType: .html) {
print(html)
} else {
// as I prefer to see this fail
exit(1)
}
We can then see this in action:
# pbpaste only returns plaintext
$ pbpaste
renovate
# even with -Prefer=rtf
$ pbpaste -Prefer=rtf
renovate
$ pbpaste -Prefer rtf
renovate
# but our Swift script does what we want:
$ ~/bin/pbpaste_html
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body><a data-target="context-region-crumb.linkElement" data-analytics-event="{"category":"SiteHeaderComponent","action":"context_region_crumb","label":"renovate","screen_size":"full"}" href="https://github.com/renovatebot/renovate" aria-current="page" id="contextregion-repositorycrumb-renovate-link" data-view-component="true" class="AppHeader-context-item"><span data-target="context-region-crumb.labelElement" class="AppHeader-context-item-label ">renovate</span></a></body></html>