Getting HTML and RTF strings from the clipboard

So to replace the rtf clipboard with HTML text, ready for pasting, perhaps something like this:

(function () {
    'use strict';

    ObjC.import('AppKit');

    function pboardUnpacked(strType) {
        return ObjC.unwrap(
            $.NSPasteboard.generalPasteboard.stringForType(
                strType
            )
        );
    }


    var strRTF = pboardUnpacked('public.rtf');

    if (strRTF) {
        var a = Application.currentApplication(),
            sa = (a.includeStandardAdditions = true, a);

        sa.setTheClipboardTo(strRTF);

        sa.doShellScript('pbpaste | textutil -format rtf -convert html -stdin -stdout | pbcopy -Prefer html')

        return sa.theClipboard();
    }
})();

Which on my system takes a copied textEdit selection like:

to:



  
  
  
  
  
  
    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 48.0px Helvetica}
    span.s1 {color: #e32400}
    span.s2 {font: 48.0px 'Ezra SIL'}
  


Big red text

2 Likes