Merge images and delete originals

Merging images into a PDF is really easy using the ‘merge images’ contextual menu item, but I am working with thousands of images that need to be made into hundreds of PDFs, so merging and then deleting originals takes time. It would help if I could have an AppleScript that combines images into a pdf and deletes the original jpgs afterwards. Images are named 1.jpg, 2.jpg, 3.jpg, etc. So name would be 1-3.pdf, 4-7.pdf and so on. Can someone explain how this would be done?

Welcome @mbriordan

What specifically do you need help with / what are you expecting a script to do?

Sorry I wasn’t clear.

I’d like the script to merge JPEG images into an multi-page OCRd PDF (like the ‘Merge Images’ menu item) called [first image name]-[last image name], and then delete the original jpegs.

I have a few thousand images, but the PDFs I’m creating are all very short (between 2 and 10 pages long), and I’m finding merging the images, and then deleting each image individually, is taking a lot of time so I’d like to combine the two processes.

No worries!

And just how would the script know what images are supposed to be merged?
Are you putting them in groups?Tagging them? …?

You’re not going to save yourself 100% of the work on this. Maybe not even 80%. :slight_smile:

What I am looking for is a script that I can use (in the contextual right click menu) once I have selected multiple images, so that it:

  1. merges those images into existing multi-page OCR PDF (this is what ‘Merge Images’ currently does); then
  2. deletes the original jpegs

This will take a lot less time than what I currently do, which is:

  1. select all images and merge the images using ‘Merge Images’
  2. select all the images I want to delete
  3. hit delete

Played around with various other ways of doing this, but all take quite a bit more time.

Obviously if I was just wanting to merge all images and then delete originals, this wouldn’t take much more time; but I am merging thousands of images into thousands of short (1-5 page) PDFs, which takes the time. The photos are arranged in name order — 1, 2, 3 … Because I am working through the images in order to add proper metadata to each, I also want to ensure the order of the photos and PDFs is maintained.

Thanks. I want to be able to select images, right click, merge them into a OCR PDF and delete the original images.

Currently, I have to:

  1. Select images
  2. Right click > Merge Images
  3. Select original images again
  4. Delete

I want to:

  1. Select images
  2. Right click > Scripts > Merge Images and Delete

Without having to reselect the images and delete them. I am adding metadata to thousands of documents at once, so I need to retain the files in name order as I process them. Because of this, I need to delete the images as I go, not leave this till the end.

So you’re adding the image to existing PDFs or are you creating new PDFs from the images?

You’re adding metadata to the images, than you merge the images to a PDF – what happens to the image metadata in the PDF?

In any case, a script to merge thousands of manually selected images in chunks is certainly possible. But in my mind, it’s not much of an automation. How do you decide which images to merge? If you follow rules on doing that, I’d write the script around them.

Otherwise, a simple four-liner should do the trick:

tell application id "DNtp"
  set pdf to merge records selected records 
  delete selected records 
end tell

Though that’s an extremely dangerous approach: if the merge fails, the originals are gone. And I did not test the script,

1 Like

Many thanks. I am creating a new PDF from jpeg images.

I know it is simple - but I am new to automating DevonThink. I’ve used AppleScript before (a while back), but I don’t know the specifics of DevonThink.

I don’t need to merge the image metadata, except that the name of the PDF should be [first image name]-[last image name].pdf. As all the image names are numbers, this would be (eg) 1-3.pdf.

This can presumably be done by adding another line to the AppleScript?

Could we secure it by changing the ‘delete selected records’ to move them to trash?

This script should be added to the contextual menu folder, is this correct?

Forgive me, but I’m not an AppleScript person. So, I’m not going out on a limb with more code. Script editor should help you, together with the DT scripting dictionary and the gazillion of sample code pieces in the forum.

I still don’t understand why you add metadata to images that you delete later. Seems futile to me.

Sorry, not adding metadata to the originals, just the name of the original file — 1.jpg, 2.jpg and so on — should be used in the title or PDF: [first-image]-[last-image].pdf. So if I combined 3 images called 1.jpg, 2.jpg and 3.jpg, the name would be 1-3.pdf

I have worked this out for myself.

tell application id "DNtp"

        -- set to the Trash folder, or other temporary folder for images, in database
	set theFilingGroup to get record at "/Trash" in database "My Database"

        -- make sure some images are selected 
	if selection is {} then error "Select some documents"
	set theSelection to selection

        -- set name for merged file
	set numItems to the (number of items in theSelection) - 1
	set mergedName to the name of the first item of theSelection & "-" & the name of the last item of theSelection
	set tempDate to creation date of the first item of theSelection
        -- perform the merge
	set mergedRecord to (merge records theSelection in current group)
	set the name of mergedRecord to mergedName
	set the creation date of mergedRecord to tempDate

       -- move selected items to the folder named at top of script
	repeat with thisItem in theSelection
		set mvItem to move record thisItem to theFilingGroup
	end repeat
end tell

I saved this as 'Merge and Delete.scpt ’ n the ‘Contextual Menu’ subfolder of the scripts folder, accessible by selecting Scripts > Open Scripts Folder in Devonthink. To run, select images, right click > Scripts > Merge and Delete.

1 Like


Hold the Option key when choosing Tools > Merge and you’ll see it shows Merge & Delete n documents.

1 Like

Thanks! This isn’t really intuitive…

There are optional commands available in several menus. Merging without deleting the originals is by far the preference of our userbase, hence it being added as an alternative command. This is also documented behavior in Menus > The Tools Menu > Create, Summarize, Merge, Split section of the built-in Help and manual


Thanks. I did look at the help files but didn’t see this. I’m new to DT.

No worries. DEVONthink is a deep application but it is well documented. :smiley:

Preferences > OCR > Original Document: Move to Trash

… as noted in the Preferences > OCR section of the Help and manual.

1 Like

Great, thank you. Yes I spotted this after I had written my response - so deleted.

1 Like

Man, this thread convinces me I have got to learn AppleScript in the hope of someday doing something similarly awesome.