Create rtf from clipboard with text and images

I’m developing a workflow where a web page, or piece of it, is put into the clipboard, and a new DT record is made from that with Applescript.

If I create a record, open it and then paste the clipboard, the text is there, in the same font, images and all.
But if I create the record as follows:

set newRec to create record with {name:theName, URL:theURL, type:rtf, content:the clipboard, tags:theTag, comment:""}

The record contains only the text, in a generic font, without the images. Changing the type to “rtfd” and/or assigning the clipboard to the property “text” instead of “content” gives the same result.

What am I missing?

This isn’t a DEVONtech issue but one of AppleScript and how the clipboard works. Copy the text and images then check Edit > Show Clipboard in the Finder. See any images? I’d guess not and likely what you’re seeing in the new document in DEVONthink.

Here’s something fun to use after copying your text and images…

return clipboard info

… and though I generally don’t use Script Debugger as my daily driver, it does show these results in a clear way…

Notice item 1 of each item. Those are the types of data on the clipboard, in this case how Safari handles the copied data.

Now, a little further researching shows you how to (sometimes) convert clipboard contents, though it’s really just extracting one particular clipboard element. So…

tell application id "DNtp"
	set r to (get the clipboard as «class rtfd»)
end tell

… yields this…

:flushed: :open_mouth:
But the important bit of data is right at the beginning: data rtfd.

A llittle further searching on our forums and the AppleScript dictionary shows you there is a data property for records…

So…

tell application id "DNtp"
	-- 1. Get the clipboard data as RTFD data
	set clipDataAsRTFD to (get the clipboard as «class rtfd»)
	--2. Create a RTFD document with empty (but required) content.
	set newRecord to create record with {name:"clipboard", type:rtfd, content:""} in current group
	-- 3. Set the data of the new document to the RTFD data
	set data of newRecord to clipDataAsRTFD
end tell

Therefore…


becomes…

But again, this depends on what the app you’re copying from puts on the clipboard. I would not expect these results from every app you want to try this with. Safari is just well behaved here. As a perfect example (and not unexpectedly), Firefox does not work as it doesn’t put rtfd data on the clipboard.

3 Likes

This lovely little app, is useful for understanding the contents on a clipboard: Clipboard Viewer

1 Like

This seems to be very similar to what the Take Rich Note service does.

That is fun and interesting, thanks.
Not pursuing this path then, as (for other good reasons) I’m switching from Safari to Firefox.
The Take Rich Note service does not appear in my Firefox menu. And in Safari it turned out to be quite useless.
So right now my workflow is to create a new record from the web page, and later open the record, tell it to launch the page, put it in reader mode (which in Firebox works better than Safari for some sites) and copy it over. Some Keyboard Maestro shortcuts make it a mouseless operation, even for multiple files, which is a must for me. There may be more elegant ways to do this, but it works.

It’s only available in apps providing rich text, e.g. Safari, Mail, TextEdit. But it doesn’t use the Sorter.

Good to know! Thanks for explaining all this.