How can I save all tabs as PDFs?

I’m still new to DEVONagent Pro, so pardon me if this has an obvious solution that I missed.

I’m interacting with a site that uses session cookies and dynamically generated pages. One of the pages is an index page with links to a bunch of other pages. I used DEVONagent to open this index page, selected a subset of the links in DEVONagent’s right-hand side panel (from the “all links” list), and then used Open in New Tab to open the subset in separate tabs. So far so great: the tabs contain full pages, with the dynamically-generated content as hoped. Yay!

Now I’d like to save all those tab contents as PDF files (meaning, saving the final rendered pages as one-page PDF files), either to disk or to DEVONthink Pro (either destination would be equivalent for my purposes). How can I do that?

I can save individual tabs as PDFs (by selecting each tab in turn, and then either using the menu item File ➜ Save As… or using options in the Data ➜ Add to DEVONthink submenu), but I haven’t found the equivalent of “save all tabs”. I can handle a little bit of AppleScripting if necessary, if someone could help get me started.

There is no Save All Tabs command. (It’s actually the first request I can remember).

Here is an example…

tell application id "DNag"
	start downloads true
	repeat with theTab in (tabs of browser 1)
		set theURL to URL of theTab
		if thisURL ends with ".pdf" then
			add download theURL -- Downloads to the "Downloads" directory by default. This can be changed in DEVONagent's Preferences > General.
		end if
	end repeat
end tell

DEVONthink installs an “Add tabs to DEVONthink” script for the Scripts menu extra while DEVONagent is active.

Unfortunately, the “Add tabs to DEVONthink” script only adds bookmarks for the locations; what I meant was that I wanted to save snapshots of the final, rendered pages. (Thus the reason why I’m trying to save one-page PDFs of the pages, rather than some other format.) I’ll edit the text of my question to clarify this.

My script downloads the PDFs loaded in tabs in a DEVONagent browser window.

Thanks for the script. It unfortunately doesn’t run straight off; the variable “thisURL” doesn’t exist, but even when I change it to “theURL” (assuming it’s just a typo), the result still doesn’t seem to do anything in my environment. I’m currently trying to debug it.

(Reading the script, however, it seems like the script would only download things if the URLs in question are PDF files, which they are not if they are just regular web pages on a site. But maybe I misunderstand what’s going on.)

Following up on the above: I think the script needs to be something along the following lines, but I’m stuck trying to make it save the PDF data. The “save” line is wrong, but I haven’t figured out the correct equivalent command and syntax. Maybe you know?

tell application id "DNag"
	repeat with theTab in (tabs of browser 1)
		set theTitle to name of theTab
		set contentAsPDF to PDF of theTab
		save contentAsPDF in file theTitle
	end repeat
end tell

Yes, that was a typo.
Yes, it’s downloading PDFs, as I understood your request to be.

Right, that’s what I was afraid it was doing. I’m sorry if my question was unclear, and thank you for your replying and trying to solve it. I tried to clarify what I’m actually trying to do, and the script I just posted may help make it more clear.

I’ve almost got it now:

tell application id "DNag"
	repeat with theTab in (tabs of browser 1)
		set theTitle to name of theTab
		set contentAsPDF to PDF of theTab
		set theFile to open for access (the POSIX path of (path to desktop)) & theTitle & ".pdf" with write permission
		write contentAsPDF to theFile
		close access theFile
	end repeat
end tell

This still fails because the file name it creates can have illegal characters (like “/”), but now I know what to do. The file names need to be filtered a bit but I can solve that, as well as make other things a little nicer like configuring the destination directory instead of hardwiring it.

For anyone who may find it useful in the future, I now have a reasonable working version of an AppleScript program that saves PDF versions of the pages currently open in the tabs of a DEVONagent Pro window. The script is open-source (BSD 3-clause license) and freely available from GitHub. The operation is simple: with a DEVONagent window open, run the AppleScript; it will prompt for a destination folder on your computer, then iterate over each tab, convert the page to PDF, and save it in a file named after the title of the web page. The file name is modified to remove characters that are problematic on some other operating systems, so that the files it creates are more portable to non-macOS file systems.

If you use it and find problems or have suggestions, please do let me know (preferably via the issue tracker in the repository, but other ways would work too).

1 Like