You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator shrink images larger than X

I have created an automator task compiled from various resources I found online to optimize usage for our website. It performs the following actions:


  • All selected images get copied to an export folder in place
  • All images get -web added to the filename
  • various unsafe characters get replaced
  • The imagetype gets changed to JPEG
  • It is run through ImageOptim for compression.


This works great, however I still miss one optimisation. Sometimes images are 4000 pixels or larger, which is unnecessary for web. I would really like to be able to shrink (resize) all images that are over 1200 pixels in either width or height dimension proportionally. I am aware of the feature in automator, however to my knowledge this would also enlarge images smaller than 1200 pixels which is unwanted.


I am very curious if anyone has found a way to fix this issue.


Thank you for the help!

Martijn


Posted on Oct 29, 2021 5:20 AM

Reply
Question marked as Top-ranking reply

Posted on Oct 29, 2021 10:17 AM

Automator is great at running linear workflows. It really falls down when you have any logic or flow needs (e.g. if/then statements, loops, etc.)


In this case you're likely to need to step out to a shell or AppleScript where you have a lot more control over logic flows.


For example, here's an AppleScript you can incorporate into your workflow via a Run AppleScript action. It takes a file and determine the pixel height and width. If either exceeds 4000, it resizes the image using the built-in sips command (there are other ways of doing this, too:


on run {input, parameters}
	
	repeat with theImage in input
		set theFilePath to quoted form of POSIX path of theImage
		
		set theWidth to last word of (do shell script "sips -g pixelWidth " & theFilePath) as integer
		set theHeight to last word of (do shell script "sips -g pixelHeight " & theFilePath) as integer
		
		if theWidth > 4000 or theHeight > 4000 then
			do shell script "sips -Z 1200 " & theFilePath
		end if
	end repeat
	return input
end run


Similar questions

2 replies
Question marked as Top-ranking reply

Oct 29, 2021 10:17 AM in response to MJansen0031

Automator is great at running linear workflows. It really falls down when you have any logic or flow needs (e.g. if/then statements, loops, etc.)


In this case you're likely to need to step out to a shell or AppleScript where you have a lot more control over logic flows.


For example, here's an AppleScript you can incorporate into your workflow via a Run AppleScript action. It takes a file and determine the pixel height and width. If either exceeds 4000, it resizes the image using the built-in sips command (there are other ways of doing this, too:


on run {input, parameters}
	
	repeat with theImage in input
		set theFilePath to quoted form of POSIX path of theImage
		
		set theWidth to last word of (do shell script "sips -g pixelWidth " & theFilePath) as integer
		set theHeight to last word of (do shell script "sips -g pixelHeight " & theFilePath) as integer
		
		if theWidth > 4000 or theHeight > 4000 then
			do shell script "sips -Z 1200 " & theFilePath
		end if
	end repeat
	return input
end run


Automator shrink images larger than X

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.