Smart Rule to automatically rotate portrait-oriented pages to landscape in PDFs

I was hoping to implement a Smart Rule that, on demand, detects whether any pages in a PDF are oriented in portrait (i.e. height > width) and for those pages, sets the orientation to landscape (i.e. rotates +/- 90 degrees).

I tried the following embedded script but am getting an error when trying to save the Smart Rule:

on performSmartRule(theRecords)
	tell application id "DNtp"
	   repeat with theRecord in theRecords
          set myDoc to document 1
          set pageCount to count pages of myDoc
          repeat with pageNumber from 1 to pageCount
              if (height of page pageNumber of myDoc) > (width of page pageNumber of myDoc) then
                 set rotation of page pageNumber of myDoc to 90
              end if
	      end repeat
       end repeat
    end tell
end performSmartRule

DT3 throws the error “Expected ‘,’ but found identifier,” and highlights pageNumber in repeat with pageNumber from to pageCount`.

I am not quite sure what the issue is with this syntax because I am able to compile this script without an issue in Script Editor for use with PDFpen Pro.

Any assistance would be appreciated.

Scripting of PDF pages isn’t supported by DEVONthink, this would require a third-party app. E.g. Image Events could be used for single-page PDF documents:

tell application id "DNtp"
		set this_selection to the selection
		repeat with this_item in this_selection
			if the type of this_item is equal to picture or the type of this_item is equal to PDF document then
					set image_width to the width of this_item
					set image_height to the height of this_item
					if image_width < image_height then
						set this_image to the image of this_item
						with timeout of 30 seconds
							tell application "Image Events"
								set this_file to open file this_image
								rotate this_file to angle 90
								save this_file without icon
								close this_file
							end tell
						end timeout
					end if
				end try
			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
	end try
end tell

thanks for this response… I was surprised to find that when executing this script manually (by placing the script in ~/Library/Application Scripts/com.devon-technologies.think3/Custom), the page does not rotate (even if meeting the condition image_width < image_height) and the record in DT3 gets changed from PDF + Text to PDF (even though I can still highlight text within the PDF). Is it expected that using Image Events deletes the text layer from the PDF?

To give a little more background here, I am keeping records of checks deposited. When I scan the checks, the orientation for side 1 is identified as landscape (width > height) but the orientation for side 2 is identified as portrait (height > width). Is there a way to use a script condition in a Smart Rule to identify multi-page PDFs that have pages of both portrait and landscape orientations?

I am envisioning something that basically runs, as a condition of the Smart Group, a shell command on the input records that are of type PDF/PDF + Text (e.g., pdfinfo -l {last page of the doc in the selection} -meta /path/to/source/file.pdf) and then greps the result and includes in the Smart Group only those items where two conditions are true:

  • Pages = 2
  • Page 2 size: width > height

There are no programmable criteria in a smart group or smart rule.
There is only the Execute Script action.

I would suggest you set up a specific In and Out group in DEVONthink to be specific in targeting and a subsequent move. Here’s mine…

Screen Shot 2020-02-11 at 11.30.22 PM

While generally we prefer to offer solutions more based on pure AppleScript, this is a case where it seems appropriate to use some deeper tech, i.e. shell scripting. Also note, I would caution people who want to play around in the shell since it’s easy enough to corrupt a file (or worse), if you don’t know what you’re doing.

Here is a smart rule that triggers when importing files to the targeted group: Checks In.
After the processing, it moves the file to the Checks Out group, which also keeps the rule from reprocessing a file if you were to perform the rule again.

You will have to edit the smart rule and script to match your own configuration and database name.

Bear in mind, there’s no guarantee this is 100% and I would suggest you test this by dragging some files into the In folder to see how it behaves before you commit to it.

Process (1.6 KB)

going to bed now… :sleeping:

1 Like