Change image resolution (dpi) with AppleScript or Automator

Is there a way to change the resolution of an image (the DPI, not the size) using AppleScript or Automator. To be clear with the terms, I want to change images to 72 dpi, not change their width and height. (I already know how to scale in AppleScript and Automator.) I’m a little surprised that there’s no obvious way to do this.

The sips command can do it:

sips -s dpiHeight 72.0 -s dpiWidth 72.0 test.png

Make sure the values are given as floating point rather than integer.

Since sips is closely tied to AppleScript’s Image Events feature, I would think there’s a way to do it directly in AppleScript, but maybe not.

2 Likes

Thank you, Dr. Drang. I’ll give that a try. Right now, I’m trying to figure out how to pass the path to the file from Automator to the shell script and back without getting an error message.

I’m a little surprised that you can do this from the shell but not from Applescript. (I’ve checked everywhere and while you can find out the resolution, you can’t change it.)

Everything can be done from a shell. I adore AppleScript more than it warrants, but I do see a lot of AppleScripts that are 90% do shell script and wonder why they weren’t just written as shell scripts.

You need only check the Image Events scripting dictionary, which states that the resolution property for an image object is read-only. I am at a loss to explain why that restriction is there. It can be done using AppleScriptObjC, but the shell command is much simpler and more efficient, and probably quicker.

To do what you’re looking to do, you’d create a Quick Action that accepts files as input from within Finder. The workflow would consist of a single Run Shell Script action that would receive input as arguments. This would assign each file path to the shell variables $1, $2, ... etc.

Reading the manpage for sips, I see it accepts multiple file paths as arguments, which saves us having to loop through the files. Collectively, the variables "$1" "$2" "$3" ... when written on the command line as individual quoted arguments can be expressed with the shorthand "$@".

Therefore, your entire shell script would look like this:

sips -s dpiHeight 72.0 -s dpiWidth 72.0 "$@"

I believe this would overwrite the files with the new property key values. However, if they don’t, we’d probably need to specify a value for the --out option that sips provides to allow a folder path to be passed at which new copies of each image file would be saved set with the new resolution.

As a small side note, if the shell script action reports being unable to locate the sips command, you’d need to refer to it using its full path, which can be got by running the command which sips.

Thank you, chri.sk, that seems to have been what I needed to learn to make it work. I most often only need to do this with one file at a time but I included the argument variables. I’m okay with overwriting the file because in a previous step I duplicate it in the Finder.

Thanks again to everyone for their help. I’m glad to have learned something new as much as accomplished the task.