Setting selection of viewer window

I’d like to set the selection to every PDF in DEVONthink’s inbox that needs to be OCR’ed. However, it seem that “set selection to list” command doesn’t work. The following script fails with an “AppleScript error -10000” as soon as there is at least one PDF without text:


tell application "DEVONthink Pro"
	set myWindow to open window for record incoming group
	set myGroup to root of myWindow
	set myList to every child of myGroup whose kind is "PDF"
	set myWindow's selection to myList
end tell

Kind shouldn’t be used usually, the type is more reliable and doesn’t depend on the localization. Is anything logged to the console?

I’d prefer to use “type” because of the reason you gave, however it seems that PDF containing text share the same type with “graphics-only” PDFs, so I resorted to use “kind” in order to find the PDFs without a text layer.

I just tried it again and there is an error logged to the console:


23.09.16 15:39:31,834 DEVONthink Pro[704]: -[DTRecord objectsByEvaluatingSpecifier]: unrecognized selector sent to instance 0x7fe01c85ef80
23.09.16 15:39:31,835 DEVONthink Pro[704]: An exception was thrown during execution of an NSScriptCommand...
23.09.16 15:39:31,835 DEVONthink Pro[704]: -[DTRecord objectsByEvaluatingSpecifier]: unrecognized selector sent to instance 0x7fe01c85ef80

This might be a bug of AppleScript or of DEVONthink Pro, I’ll check this. In the meantime just use a loop to retrieve the desired items:


tell application id "DNtp"
	set myWindow to open window for record incoming group
	set myGroup to root of myWindow
	set myChildren to children of myGroup
	set myList to {}
	repeat with theChild in myChildren
		if kind of theChild is "PDF" then set myList to myList & theChild
	end repeat
	set myWindow's selection to myList
end tell

Things are getting stranger and stranger. The code you posted doesn’t work here (OS X 10.11.6, DTPro 2.9.4) I got it to “work” by changing the if statement; however it still fails at the last statement (“set myWindow’s selection to myList”).


tell application id "DNtp"
	set myWindow to open window for record incoming group
	set myGroup to root of myWindow
	set myChildren to children of myGroup
	set myList to {}
	repeat with theChild in myChildren
		if kind of theChild is "PDF" then set end of myList to theChild
	end repeat
	set myWindow's selection to myList
end tell

FWIW I am finding that I can get the list with

tell application id "DNtp"
	set oWin to open window for record incoming group
	set oRoot to root of (oWin)
	
	set lstPDF to (children of oRoot where kind = "PDF") as list
	
	return lstPDF
end tell

But I seem to get an error when I try to set the selection list of the window

Why do you want to set the selection?

Because I’d like to convert them to a searchable PDF with a single click (or a single call to “System Events”).

Unfortunately, the “Data → Convert → to searchable PDF” menu command isn’t exposed to AppleScript. I am aware of the “convert image” command. However, this creates a new record while the menu command converts the PDF “in place” which I prefer (especially because it ensures the UUID of the record stays the same, so that any references I created in OO4 or otherwhere will continue to work).

As a workaround I now open a new window for each PDF that needs to be converted and use System Events to do the OCR.

Data > Convert > to searchable PDF creates a new record (like all conversions), the original is moved to the trash on demand (see Preferences > OCR).

Thanks for pointing me to this. I really thought that the original UUID would be preserved, but never checked on it…

Version 2.9.6 will fix this.

Great! Thanks a lot for the update!

You can also automate in place OCR if you use an external application such as Abby Finereader or Smile’s PDFPenPro. Here is an example using Finereader [url]ABBYY Finereader Pro v12]

It would be more reliable than relying on system events.

Frederiko