How to get a list of clipboard meta types

I'd like to get a list of all the types currently present in the clipboard. E.g.

['public.rtf', 'public.html', ...]

Is this possible? I've been hacking at this and trying to use JavaScript for Application but I'm not sure if I'm going in the right direction. I can apparently get a list of types but can't figure out how to write out what I have (I keep getting "can't convert type").

function getItems() {
  return $.NSPasteboard.generalPasteboard.pasteboardItems
}

// this gets SOMETHING but I don't know what - just that it's not undefined
const to = getItems().firstObject.types.firstObject;

Eventual goal: if there are objects of a certain type on the clipboard then don't blow it away. In my macro (used hundreds of times a day) insert-by-pasting is faster than insert-by-typing, but if there is OneNote data in the clipboard then there's no way to preserve it with insert-by-pasting. So, I want to use the fast version if any other app has pasted to the keyboard, but the slow version that preserves the clipboard fully if OneNote has pasted. So I need to see if a specific type ("com.microsoft.OneNote-2010-Internal") is in the general pasteboard.

(The KM script editing environment is pretty limited too - I'm definitely open if someone has a recommendation for an improved editor. E.g. no line numbers to compare to error messages, no autocompletion or mouseover type inspection.)

Have you taken a look at the JS code in this macro:

?

1 Like

Awesome, that works well enough for what I need! There are still a number of missing metadata items that I can see in Clipboard Viewer, but by checking the text of that result I can get what I need.

My specific solution:

In my case, this will nearly always distinguish between "copied OneNote page" and "copied anything else":

Doesn't contain: plain-text as propertyList
Contains: Calibri Light [from the title]
Contains: content=OneNote.File

The only false positive is where both a title and other content (date/body) are copied from inside a ON page, in this case the macro will still think a page was copied. But that's a rare situation. This is good enough to get a very good performance increase by speeding up the macro and not blowing away the clipboard in almost all cases where the pasted content doesn't include OneNote pages.

The shortest route to a complete list of types might, I think, be something like:

(() => {
    'use strict';

    ObjC.import('AppKit');

    return ObjC.deepUnwrap(
        $.NSPasteboard.generalPasteboard
        .pasteboardItems.js[0].types
    ).join('\n');
})();

The Clipboard Viewer macro limits itself to types with a textual representation, but if you copy something in a graphics app, for example, you might (from the snippet above) get a fuller list like:

OmniGraffle

com.omnigroup.OmniGraffle.GraphicType
com.adobe.pdf
public.png
public.tiff
dyn.ah62d4rv4gu8y24psrrbgc25pkvu1k2k

Sketch

com.bohemiancoding.sketch.v3
com.adobe.pdf
public.tiff
com.bohemiancoding.sketch.v3.tempfile
dyn.ah62d4rv4gu8y4y4xsv6023nukm10c6xenv61a3k
dyn.ah62d4rv4gu8y4y4ftb2g86xym72hk4ptr33zauxtqf3gkzd3sbwu

Pixelmator Pro

public.file-url
public.tiff
com.pixelmatorteam.pixelmator.sourceimage.position
com.pixelmatorteam.pixelmator.sourceimage.canvas.size
com.pixelmatorteam.pixelmator.documentid
2 Likes