Using convert image in JavaScript

After reading some older posts here on the AppleScript method convert image, I wrote a tiny little thingy in JavaScript:

function performsmartrule(records) {
	var app = Application("DEVONthink 3");
	app.includeStandardAdditions = true;

	records.forEach (r => {
	  if (r.wordCount() === 0) {
 	    const newRecord = app.convertImage (r, {record: r,
			type: "PDF document", waitingForReply: false});
       }
	})
}

which does nothing but produce weird error messages:

  • If I leave out the first parameter of convertImage, i.e. r, I get a “missing parameter” error
  • The same happens if I leave out the record: r part from the second parameter
  • If I call the method as shown above, I get an Error -1728, object can’t be found:
app = Application("DEVONthink 3")
	app.selectedRecords()
		--> [app.databases.byId(1).contents.byId(186284)]
	app.databases.byId(1).contents.byId(186284).wordCount()
		--> 0
	app.convertImage(app.databases.byId(1).contents.byId(186284), {record:app.databases.byId(1).contents.byId(186284), type:"PDF document", waitingForReply:false})
		--> Error -1728: Objekt kann nicht gefunden werden.

From the older posts, I concluded that in AppleScript the method would be called as
convert image record r, without an unnamed first parameter (which goes against the description in the scripting library).

Bonus discovery: using app.ocr(r.path(), { file: r.path(), waitingForReply: true}); at least works, but also insists on using the file’s path twice. And of course it does not remove the original file after OCRing. On the other side, I don’t know if convertImage would remove it, were it working as advertised.

Why do I even bother? Until recently, I had to use Hazel to watch a folder for incoming documents and change their names according to my wishes, since DT could not run JavaScript inside a smart rule. Now it can, and I’d like to get rid of the Hazel step, in analogy to

I think part of the problem is that there shouldn’t be an unnamed parameter to any of the OCR commands. Whilst the automatically generated documentation in Script Editor shows that parameter it is not as far as I can see defined internally in the application.

Thanks for the full description, I will take a look.

1 Like