Scripting "Capture PDF" context menu option

I’m using web archive capturing from Safari extensively (shortcut: cmd+%). The benefit is in only capturing parts of a webpage. Unfortunately, most iOS apps cannot display webarchive format well. Therefore I decided to convert my webpage snippets into PDF format.

When displaying a web archive in DT there is a contextual menu option “Capture PDF” (“PDF erfassen”) which converts the displayed web archive to PDF. This is exactly what I was looking for. However, I cannot evoke this method by means of AppleScript. All methods seem to require or rely on the URL of the record and re-download the FULL record again.

Is this method scriptable at all?

You might find Robin’s concepts here useful. Uses the /System/Library/Printers/Libraries/convert utility.

Thanks for the info :slight_smile:

Does this mean that no direct scripting of the “Capture PDF” menu option is possible? Maybe someone of the DEVON support team could comment on this.

I have adjusted the linked script in the following way:

property pstrConvert : "/System/Library/Printers/Libraries/convert -f "
property pstrDesktop : POSIX path of (path to desktop)
tell application id "DNtp"
	repeat with oRec in (selection as list)
		if kind of oRec is "Web-Archiv" then
			set oRec to convert record oRec to rich
			set {strName, strPath, strFile} to {name, path, filename} of oRec
			if strPath ≠ "" then
				-- Clean the file name
				set strCleanFile to do shell script ("printf '%q' " & quoted form of strFile)
				-- Convert to PDF
				set strPDFPath to pstrDesktop & strCleanFile & ".pdf"
				do shell script pstrConvert & quoted form of strPath & " -o " & quoted form of strPDFPath
				set oPDF to import strPDFPath to (first parent of oRec)
				set name of oPDF to strName
				-- Delete temporary copy from desktop
				do shell script "rm " & quoted form of strPDFPath
			end if
		end if
	end repeat
end tell

With or without the intermediate step of converting to rtf first, unfortunately, I do get cropped PDFs. The text on the right of the webarchive is not included in the resulting PDF. Thus, the PDF is different from the one generated by the “Capture PDF” option. I could not find any man page for the convert utility, therefore I do not know if more options are available for convert like specifying paper size, margins etc.

After migrating to Lion I discovered that my beloved “Capture PDF” context menu option has dissapeared in DEVONthink. I now built my own solution which mimics the “Capture PDF” behavior. Additionally, it sets creation and modification dates to the ones of the original file. In case anyone might need easy conversion to PDF see the script below.

-- Convert documents to paginated PDFs
-- based on C.Grunenberg script from Dec 2008 (
-- tm.Jan2012

tell application id "com.devon-technologies.thinkpro2"
		set theSelection to the selection
		if theSelection is not {} then
			set frontMostWindow to think window 1
			repeat with theRecord in theSelection
				set theGroup to first parent of theRecord
				set theName to (name of theRecord) as string
				set theurl to (URL of theRecord) as string
				set cr_date to (creation date of theRecord)
				set mo_date to (modification date of theRecord)
				set theTags to (tags of theRecord)
				set theWindow to open window for record theRecord
				repeat while loading of theWindow
					delay 1
				end repeat
				set theData to paginated PDF of theWindow
				--set theData to PDF of theWindow -- single-page PDF
				set thePDF to create record with {name:theName, URL:theurl, type:PDF document, creation date:cr_date, tags:theTags} in theGroup
				set data of thePDF to theData
				set modification date of thePDF to mo_date
				if theWindow is not the frontMostWindow then close theWindow
			end repeat
			error "Nothing selected."
		end if
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell