Paste Clipboard vs Copy and Paste

I am automating capture of a selection of something like a portion of a web page and converting it to a PDF.

The easiest way is just to use the paste clipboard command:

tell application id “DNtp”
set clipboardRecord to paste clipboard
convert record clipboardRecord to PDF document
end tell

The results of that are often (perhaps even typically) disastrous. In the example below, for example, it gives me a 6 MB output file with two copies of the same set of images.

The other way is to create a new html page, open it, and paste the clipboard in by hand with ⌘V, then convert that to a PDF. The result is a nice little 300KB file that gets it right.


Screen capture of the original page from the NYT:

Results from using “paste clipboard” in AppleScript:

Same thing, but creating an HTML document, pasting by hand, and converting to PDF.

I don’t suppose anybody understands the ins and outs of the clipboard and scripting DT3 well enough to get results like the second example without having to actually open a window and script the interface to do the paste.

What about the Mac Print to PDF command? That’s what I would use.

1 Like

If you’re using Safari you could drag the selection to DEVONthink’s icon in the dock. That creates a clean webarchive. When you’re done with clipping select all webarchives and use Data > Convert > to paginated PDF.

You might want to take a look at this Smart Rule script if you want to create the webarchives in a group structure. But it’s just a workaroud, see below.

@cgrunenberg would it be possible to add a preference that toggles the resulting record type when dragging from Safari into a window? Currently it creates RTF(D), would be awesome if we had the choice to change that to webarchive which gives the best results over here.

A web archive of the selection? Last time I tested this the results were actually quite poor as the web archive provided by Safari for selection didn’t render as expected quite often.

Yes. The same result as if the Safari selection were dragged on the dock icon but instead of using the user’s default location create the webarchive directly in the group it was dragged to. Or to put it the other way round: The same behaviour as it’s currently when dragging into a window but create a webarchive instead of RTF. As a preference, of course.

Hmm, I guess the RTF result was even worse then? If I don’t want the whole page I’m almost always clipping via dragging to the dock, with very good results. Might be that it doesn’t always look exactly like the source but it gives nice clutter free results. I’m more interested in getting the best possible result than getting the exact same as most of the time that’s not really necessary.

This is what I see with a very small test. RTFs created by dragging into a window, webarchives created by dragging onto the dock.

1 - RTF

1 - Webarchive

2 - RTF

2 - Webarchive

3 - RTF

3 - Webarchive

I wouldn’t recommend web archives anymore actually, they’re only supported on Apple’s platforms and Apple already deprecated them.

Converting them to paginated PDF works fine.

RTF is not really an alternative so please don‘t remove the ability to create webarchives.

This isn’t planned, at least as long as macOS/iOS support them.

1 Like

BTW: Another possibility is to enable the Capture Web Archive service in the preferences and to optionally assign a different shortcut.

1 Like

I don’t use services at all for capturing as it’s necessary to trigger them via the menu bar once after Safari was launched before the assigned shortcut works (at least with macOS Mojave), and it’s not possible to “activate” the menu on Safari launch via Keyboard Maestro because the menu is only available if text is selected.

It would indeed be a far superior way to capture, unfortunately I never found a way to trigger services programmatically. Searching again I found NSPerformService but that seems to need the clipboard. Any way to trigger a DEVONthink service programmatically?

The only possibility is indeed NSPerformService and e.g. DEVONagent uses this to add data to DEVONthink.

Any way to copy the Safari selection without using UI scripting?

Sending just the shortcut Cmd-C to Safari is usually quite reliable.

1 Like

I don’t use services at all for capturing as it’s necessary to trigger them via the menu bar once after Safari was launched before the assigned shortcut works (at least with macOS Mojave),

How is this an issue when the shortcut persists after doing this?

Pressing a shortcut should execute the expected action, period. If this doesn’t happen it’s unexpected behaviour. It’s simply confusing until I figure out again that I have to manually activate the menu.

Turns out it’s easy to work around.

  1. Create a Dummy Service in whose only purpose is to be a menu item.

  1. Create a macro in Keyboard Maestro that invokes the Dummy Service after launch.

Afterwards Safari’s Services menu responds to shortcuts.

Seems like more steps were required than merely opening the menu :wink:

I really tried but forgot it every time.

@jhester just realized that I hijacked this thread, sorry! Did you find a solution?