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

Use Automator to get image dimensions

I want to rename image files by appending the image dimensions. So for instance if I feed an Automator workflow a collection of images files like

  1. apple.jpeg
  2. orange.png

Those 2 files would be renamed:

  1. apple 640x480.jpeg
  2. orange 1024x1024.png


I know I can embed an AppleScript action in a workflow. AppleScript has a way to get image dimensions, but I haven't been able to get it to work at the basic AppleScript level. I get "Can’t get dimensions of {missing value}."


What I have so far:

[Get Specified Finder Items]

[Run AppleScript]
on run {input, parameters}
	set img to open input
	set d to dimensions of img
	display dialog input & ": " & (item 1 of d) & "x" & (item 2 of d)
	return input
end run



Posted on Mar 1, 2020 2:31 PM

Reply
Question marked as Top-ranking reply

Posted on Mar 7, 2020 8:35 AM

And an AppleScript only solution used in an Automator application to append the dimensions to the end of a selected image name.


use scripting additions

on run argv
	
	if (count of argv) = 0 then return
	tell application "Image Events"
		launch
		repeat with anImg in (item 1 of argv)
			set ix to open anImg
			set ixname to ix's name
			-- punctuate the dimensions with 'x' and change list to string
			-- from {width, height} to "widthxheight"
			set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "x"}
			set dimstr to (dimensions of ix) as text
			set AppleScript's text item delimiters to TID
			close ix
			set name of anImg to my append_dimensions(ixname, dimstr) as text
		end repeat
	end tell
    return
	
end run

on append_dimensions(afile, dimstr)
	-- separate name string into basename and extension array elements
	-- then append dimension string to end of basename and then append extension
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
	set nameLst to text items of (afile as text)
	set AppleScript's text item delimiters to TID
	return ((item 1 of nameLst) & space & dimstr & "." & (rest of nameLst)) as text
end append_dimensions


Similar questions

4 replies
Sort By: 
Question marked as Top-ranking reply

Mar 7, 2020 8:35 AM in response to VikingOSX

And an AppleScript only solution used in an Automator application to append the dimensions to the end of a selected image name.


use scripting additions

on run argv
	
	if (count of argv) = 0 then return
	tell application "Image Events"
		launch
		repeat with anImg in (item 1 of argv)
			set ix to open anImg
			set ixname to ix's name
			-- punctuate the dimensions with 'x' and change list to string
			-- from {width, height} to "widthxheight"
			set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "x"}
			set dimstr to (dimensions of ix) as text
			set AppleScript's text item delimiters to TID
			close ix
			set name of anImg to my append_dimensions(ixname, dimstr) as text
		end repeat
	end tell
    return
	
end run

on append_dimensions(afile, dimstr)
	-- separate name string into basename and extension array elements
	-- then append dimension string to end of basename and then append extension
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "."}
	set nameLst to text items of (afile as text)
	set AppleScript's text item delimiters to TID
	return ((item 1 of nameLst) & space & dimstr & "." & (rest of nameLst)) as text
end append_dimensions


Reply

Mar 1, 2020 5:32 PM in response to Gyro Gearloose

Apple Script's Image Events knows about dimensions, but otherwise AppleScript is clueless about it. Image Events is slow to get the dimensions too, as the following Automator code will have processed three files in the time it takes Image events to open and get the dimensions on one file.


You have coded a Service, but that is the wrong structure to take arguments in from a preceding action.


I just tested the following Run AppleScript action taking input from Ask for Finder items, where I selected three image files in a folder.


Before:


After:


The Automator application workflow:


Replace the contents of a Run AppleScript action with the following copy/pasted code. It uses AppleScript Objective-C and that is a key to its speed.


use framework "Cocoa"
use AppleScript version "2.4" -- Yosemite 10.10 or later
use scripting additions

property NSString : a reference to current application's NSString
property NSBitmapImageRep : a reference to current application's NSBitmapImageRep

on run argv
	if (count of argv) = 0 then return
	tell application "Finder"
		repeat with anImg in (item 1 of argv)
			set name_ext to name of anImg
			set wxh to my img_dimensions(anImg) as text
			if class of wxh is boolean then
				log "foo" -- like a continue statement
			else
				set wxh to space & wxh
				set name of anImg to my append_dimensions(name_ext, wxh) as text
			end if
		end repeat
	end tell
	return
end run

on img_dimensions(afile)
	-- get the width and height of the passed image.
	-- return the wxh string if both are greater than zero
	set imgRep to NSBitmapImageRep's imageRepsWithContentsOfFile:(POSIX path of afile)
	set width to (imgRep's pixelsWide) as integer
	set height to (imgRep's pixelsHigh) as integer
	if width > 0 and height > 0 then
		return (width & "x" & height) as text
	else
		return false
	end if
end img_dimensions

on append_dimensions(afile, dimensions)
	-- insert the dimensions at the end of the basename
	-- and then append the extension.
	set fileStr to NSString's stringWithString:(POSIX path of afile)
	set ext to fileStr's pathExtension()
	set filebase to (fileStr's stringByDeletingPathExtension)'s stringByAppendingString:dimensions
	return ((filebase's stringByAppendingPathExtension:ext)'s lastPathComponent()) as text
end append_dimensions


Reply

Mar 2, 2020 7:27 AM in response to VikingOSX

Or for something more terse, a Zsh script that can be used in a Run Shell Script to do the same thing as the AppleScript/Objective-C example above. Shell: /bin/zsh Pass input: as arguments.


#!/bin/zsh

# get the widthxheight dimensions of an image and
# rename by appending those dimensions to the file's basename.
# Legend: a = absolute path, a:r = path and basename, a:e = extension
#
# Usage: dim.zsh image1.ext image2.ext ... imagen.ext

for f in "$@"
do
    # generate widthxheight string
    wxh=$(/usr/bin/sips -g pixelWidth -g pixelHeight ${f:a} |\
        /usr/bin/awk '{getline; printf "%sx", $2}' | sed -e 's/.$//')
    # rename to image wxh.ext
    /bin/mv "${f:a}" "${f:a:r} $wxh.${f:a:e}"
done



Reply

Mar 2, 2020 1:54 PM in response to VikingOSX

And a further Zsh simplification:

#!/bin/zsh

# get the widthxheight dimensions of an image and
# append that to the file's basename.
# Legend: a = absolute path, a:r = path and basename, a:e = extension
#
# Usage: dim.zsh image1.ext image2.ext ... imagen.ext

for f in "$@"
do
    # generate widthxheight string by joining array elements w/'x' as separator
    wxh=${(j:x:)$(/usr/bin/sips -g pixelWidth -g pixelHeight ${f:a} |\
        /usr/bin/awk '{getline;print $2}')}
    # rename to foo wxh.ext
    /bin/mv "${f:a}" "${f:a:r} ${wxh}.${f:a:e}"
done



Reply

Use Automator to get image dimensions

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