Skip to content

Instantly share code, notes, and snippets.

@also
Created March 17, 2016 05:45
Show Gist options
  • Save also/5096d3342e32eec6cddc to your computer and use it in GitHub Desktop.
Save also/5096d3342e32eec6cddc to your computer and use it in GitHub Desktop.
Get HTML from OS X RTF clipboard
var $ = require('nodobjc');
$.framework('AppKit');
function getNSData(data) {
var byteBuffer = new Buffer(data('length'));
data('getBytes', byteBuffer, 'length', byteBuffer.length);
return byteBuffer;
}
var toHtmlOpts = $.NSDictionary(
'dictionaryWithObject', $.NSHTMLTextDocumentType,
'forKey', $.NSDocumentTypeDocumentAttribute);
function rtfToHtml(rtfData) {
var attrString = $.NSAttributedString('alloc')(
'initWithRTF', rtfData,
'documentAttributes', null);
var htmlData = attrString(
'dataFromRange', $.NSMakeRange(0, attrString('length')),
'documentAttributes', toHtmlOpts,
'error', null);
return getNSData(htmlData);
}
function getRtfHtml(pboard) {
var rtfData = pboard('dataForType', $.NSString('stringWithUTF8String', 'public.rtf'));
if (!rtfData) {
console.error('No public.rtf data in pasteboard');
process.exit(1);
}
console.log(rtfToHtml(rtfData).toString('utf8'));
}
getRtfHtml($.NSPasteboard('generalPasteboard'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment