Image Events Resize

sips is still supposed to work. It definitely works on Sequoia.

This is a script where you specify the larger value of width and height of the image and it will be resized preserving the aspect ratio. The script adds the size information to the file name of the resized image.

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

property targetWH : "480"

set theImagePath to POSIX path of (choose file of type "public.image")
set theURL to current application's NSURL's fileURLWithPath:theImagePath
set fileExtension to theURL's pathExtension() as text
set fileName to theURL's lastPathComponent() as text
set parentFolder to POSIX path of (theURL's URLByDeletingLastPathComponent() as text)
set destinationPath to parentFolder & text 1 thru -((count fileExtension) + 2) of fileName & "_" & targetWH & "." & fileExtension
do shell script "sips -Z " & targetWH & space & quoted form of theImagePath & " --out " & quoted form of destinationPath
1 Like

If you’re dealing with files whose dimensions may fall below the destination size…

do shell script "sips -z " & targetWH & space & targetWH & space & quoted form of theImagePath & " --out " & quoted form of destinationPath

Is there an issue with image events? It strikes me as odd to call out to the shell for something that applescript provides natively.

I modified this script slightly. Basically, specify your source and destination folders. It will get all the matching files in the sources and save the scaled copies with a modified name to a destination folder. It should be easy enough to change those factors depending on the required workflow.

Important: As it can grab files from multiple folders, there may be name conflicts when saving. I’ve set it to skip any image that has a name of an already scaled image to prevent any overwriting.

As to the scaling… as written it takes the longest dimension of the image and scales that to 960 pixels. The opposing dimension will scale accordingly. Adjust as required.

use scripting additions

-- set up default location for 'choose folder' command
set defLoc to (path to pictures folder) -- customize as required

tell application "Finder"
	-- specify destination folder, create as necessary
	try
		set repository to ((path to desktop as text) & "repository:") as alias -- destination
	on error
		set repository to (make new folder at (path to desktop) with properties {name:"repository"}) as alias
	end try
	
	-- specify source folders
	set sourceFolders to (choose folder default location defLoc with prompt "pick some already" with multiple selections allowed) as alias list
	-- set depot to a reference to selection as alias -- finder selection as alternate approach
end tell

-- get jpegs of source folder
tell application "System Events"
	-- set sourceList to files of depot whose type identifier is "public.jpeg"
	set depot to {}
	repeat with folio in sourceFolders
		set end of depot to (files of folio whose type identifier is "public.jpeg")
	end repeat
end tell

-- flatten to simple list
set depot to flatten(depot)

-- cycle images
repeat with eachImage in depot
	-- set up new name and path
	tell application "System Events"
		set nom to name of eachImage
		set ext to name extension of eachImage
		set AppleScript's text item delimiters to "." & ext
		set baseName to text item 1 of nom
		set oldPath to path of eachImage
		set AppleScript's text item delimiters to ""
		-- append 'scaled' to resulting file name
		set newPathName to (repository & baseName & " scaled" & "." & ext) as text
	end tell
	
	-- scale images along longest dimension
	try -- if file with that name already exists, will skip
		alias newPathName
		tell application "Image Events"
			launch
			
			set orig to open oldPath
			scale orig to size 960
			save orig in newPathName -- with icon
		end tell
	end try
	
end repeat
tell application "Finder"
	activate
	select repository
end tell

-- flatten depot lists
on flatten(listOfLists)
	script o
		property fl : {}
		
		on flttn(l)
			script p
				property lol : l
			end script
			
			repeat with i from 1 to (count l)
				set v to item i of p's lol
				if (v's class is list) then
					flttn(v)
				else
					set end of my fl to v
				end if
			end repeat
		end flttn
	end script
	
	tell o
		flttn(listOfLists)
		return its fl
	end tell
end flatten

NB I borrowed Nigel’s recursive list flattener.