Script: Create PDFs from URLs and merge them

This script creates PDFs for page links and merges them into one PDF afterwards.

The script gets the start URL

  • from DEVONthink’s current tab or
  • from the selection (i.e. it’s possible to process multiple records at once).

You need to make sure that the start URL contains the string that’s set in property theURL_Query.

If something doesn’t work check DEVONthink’s log.

-- Create PDFs from URLs and merge them

property theURL_Query : "?page="

tell application id "DNtp"
	try
		set {theURLs, theRecords} to my getURLsAndRecords()
		
		activate
		set theURL_StartPage to (display name editor "Create PDFs from URLs and merge them" info "Start Page:" default answer "0")
		set theURL_EndPage to (display name editor "Create PDFs from URLs and merge them" info "End Page:" default answer "9")
		set theGroup to display group selector "Choose destination:"
		
		repeat with i from 1 to (count of theURLs)
			set thisURL to (item i of theURLs) as string
			
			if thisURL ≠ "" then
				
				if thisURL contains theURL_Query then
					set theURL_withoutPage to item 1 of my tid(thisURL, theURL_Query)
					set theDate to do shell script "date \"+%Y-%m-%d% %H:%M:%S\""
					
					repeat with j from theURL_StartPage to theURL_EndPage
						set thisURL to theURL_withoutPage & theURL_Query & j
						set thisRecord to create PDF document from thisURL in theGroup with pagination
					end repeat
					
					set theChildren to search "kind:pdf creationDate>=" & theDate in theGroup
					
					if theChildren ≠ {} then
						set theRecord to merge records theChildren in theGroup
						set theRecord_Name to (name without extension of theRecord) & space & "[Merged]"
						set name of theRecord to theRecord_Name
						set URL of theRecord to theURL_withoutPage
						display notification theURL_withoutPage subtitle theRecord_Name with title "Merged"
					else
						my logMessage("No PDF records found", (item i of theRecords))
					end if
					
				else
					my logMessage("URL does not contain" & space & "\"" & theURL_Query & "\"", (item i of theRecords))
				end if
				
			else
				my logMessage("No URL", (item i of theRecords))
			end if
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on getURLsAndRecords()
	try
		tell application id "DNtp"
			if (exists think window 1) then
				set theWindow to think window 1
			else
				error "Please open a think window"
			end if
			
			set theContentRecord to (content record of (current tab of theWindow))
			if theContentRecord ≠ missing value then
				set theContentRecord_URL to URL of theContentRecord
			else
				set theContentRecord_URL to missing value
			end if
			
			set theCurrentTab_URL to URL of (current tab of theWindow)
			
			if theCurrentTab_URL = "" or theCurrentTab_URL starts with "file:///" then
				set theRecords to selected records
				if theRecords ≠ {} then
					set theURLs to URL of selected records
				else
					error "Please select some records."
				end if
			else
				if theCurrentTab_URL = theContentRecord_URL then
					set theURLs to {theCurrentTab_URL}
					set theRecords to {theContentRecord}
				else
					if theCurrentTab_URL does not start with "file:///" then
						set theURLs to {theCurrentTab_URL}
						set theRecords to {missing value}
					else
						set theURLs to {""}
						set theRecords to {missing value}
					end if
				end if
			end if
			
			return {theURLs, theRecords}
		end tell
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"getURLsAndRecords\"" message error_message as warning
		error number -128
	end try
end getURLsAndRecords

on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	if class of theInput = text then
		set theOutput to text items of theInput
	else if class of theInput = list then
		set theOutput to theInput as text
	end if
	set AppleScript's text item delimiters to d
	return theOutput
end tid

on logMessage(theLog_Info, theRecord)
	try
		tell application id "DNtp"
			set theLog_Info_Prefix to "Script \"Create PDFs from URLs and merge them\": "
			if theRecord ≠ missing value then
				log message info theLog_Info_Prefix & theLog_Info record theRecord
			else
				log message info theLog_Info_Prefix & theLog_Info
			end if
		end tell
	on error error_message number error_number
		activate
		if the error_number is not -128 then display alert "Error: Handler \"logMessage\"" message error_message as warning
		error number -128
	end try
end logMessage

4 Likes

absolutely fantastic. thank you so much @pete31 Solves a big irritant.

1 Like