A script you can use to reduce RTF documents with images

TL;DR

A script you can use to reduce RTF documents with images.
The script is written in AppleScript and is intend to use from the script menu of DEVONthink, So please put the script in the scripts folder of DEVONthink.

To reduce RTF documents, select one or more RTF documents in DEVONthink, run the script from the DT script menu.

Warning: the file size reduction is intrusive since it will happen in place!

The script relies on ImageMagick, be sure to install it via brew install imagemagick ghostscript


While reading Antinet Zettelkasten book of Scott P. Scheper, I am wondering if I can use DT as a repository for long time archival of digital articles, and that I can refer to from my analog antinet. Normally I use markdown, but the problem with markdown is that images are not always downloaded and offline available or available when the website does not exist anymore. Second problem is that images don’t move with the markdown file when this file is moved from one DT database to another DT database.

Long story short, I am evaluating if the RTF document format suites me better than markdown.

RTF documents with images: the “size” issue

When you capture an article with images for later reference in DT, images are embedded in the RTF document.
Zotero has a similar feature to capture articles. Both DEVONthink and Zotero suffer from the same issue: you database will expand very fast due to the graphical content.

To solve this issue in DEVONthink, I came up with a script that is able to reduce the size of RTF documents that has embedded graphical content.

For the script to work, you need to install ImageMagic that you can do by opening your terminal and execute this brew command:

brew install imagemagick ghostscript

If you don’t have brew installed on your mac, visit brew.sh/ on how to install brew on your mac.

The script code and how to install the script

Open Script Editor and copy 'n paste the script code below. Save the script in your DEVONthink script folder as a script. I gave the script the name “Reduce RTF file size.scpt” and saved it in the Format script folder.

(*
A script for reducting RTFD documents by reducing its jpg/JPG/jpeg/JPEG/png/PNG attachments.
Created by Johan Havermans, July 2025. Inspired by the DEVONthink and ImageMagick community.
Version 202507.2
Before you can use this script, you need to install Imagemagick. 
Open terminal and execute: brew install imagemagick ghostscript
posted on https://discourse.devontechnologies.com/t/a-script-you-can-use-to-reduce-rtf-documents-with-images/84544

Used sources:
- 1) https://github.com/ImageMagick/ImageMagick/discussions/7476
- 2) https://usage.imagemagick.org/resize/
- 3) https://www.simplykyra.com/blog/easily-resize-multiple-images-quickly-through-the-terminal-on-your-mac/
- 4) https://stackoverflow.com/questions/26737836/install-multiple-homebrew-formulas-at-the-same-time
- 5) https://discourse.devontechnologies.com/t/applescript-to-bash/58823/4
- 6) https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
- 7) https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateListsofItems.html
- 8) https://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/
- 9) https://stackoverflow.com/questions/6917219/imagemagick-scale-jpeg-image-with-a-maximum-file-size
- 10) https://imagemagick.org/script/defines.php (overview of -define options)
- 11) https://imagemagick.org/script/command-line-processing.php#option (overview of magick options)

*)

set theExtensionList to {"jpg", "JPG", "jpeg", "JPEG", "png", "PNG", "avif", "AVIF", "webp", "WEBP", "tiff", "TIFF"}
set ImageMagick to "/opt/homebrew/bin/magick"
(*
-- Check if Imagemagick is installed. if not, show error message and stop script.
tell application "System Events"
	if not (exists folder ImageMagick) then error "ImageMagick is not installed. Please install via: brew install imagemagick ghostscript"
end tell
*)

-- For each RTFD file reduce its size.
tell application id "DNtp"
	try
		set this_selection to the selection
		if this_selection is {} then error "Please select one or more Rich Text (RTF(D)) document(s)."
		-- for each RTF document in the list ...
		repeat with this_item in this_selection
			-- get the location of the file on the file system
			set thisPath to path of this_item
			
			-- for the current RTF document, reduce the size of the attachment(s) (if any) of type listed in the theExtensionList
			-- (and don't give errors if you don't find an attachment type)
			repeat with n from 1 to length of theExtensionList
				set theCurrentExtension to item n of theExtensionList
				try
					-- my original set theScript to "cd \"" & thisPath & "\"" & " ; " & ImageMagick & " mogrify -path ./ -strip -resize '800>' " & "-quality 80% -define jpeg:extent=100KB *." & theCurrentExtension as string
					set theScript to "cd \"" & thisPath & "\"" & " ; " & ImageMagick & " mogrify -path ./ -thumbnail '790>' -quality 82 -define png:compression-filter=6 -define png:compression-level=9 -define png:compression-strategy=1 -interlace none -colorspace sRGB -strip -compress zip *." & theCurrentExtension as string
					-- Explanation of used parameters
					-- - mogrify : enable in place replacement batch execution mode of magick
					-- - path ./ : Find files in current directory (you first have to cd into the RTFD directory (as in Show Package Contents) otherwise it wont seem to work)
					-- - strip : remove metadata (to further reduce file size)
					-- - thumbnail '800>' : downsize image only when width is larger than 800 pixels (and keep aspect ratio) (same as resize but optimized for performance and also removing color profiles and comments) 
					-- - quality 82% : reduce image quality to 82%
					-- - define jpeg:extent=100KB : resulting jpeg image should not be larger than 100kB					
					-- -define png:compression-filter=5 : adaptive filtering , except for images with a colormap (if you want adaptive filtering for all images, use compression-filter=6)
					-- -define png:compression-level=9 : maximum possible compression
					-- -define png:compression-strategy=1 : what compression to use? 1=default, 2=filtered, 3=huffman_only, 4=fixed ZLIB
					-- -interlace none : remove interlace
					-- -colorspace sRGB : change colorspace to sRGB
					-- -strip : remove image metadata
					-- -compress zip :compress tiff images
					-- - theCurrentExtension : apply to all images of the kind theCurrentExtension as defined in the theExtensionList
					--
					-- **** Removed options ****
					-- -dither None
					-- -define png:exclude-chunk=all : removed since it does the same as -strip
					-- - filter Triangle -define filter:support=2 gives extra size reduction when used with resize or thumbnail command (I got it from source 8))
					-- - unsharp 0.25x0.25+8+0.065 : 
					-- -define jpeg:fancy-upsampling=off : no upsampling
					-- -posterize 136 : reduce the number of color levels
					-- gif resizing does not give good results
					
					do shell script theScript
				end try
			end repeat
		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

How to execute the script

Select one or more rich text documents in DEVONthink, click on the script in the menu bar of DEVONthink and select Reduce RTF file size in the sub folder Format.

As a result, you see in DEVONthink the size of each selected document reduced. Please be warned: the reduction is intrusive. It happens in place.

How does the script work behind the scenes?

A Rich Text or RTF document with attachments is actually a container file. You can get in this container when you right click in Finder on your RTF file and select from the sub menu Show Package Contents. An RTF document with attachments has the extension RTFD. From a UNIX point of view a RTFD file is just a folder with a RTF document and a bunch of attachments.

What the script does is getting into this folder and reduce the size of images and – as a result – reducing the RTF document as a whole. Images that currently are reduced are of the type jpeg, png, avif, webp, gif and tiff.

I am not a software developer, so my script is open for improvement (please be so kind to share your improvements).

Johan

2 Likes

Thanks for sharing.
Should the user expect to see the images change size within the document?