List of image dimension

Hi,

I’m really new to Applescript and at the moment only want to use it to do one really simple thing. I have a large number of image files (jpgs) in a folder. I would like to use Applescript to generate a list of the image filenames and relevant dimensions, to look something like this:

image1,200x400
image2,350x276
image3,200x345

Can anyone help me please? I’ve had a go myself, but can’t seem to get very far! :slight_smile:

Thanks…

MARTIN

Of course! This is MacScripter :smiley:

Here is the short script (droplet) I just wrote while having dinner. I hope it gets you started.


property mytitle : "Sizomago"

-- I am called when the user starts the script with a doubleclick
on run
	tell me
		activate
		display dialog "Please drop image files onto my icon in order to crate a list of their dimensions." buttons {"OK"} default button 1 with icon note with title mytitle
	end tell
end run

-- I am called when the user drops finder items onto the script's icon
on open finderitems
	try
		set finderfiles to {}
		-- scanning the dropped finder items for files
		repeat with finderitem in finderitems
			set finderiteminfo to (info for finderitem)
			if not (folder of finderiteminfo) then
				set finderfiles to finderfiles & finderitem
			end if
		end repeat
		-- no files...
		if finderfiles is {} then
			set errmsg to "You did not drop any files onto the script."
			my dsperrmsg(errmsg, "--")
			return
		end if
		-- creating the list of image dimensions using the built-in sips tool
		set imgsizereport to ""
		repeat with finderfile in finderfiles
			set filename to name of (info for finderfile)
			set qtdfilepath to quoted form of (POSIX path of (finderfile as text))
			try
				set command to "sips -g pixelHeight -g pixelWidth " & qtdfilepath
				set output to paragraphs of (do shell script command)
				set pixelheight to item 2 of (words of item 2 of output)
				set pixelwidth to item 2 of (words of item 3 of output)
				set imgsizeentry to filename & "," & pixelheight & "x" & pixelwidth & return
				set imgsizereport to imgsizereport & imgsizeentry
			end try
		end repeat
		-- everything just went wrong
		if imgsizereport is "" then
			set errmsg to "Could not detect any image sizes."
			my dsperrmsg(errmsg, "--")
			return
		else
			-- no more calls, we have a winner!
			tell application "TextEdit"
				activate
				make new document
				set text of document 1 to imgsizereport
			end tell
		end if
	on error errmsg number errnum
		-- ignoring 'User canceled'-error
		if errnum is not equal to -128 then
			my dsperrmsg(errmsg, errnum)
		end if
	end try
end open

-- Well, I am displaying error messages (hopefully only few...)
on dsperrmsg(errmsg, errnum)
	tell me
		activate
		display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop with title mytitle
	end tell
end dsperrmsg

Thanks so much - just seen your reply. However, I don’t understand the dropping images onto the icon bit. Is there anyway that I can just point this script at a particular folder with around 5,000 images, and let it generate the list of filenames and x/y dimensions?

I’m HUGELY grateful for your help…!

MARTIN

I have modified the run handler, now you are asked to choose a folder when you start the script with a doubleclick:



property mytitle : "Sizomago"

-- I am called when the user starts the script with a doubleclick
on run
	set imgfolder to (choose folder with prompt "Please choose a folder containing image files:" without multiple selections allowed, invisibles and showing package contents)
	tell application "Finder"
		set finderitems to (every file in imgfolder) as alias list
	end tell
	open finderitems
end run

-- I am called when the user drops finder items onto the script's icon
on open finderitems
	try
		set finderfiles to {}
		-- scanning the dropped finder items for files
		repeat with finderitem in finderitems
			set finderiteminfo to (info for finderitem)
			if not (folder of finderiteminfo) then
				set finderfiles to finderfiles & finderitem
			end if
		end repeat
		-- no files...
		if finderfiles is {} then
			set errmsg to "You did not drop any files onto the script."
			my dsperrmsg(errmsg, "--")
			return
		end if
		-- creating the list of image dimensions using the built-in sips tool
		set imgsizereport to ""
		repeat with finderfile in finderfiles
			set filename to name of (info for finderfile)
			set qtdfilepath to quoted form of (POSIX path of (finderfile as text))
			try
				set command to "sips -g pixelHeight -g pixelWidth " & qtdfilepath
				set output to paragraphs of (do shell script command)
				set pixelheight to item 2 of (words of item 2 of output)
				set pixelwidth to item 2 of (words of item 3 of output)
				set imgsizeentry to filename & "," & pixelheight & "x" & pixelwidth & return
				set imgsizereport to imgsizereport & imgsizeentry
			end try
		end repeat
		-- everything just went wrong
		if imgsizereport is "" then
			set errmsg to "Could not detect any image sizes."
			my dsperrmsg(errmsg, "--")
			return
		else
			-- no more calls, we have a winner!
			tell application "TextEdit"
				activate
				make new document
				set text of document 1 to imgsizereport
			end tell
		end if
	on error errmsg number errnum
		-- ignoring 'User canceled'-error
		if errnum is not equal to -128 then
			my dsperrmsg(errmsg, errnum)
		end if
	end try
end open

-- Well, I am displaying error messages (hopefully only few...)
on dsperrmsg(errmsg, errnum)
	tell me
		activate
		display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop with title mytitle
	end tell
end dsperrmsg

If you need the script to just process one specific folder, which always stays at the same location, you can also modify the run handler as follows:


-- I am called when the user starts the script with a doubleclick
on run
	-- hard coded path...
	set imgfolderpath to "Macintosh HD:Users:martin:Desktop:"
	tell application "Finder"
		set finderitems to (every file in (imgfolderpath as alias)) as alias list
	end tell
	open finderitems
end run

This is just amazing!!! Thank you so much. I’ve wanted to be able to do something like this for years. You have been so incredibly helpful. I will be using this all the time now - it’s going to save me a lot of time.

Can’t thank you enough… :smiley:

Hi new here this script works great, is there a way instead of displaying height & width in pixel to display it in inches?

That’s easy.

The script is already using sips to extract data from the image.

Just extend that to ask for dpiHeight and dpiWidth to get the pixel density, then divide the pixel count by the density and you have your answer in inches:

Instead of:

...
				set command to "sips -g pixelHeight -g pixelWidth " & qtdfilepath
				set output to paragraphs of (do shell script command)
				set pixelheight to item 2 of (words of item 2 of output)
				set pixelwidth to item 2 of (words of item 3 of output)
				set imgsizeentry to filename & "," & pixelheight & "x" & pixelwidth & return
...

You can say:

...
		set command to "sips -g pixelHeight -g pixelWidth -g dpiHeight -g dpiWidth " & qtdfilepath
		set output to paragraphs of (do shell script command)
		set pixelheight to item 2 of (words of item 2 of output)
		set pixelwidth to item 2 of (words of item 3 of output)
		set dpiHeight to item 2 of (words of item 4 of output)
		set dpiWidth to item 2 of (words of item 5 of output)
		set imgsizeentry to filename & "," & (pixelheight / dpiHeight) & "x" & (pixelwidth / dpiWidth) & return
...

Just be aware that resolution doesn’t really mean anything absent an appropriate medium, eg you are printing an image to paper.

What is the ultimate objective here?