Created
March 17, 2016 05:45
-
-
Save also/5096d3342e32eec6cddc to your computer and use it in GitHub Desktop.
Get HTML from OS X RTF clipboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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