PDF to JPEG automation

I write a newsletter which gets sent as an email using MailChimp. People send me ‘posters’ to include in the newsletter. These come to me as PDF or WORD files.

Using an automation on this forum I can successfully convert WORD to PDF.

I have an AppleScript that uses Image Events to convert the PDF to a JPEG. I can only get Image Events to save the JPEG to the desktop or into the DT global inbox. Is there a way to save it to a particular database Inbox or tag it so I can use a smart rule to get it to the right place? I can’t work out how to do this in AppleScript. Any help gratefully received.

Or should I use another way to do this?

I want to select a PDF poster and generate a JPEG version. Thanks

Post the code you have.

on performSmartRule(theRecords)
	tell application id "DNtp"
		set theSelection to the selection
		repeat with theRecord in theSelection
			set theFile to (path of theRecord) as POSIX file
			set theDoc to theFile as alias
			
			tell application "Finder"
				set ext to name extension of theDoc
				if ext is "pdf" then
					set theName to name of theDoc
					set theFilename to characters 1 through ((length of theName) - (length of ext) - 1) of theName as string
					set theFilename to theFilename & ".jpeg"
					
					tell application "Image Events"
						launch
						set x to open theFile
						save x as JPEG in "/Users/timothyjsmith/Library/Application Support/DEVONthink 3/Inbox/" & theFilename
						close x
					end tell
					
				end if
			end tell
			
			try
				move record theRecord to trash group of (database of theRecord)
			on error errMsg
				display dialog "Error deleting item: " & errMsg
			end try
			
		end repeat
	end tell
end performSmartRule

You could save the file to the /tmp folder, tell DT to import it to the location where you want it, and then delete the file. Something like

save x as JPEG in "/tmp/" & theFilename
close x
tell application id "DNtp" to import "/tmp/" /& theFilename to …
do shell script "rm " & quoted form of ("/tmp/" & theFilename)

This is just off the top of my hat, since I don’t do AppleScript usually.

There might be other ways to do that, eg using Apple’s PDFKit and the ASObjC bridge. Something along these lines:

If I run the script against a pdf in Database A / Group A, it creates a jpeg in the DT global inbox. Ideally I would like the file in either Database A /Group A or at least in Database A inbox (either by saving it there or having a tag that a smart rule picks up and moves it from the global inbox box to the Database A inbox.

It’s not a deal breaker - I am curious how it could be done in AppleScript (if at all)

FYI:

I think that I did understand what you want to achieve. And I proposed steps as well as two additional lines of code that would probably do that.

Did you try that code?

And yes, I do know what Image Events is and where to find its reference.

Ah, I misunderstood your code. The idea is to save to a temp location and then import into DT where I can use the current database/group in the import statement and then remove from the temp location.

I will give it a try.

At a quick glance, you shouldn’t be processing the selection in a smart rule. You should be processing the input parameter theRecords.

Also, is your code actually working? I am getting a missing value when trying to process PDFs.
Also, sips can’t extract an image from a PDF document (likely unless there’s an image to extract).

sips -s format pdf /Users/apollox/Downloads/2111111-1.pdf --out ~/downloads/test.jpeg
/Users/apollox/Downloads/2111111-1.pdf
Error: Cannot extract image from file.
  /Users/apollox/Downloads/test.jpeg
Error 13: an unknown error occurred
2 Likes

Check out this proof-of-concept Automator workflow…

  1. Unzip the attached file.
  2. In the Finder, press Command-Shift-G and paste: ~/Library/Application Scripts/com.devon-technologies.think3.
  3. Drag and drop the workflow from Step 1 into the Menu folder in this window (or a desired subfolder).
  4. Relaunch DEVONthink and you can access the script from the Script icon menu (and the subfolder, if chosen).

Optionally, if you put the script in the Toolbar folder in step 2 above, you can…

  1. Relaunch DEVONthink.
  2. Right-click the toolbar in DEVONthink and choose Customize Toolbar.
  3. Drag and drop the workflow to the Toolbar.

And yes, you could rename it before you add it to the Toolbar directory.

PDF Pages to Imported JPEGs.workflow.zip (293.4 KB)

I had to update “Render PDF Pages as Images” in the workflow to a later version on my MacBook and change the import database destination in your script. I didn’t know you could include automator workflows in the script menu.

It works fine - Than you!

I think my smart rule worked as the ‘poster’ pdfs are always a single pdf page.

@chrillek Altering AppleScript as follows it works (but only for one page PDF) - which is what my poster PDFs are. Thank you

The automator workflow from @BLUEFROG also works and for multipage PDFs - Thank you.

FYI revised script:

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set theFile to (path of theRecord) as POSIX file
			set theDoc to theFile as alias
			
			tell application "Finder"
				set ext to name extension of theDoc
				if ext is "pdf" then
					set theName to name of theDoc
					set theFilename to characters 1 through ((length of theName) - (length of ext) - 1) of theName as string
					set theFilename to theFilename & ".jpeg"
					
					tell application "Image Events"
						launch
						set x to open theFile
						save x as JPEG in "/Users/timothyjsmith/Documents/Temp/" & theFilename
						close x
					end tell
					
				end if
			end tell
			
			-- import the picture and delete the temporary file and move original to trash
			try
				import "/Users/timothyjsmith/Documents/Temp/" & theFilename to (incoming group of the current database)
				do shell script "rm " & quoted form of ("/Users/timothyjsmith/Documents/Temp/" & theFilename)
				move record theRecord to trash group of (database of theRecord)
				display dialog "PDF converted to JPEG"
			on error errMsg
				display dialog "Error deleting item: " & errMsg
			end try
			
		end repeat
	end tell
end performSmartRule

That might be a limitation of Image Events.