[FR] Copy HTML to the clipboard
NightMachinery opened this issue · 6 comments
See https://stackoverflow.com/questions/68937989/macos-copy-html-to-the-clipboard for more details. Basically, it would be great if we could copy HTML and then paste it as rich text in Chrome.
I agree! Should be reasonably straightforward (vs. that iOS example in your link) — read stdin as Data
, then setData on the general pasteboard with type="public.html".
Call it pbc.swift
, and copy over the pbv
-specific build targets in the Makefile, and I'd be happy to take a look at a PR :)
@chbrown I don't know any swift or obj-c, but trying it:
let pasteboard: NSPasteboard = .general
let dataTypeName : String = "public.html"
let dataType = NSPasteboard.PasteboardType(rawValue: dataTypeName)
pasteboard.setData("<b>hi</b>", forType: dataType)
It wants a Data
object, and does not accept "<b>hi</b>"
:
copy_as_html.swift:140:24: error: cannot convert value of type 'String' to expected argument type 'Data?'
pasteboard.setData("<b>hi</b>", forType: dataType)
Haha, I don't know Swift or Obj-C very well either :) but that's pretty much it, I think, just need to convert your String to Data. Referring to previous code I wrote that works (which is how I typically cope in Swift)... should look like this:
import Foundation // <-- this is important
/* ... */
let example = "<b>hi</b>"
let data = example.data(using: .utf8)!
pasteboard.clearContents() // <-- idk why but this is required prior to setData
pasteboard.setData(data, forType: dataType)
@chbrown This prototype worked (in TextEdit), but it is no different than my first shell function; It still pastes nothing in Chrome :(
#!/usr/bin/env swift
import Cocoa
import Foundation
let pasteboard: NSPasteboard = .general
let dataTypeName : String = "public.html"
let dataType = NSPasteboard.PasteboardType(rawValue: dataTypeName)
let example = "<b>hi</b>"
let data = example.data(using: .utf8)!
pasteboard.clearContents() // <-- idk why but this is required prior to setData
pasteboard.setData(data, forType: dataType)
exit(0)
I tried setting the plain text content as well, but then Chrome would paste the plain version:
#!/usr/bin/env swift
import Cocoa
import Foundation
let pasteboard: NSPasteboard = .general
///
let dataTypeName : String = "public.html"
let dataType = NSPasteboard.PasteboardType(rawValue: dataTypeName)
let html = "<b>hi</b>"
let data_html = html.data(using: .utf8)!
pasteboard.clearContents() // <-- idk why but this is required prior to setData
let dataTypeName_plain : String = "public.utf8-plain-text"
let dataType_plain = NSPasteboard.PasteboardType(rawValue: dataTypeName_plain)
let plain = "hi"
let data_plain = plain.data(using: .utf8)!
pasteboard.setData(data_plain, forType: dataType_plain)
pasteboard.setData(data_html, forType: dataType)
exit(0)
Feel free to close the issue. Or perhaps label it as help-wanted
? Idk. Thanks!
Further tests seem to indicate that I was mistaken in my assumption that pasting rich text is possible in most web apps on Chrome. Sorry.
Further tests seem to indicate that I was mistaken in my assumption that pasting rich text is possible in most web apps on Chrome. Sorry.
Hey! I'd still be interested in this.