Ready-to-Use AppleScript to Scale or Resize Images

I’ve missed the ability to resize images within Panorama, and finally got a good and reliable replacement going.

The following AppleScript resizes or rescales one or many images at a time.

It uses variables that must be passed to it:

fgFileList, a comma separated list of complete POSIX paths
fgSize, the maximum size of the long dimension in pixels
fgScale, the percentage of the scaling factor (e.g., 0.5 for 50%, 2 for 200%)
fgTargetFolder, a single POSIX path of the destination folder
lvThumbnail, if it has a value, the value itself is appended to the file name.

In my use, anything 360 pixels or less is tagged as a thumbnail: imagetn.jpg. If you don’t want to use that, set lvThumbnail = “” rather than trying to edit the script.

It can’t both scale and resize an image, so if both have values, Scale gets processed and Size is ignored.

I have this loaded as a called procedure in my highly evolved version of the ImageLog in the Database Sharing library. Primarily, it’s for preparing images that will be posted to the galleries on my personal website. Those pages, by the way, are then built 100% by Panorama and ftp’ed to the site.

Below is the form I use to set up, then call the script.

Let lvResult = ""
Let lvThumbnail = ?(fgSize ≠ "" and val(fgSize) <361,"tn","")

AppleScript |||

-- Variables passed from Panorama
set imageFilesList to $«fgFileList»$ -- Comma-separated POSIX paths
set NewSize to $«fgSize»$ -- Maximum size in pixels
set ScaleFactor to $«fgScale»$ -- Scaling factor (e.g., 0.5 for 50%)
set destinationFolder to $«fgTargetFolder»$ -- POSIX path of destination folder
set tn to $«lvThumbnail»$ -- thumbnail gets tn added to name

-- Initialize error log
set errorLog to ""

-- Save original text item delimiters and set new ones
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set imageFiles to text items of imageFilesList
set AppleScript's text item delimiters to oldDelimiters

-- Ensure destination folder ends with a slash
if destinationFolder does not end with "/" then
	set destinationFolder to destinationFolder & "/"
end if

-- Create destination folder if it doesn't exist
try
	do shell script "mkdir -p " & quoted form of destinationFolder
on error errMsg
	set errorLog to errorLog & "Failed to create destination folder: " & errMsg & return
	return errorLog
end try

-- Initialize processed count
set processedCount to 0

-- Determine whether to use scaling or resizing
set useScaling to false
if ScaleFactor is not "" then
	try
		set ScaleFactor to ScaleFactor as real
		if ScaleFactor > 0 then
			set useScaling to true
		else
			set errorLog to errorLog & "Invalid scale factor (must be positive): " & ScaleFactor & return
		end if
	on error
		set errorLog to errorLog & "Invalid scale factor: " & ScaleFactor & return
	end try
else if NewSize is not "" then
	try
		set NewSize to NewSize as integer
		if NewSize ≤ 0 then
			set errorLog to errorLog & "Invalid size (must be positive): " & NewSize & return
		end if
	on error
		set errorLog to errorLog & "Invalid size: " & NewSize & return
	end try
else
	set errorLog to errorLog & "Neither fgSize nor fgScale provided" & return
	return errorLog
end if

-- Loop through the list of POSIX paths
repeat with posixPath in imageFiles
	-- Ensure posixPath is treated as text
	set posixPath to contents of posixPath as text
	
	-- Skip empty paths
	if posixPath is "" then
		set errorLog to errorLog & "Empty path encountered" & return
	else
		-- Convert POSIX path to AppleScript alias for file info
		try
			set thisFile to POSIX file posixPath as alias
		on error
			set errorLog to errorLog & "Invalid path: " & posixPath & return
		end try
		
		-- Only proceed if the file exists
		if thisFile exists then
			-- Get the filename from AppleScript
			set fileInfo to info for thisFile
			set fileName to name of fileInfo
			
			-- Save original delimiters before parsing file extension
			set oldDelimiters to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "."
			
			-- Separate base name and extension
			if (count of text items of fileName) ≥ 2 then
				set baseName to text items 1 thru -2 of fileName as text
				set fileExt to last text item of fileName
			else
				set baseName to fileName
				set fileExt to "jpg" -- Default extension
			end if
			
			-- Restore original delimiters
			set AppleScript's text item delimiters to oldDelimiters
			
			-- Build the output path with -resized or -scaled in the destination folder
			set outputName to baseName & tn & "." & fileExt
			set outputPath to destinationFolder & outputName
			
			-- Run sips to either scale or resize
			try
				if useScaling then
					-- Get current image dimensions
					set widthResult to do shell script "sips -g pixelWidth " & quoted form of posixPath
					-- Extract width from sips output
					set AppleScript's text item delimiters to ": "
					set currentWidth to text item 2 of widthResult as integer
					set AppleScript's text item delimiters to oldDelimiters
					-- Calculate new width
					set newWidth to round (currentWidth * ScaleFactor)
					if newWidth ≤ 0 then
						set errorLog to errorLog & "Calculated width is invalid for " & posixPath & ": " & newWidth & return
					else
						-- Apply scaling using sips --resampleWidth
						do shell script "sips --resampleWidth " & newWidth & " " & quoted form of posixPath & " --out " & quoted form of outputPath
						set processedCount to processedCount + 1
					end if
				else
					do shell script "sips --resampleHeightWidthMax " & (NewSize as integer) & " " & quoted form of posixPath & " --out " & quoted form of outputPath
					set processedCount to processedCount + 1
				end if
			on error errMsg
				set errorLog to errorLog & "Error processing " & posixPath & ": " & errMsg & return
			end try
		end if
	end if
end repeat

-- Return result with errors (if any) and count
if errorLog is not "" then
	return "Processed " & processedCount & " file(s). Errors:" & return & errorLog
else
	return "Processed " & processedCount & " file(s)."
end if


|||,lvResult

Message lvResult