getting dimensions of images and pasting into a text file

Hi, can anyone point me in the right direction

at present i have to paste a large number of web image files into outlook 2011 and then type their file name and dimensions under each file to sent to the clients for approval.

The solution i’m thinking of is to gather the image files, drag them on to an applescript application, which will get the file name and pixel dimension and paste each images with their file names and sizes

i would appreciate anyone help

Paul

Hi Paul,

The UNIX command ‘sips’ can do this without having to open the files in any imaging software.

I have posted a complete script in Code Exchange that will workout the dpi and dimensions in mm of any selected image. (post is called “Get Resolution (dpi) and physical dimensions, in mm of any image file”)

Here it is modified slightly, so as not to convert pixels to mm.

set source_file to choose file
set type to (do shell script "GetFileInfo " & quoted form of POSIX path of source_file & " | cut -d'\"' -f2")

-- sips does not work on eps files so we have to convert to pdf, save as a temp file on the Desktop, then delete it (by default this will create a 150dpi pdf!)
if type contains "EPSF" then
	set epsSaved to do shell script "/usr/bin/pstopdf " & quoted form of POSIX path of source_file & " -o ~/Desktop/tmp.pdf"
	set Res to paragraphs of (do shell script "sips -g  dpiHeight -g  dpiWidth " & quoted form of POSIX path of source_file & " | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
	set dimensions to paragraphs of (do shell script "sips -g  pixelHeight -g  pixelWidth " & quoted form of POSIX path of source_file & " | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
	delay 0.1
	do shell script "rm ~/Desktop/tmp.pdf"
	set Res to round Res
	
else
	set Res to paragraphs of (do shell script "sips -g  dpiHeight -g  dpiWidth " & quoted form of POSIX path of source_file & " | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
	--end if
	set Res to round Res
	set dimensions to paragraphs of (do shell script "sips -g  pixelHeight -g  pixelWidth " & quoted form of POSIX path of source_file & " | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
	
end if
set dpi_to_dpmm to Res / 25.4 -- converts dots per inch to dots per mm
set h to (item 1 of dimensions) --/ dpi_to_dpmm -- works out pixel height as mm
set w to (item 2 of dimensions) --/ dpi_to_dpmm -- works out pixel width as mm

display dialog "Resolution of this file is : " & Res & "dpi" & return & return & "Image is " & h & "px h x " & w & "px w (Approx!)"

This should be a starting point for you!

If you add this into a repeat routine, then rather than display the result, have it write out to a file.

in-toto.


tell application "Image Events.app"
	-- your Image Events.app code goes here
end tell

:slight_smile:

Many thanks for the reply. i did come across the ‘sips’ action script when i was searching the archives. Unfortunately i’m still new to applescripts and my real problem is saving the image files, name and dimension into a text file.

I guess back to Lynda online training for me.

Again many thanks for your help

Paul

I think this will do what you need.

It will ask you to choose a folder containing your images, then extract the dpi and pix width and height, then save the results to a file called ‘results.txt’ to your desktop.

Just be aware that any eps files do get converted to a temporary pdf first and the resolution always seems to be 150dpi, regardless of what the eps file was initially. As sips does not work on eps files, I convert to pdf!

set iFolder to (choose folder with prompt "Select Folder")
set fPath to POSIX path of iFolder
set fContent to list folder iFolder as list without invisibles
repeat with i from 1 to number of items in fContent
	set source_file to item i of fContent
	set fName to do shell script "basename " & quoted form of POSIX path of source_file
	set type to (do shell script "echo " & fName & " | cut -d'.' -f2")
	set wholePath to fPath & source_file
	
	try
		do shell script "touch ~/Desktop/results.txt"
	end try
	try
		-- sips does not work on eps files so we have to convert to pdf, save as a temp file on the Desktop, then delete it (by default this will create a 150dpi pdf!)
		if type contains "eps" then
			set epsSaved to do shell script "/usr/bin/pstopdf " & quoted form of wholePath & " -o ~/Desktop/tmp.pdf"
			set Res to paragraphs of (do shell script "sips -g  dpiHeight -g  dpiWidth ~/Desktop/tmp.pdf | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
			set dimensions to paragraphs of (do shell script "sips -g  pixelHeight -g  pixelWidth ~/Desktop/tmp.pdf | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
			delay 0.1
			do shell script "rm ~/Desktop/tmp.pdf"
			set Res to round Res
			
		else
			set Res to paragraphs of (do shell script "sips -g  dpiHeight -g  dpiWidth " & quoted form of wholePath & " | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
			--end if
			set Res to round Res
			set dimensions to paragraphs of (do shell script "sips -g  pixelHeight -g  pixelWidth " & quoted form of wholePath & " | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
			
		end if
		set dpi_to_dpmm to Res / 25.4 -- converts dots per inch to dots per mm
		set h to (item 1 of dimensions) --/ dpi_to_dpmm -- works out pixel height as mm
		set w to (item 2 of dimensions) --/ dpi_to_dpmm -- works out pixel width as mm
	end try
	
	do shell script "echo 'Resolution of " & fName & " is : " & Res & "dpi - Image is " & h & "px h x " & w & "px w (Approx!)' >> ~/Desktop/results.txt"
	
end repeat

intoto,
that’s fantastic and thank you. I must confess i’m in awe of your script and accept i’m our of my depth and a little confused when sending to a shell script.

Can i be very rude and ask for another favor.

Is it possible to place the actual image file and the ‘results’ stacked under each other in the text file

From this i can then copy and paste into my outlook2011 new email window.

If this can not be done i thank you anyway for what you have already sent me

Paul

Hi Paul,

You are welcome. I’m glad someone else has found this useful!

Below is an amended version of the script. I have broken the last ‘do shell script’ line into the individual lines you require.

set iFolder to (choose folder with prompt "Select Folder")
set fPath to POSIX path of iFolder
set fContent to list folder iFolder as list without invisibles
repeat with i from 1 to number of items in fContent
	set source_file to item i of fContent
	set fName to do shell script "basename " & quoted form of POSIX path of source_file
	set type to (do shell script "echo " & fName & " | cut -d'.' -f2")
	set wholePath to fPath & source_file
	
	try
		do shell script "touch ~/Desktop/results.txt"
	end try
	try
		-- sips does not work on eps files so we have to convert to pdf, save as a temp file on the Desktop, then delete it (by default this will create a 150dpi pdf!)
		if type contains "eps" then
			set epsSaved to do shell script "/usr/bin/pstopdf " & quoted form of wholePath & " -o ~/Desktop/tmp.pdf"
			set Res to paragraphs of (do shell script "sips -g  dpiHeight -g  dpiWidth ~/Desktop/tmp.pdf | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
			set dimensions to paragraphs of (do shell script "sips -g  pixelHeight -g  pixelWidth ~/Desktop/tmp.pdf | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
			delay 0.1
			do shell script "rm ~/Desktop/tmp.pdf"
			set Res to round Res
			
		else
			set Res to paragraphs of (do shell script "sips -g  dpiHeight -g  dpiWidth " & quoted form of wholePath & " | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
			--end if
			set Res to round Res
			set dimensions to paragraphs of (do shell script "sips -g  pixelHeight -g  pixelWidth " & quoted form of wholePath & " | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
			
		end if
		set dpi_to_dpmm to Res / 25.4 -- converts dots per inch to dots per mm
		set h to (item 1 of dimensions) --/ dpi_to_dpmm -- works out pixel height as mm
		set w to (item 2 of dimensions) --/ dpi_to_dpmm -- works out pixel width as mm
	end try
	
	do shell script "echo 'File Name: " & fName & " ' >> ~/Desktop/results.txt"
	do shell script "echo 'Resolution: " & Res & "' >> ~/Desktop/results.txt"
	do shell script "echo 'Dimensions: " & h & "px h x " & w & "px w' >> ~/Desktop/results.txt"
	do shell script "echo ' ' >> ~/Desktop/results.txt"
	
end repeat

I must admit, I am in awe of what can be achieved with Applescript and Unix commands!

Keep scripting.

edit: I have removed the (Approx!) from the end of the Dimensions as this does return the actual pixels.

intoto

Again many thanks and the stacking is perfect you’re a gentleman

I think i misled led you thought. I was trying to get the actual image so it shows:

Physical image file
File Name: e_logo.gif
Resolution: 72
Dimensions: 58px h x 114px w

Physical image file
File Name: e_miva.gif
Resolution: 72
Dimensions: 46px h x 176px w

Again i feel guilty for asking this of you as you have done so much already.

My appreciation to you and have a great weekend

PS. Could you recommend any good training web sites or books for AppleScript

Paul

Hi Paul,

As for adding the actual image to the text file, I’m not sure you can do this with a plain text file (which is how the shell is writing to it!)

You could probably to do this with TextEdit ? ? ?

I have learnt a great deal about Apple script from this website! There are lots of tutorials in the unScripted section.
I also remember that the AS4AS document helped a lot and is something that I still refer to from time to time. It can be downloaded from http://macscripter.net/viewtopic.php?id=24599here.

For Unix commands, http://ss64.com/osx/this website is helpful. Or just ‘Google’!

Regards.

in-toto.

intoto

again thanks for all your help and the directions for applescript commands

Paul

Hi Paul,

I’ve been thinking how you could achieve what you originally asked for.

If you build a html file rather than a txt file, you can then open this in a browser, then select all and copy and paste into a text document. So long as your original images are not too large this may be a workable solution:-

try
	do shell script "rm ~/Desktop/pix/results.html"
end try
-- set html head and body open
do shell script "echo \"<html>\" >>~/Desktop/pix/results.html"
do shell script "echo \"<head>\" >>~/Desktop/pix/results.html"
do shell script "echo \"<title>selected_images</title>\" >>~/Desktop/pix/results.html"
do shell script "echo \"</head>\" >>~/Desktop/pix/results.html"
do shell script "echo \"<body>\" >>~/Desktop/pix/results.html"

set iFolder to (choose folder with prompt "Select Folder")
set fPath to POSIX path of iFolder
set fContent to list folder iFolder as list without invisibles
repeat with i from 1 to number of items in fContent
	set source_file to item i of fContent
	set fName to do shell script "basename " & quoted form of POSIX path of source_file
	set type to (do shell script "echo " & fName & " | cut -d'.' -f2")
	set wholePath to fPath & source_file
	
	try
		-- sips does not work on eps files so we have to convert to pdf, save as a temp file on the Desktop, then delete it (by default this will create a 150dpi pdf!)
		if type contains "eps" then
			set epsSaved to do shell script "/usr/bin/pstopdf " & quoted form of wholePath & " -o ~/Desktop/tmp.pdf"
			set Res to paragraphs of (do shell script "sips -g  dpiHeight -g  dpiWidth ~/Desktop/tmp.pdf | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
			set dimensions to paragraphs of (do shell script "sips -g  pixelHeight -g  pixelWidth ~/Desktop/tmp.pdf | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
			delay 0.1
			do shell script "rm ~/Desktop/tmp.pdf"
			set Res to round Res
			
		else if type contains "html" then
			set fName to "this_is_the_html_file"
			set Res to "n/a"
			set h to "n/a"
			set w to "n/a"
		else
			set Res to paragraphs of (do shell script "sips -g  dpiHeight -g  dpiWidth " & quoted form of wholePath & " | grep dpiHeight | cut -d ':' -f 2 | cut -d ' ' -f 2")
			--end if
			set Res to round Res
			set dimensions to paragraphs of (do shell script "sips -g  pixelHeight -g  pixelWidth " & quoted form of wholePath & " | grep pixel | cut -d':' -f 2 | cut -d ' ' -f 2")
			
		end if
		set dpi_to_dpmm to Res / 25.4 -- converts dots per inch to dots per mm
		set h to (item 1 of dimensions) --/ dpi_to_dpmm -- works out pixel height as mm
		set w to (item 2 of dimensions) --/ dpi_to_dpmm -- works out pixel width as mm
	end try
	
	do shell script "echo \"<img src=file:///Users/yourusername/Desktop/pix/" & fName & " width=\"25%\" height=\"25%\"><br>\">>~/Desktop/pix/results.html" --change yourusername to your actual User Name!!

	do shell script "echo \"File Name = \"" & fName & "\"<br>\" >>~/Desktop/pix/results.html"
	do shell script "echo \"Resolution = \"" & Res & "\"<br>\" >>~/Desktop/pix/results.html"
	do shell script "echo \"Dimensions = \"" & h & " px x\"" & w & " px <br>\" >>~/Desktop/pix/results.html"
	
end repeat

-- close the html body (out of the repeat routine!)
do shell script "echo \"</body>\" >>~/Desktop/pix/results.html"
do shell script "echo \"</html>\" >>~/Desktop/pix/results.html"

To use this you will have to of created a folder at your Desktop called ‘pix’, then place all of your images into this folder (add your username where commented in the script). Run the script and when prompted, select this ‘pix’ folder. The script will then generate a file called ‘results.html’ within this folder. Open this ‘results.html’ file in a browser. You should be able to select all and paste into a blank text/word document. Even if images do not display in the browser, they do seem to come over when pasted into a text document.

This is probably a little messy and could, no doubt be cleaned up a lot!

intoto

i don’t know what to say but again, Thank You, and it’s exactly what I wanted. This really is brilliant. But yet again I feel very embarrassed to as one more time, what is the best way to bring the images in at actual size? I did reduce them to 10% but they seem to be contained inside a square ratio.

Are you based in the UK, I feel I must somehow return the complement.

Again thank you

Paul

Hi Paul,

I think if you replace this line:

do shell script "echo \"<img src=file:///Users/yourusername/Desktop/pix/" & fName & " width=\"25%\" height=\"25%\"><br>\">>~/Desktop/pix/results.html" --change yourusername to your actual User Name!!

with:

do shell script "echo \"<img src=file:///Users/yourusername/Desktop/pix/" & fName & " width=\"25%\"><br>\">>~/Desktop/pix/results.html" --change yourusername to your actual User Name!!

I’ve just deleted the ‘height=25%’ from the html. I didn’t realise that defining just the width would maintain the original aspect ratio! Of course, to bring them in at 100% either change the 25% to 100% or just delete the [width="25%"] part from this line.

As mentioned in my previous post, this will only affect the view of these images in the browser and when/if copied to a text file will come in at 100%.

I am based in the UK and the greatest complement is the fact that you have found this useful! :slight_smile:

Happy to of helped.
intoto

intoto,

All working perfectly and thank you a great applescript to have. Just typical with clients, give them something that makes life easy for them and they want to change it around.

Again many thanks and all the best :D=D

Hi intoto

how are you keeping. You help me a last month a a project to get the dimensions from jpeg files which is still working and very well.

Would you mind if i could ask your help on another project.

again hope you are well

Paul

Hello

I have small problems with your scripts.

The first one started with:


set source_file to choose file
set type to (do shell script "GetFileInfo " & quoted form of POSIX path of source_file & " | cut -d'\"' -f2")

When I ran it I got this log report :

tell application “AppleScript Editor”
choose file
→ alias “Macintosh HD:Users::Documents:tempo:2012-04-19T18.42.51.png”
end tell
tell current application
do shell script “GetFileInfo ‘/Users//Documents/tempo/2012-04-19T18.42.51.png’ | cut -d’"’ -f2”
→ “”
end tell

which seems a bit annoying.

In a more recent one I found :


set source_file to choose file
set fName to do shell script "basename " & quoted form of POSIX path of source_file
set type to (do shell script "echo " & fName & " | cut -d'.' -f2")

This time, the log rteport was :

tell current application
do shell script “basename ‘/Users//Documents/tempo/2012-04-19T18.42.51.png’”
→ “2012-04-19T18.42.51.png”
do shell script “echo 2012-04-19T18.42.51.png | cut -d’.’ -f2”
→ “42”
end tell
Résultat :
“42”

It’s annoying too. :wink:

Yvan KOENIG (VALLAURIS, France) jeudi 19 avril 2012 19:01:21

Hi Paul,

Create a new post and I’ll take a look. That way, if anyone else can answer it you may get several solutions!

@Yvan,

The “cut -d” part of the shell script will define a delimiter. In you first example there is no "" so asking for the second item (-f2) will result in “” (nothing).

The second example uses the “.” as a delimiter and was used on the understanding/assumption that a file name would only contain 1 full point always at the end of the filename, followed by the filetypes extension (e.g. “.jpg” “.doc”). Your example has “.” within the filename too.

I don’t claim to be an expert in either Unix or (especially not) Applescript. If there is a better or easier way the same results could be achieved (I’m sure there is!) I am more than happy to learn.

I am also aware that, while all of the scripts I have written do work for me on my system, they may well not work on multiple systems with different OS’s and set-ups. In fact, what is the best way to ensure any script/app is universally useable?

in-toto

Hello

I tried to highlight the fact that the script fails when the pictures treated are screenshots created by the good old cmd + shift + 4 call which include the time with the format hh.mm.ss.

I would not trigger a shell script to get the name extension.

version #1


set source_file to choose file
tell application "System Events" to tell disk item (source_file as text)
	set fName to name
	name extension
end tell
set type to result

version #2 using Shane Stanley’s free ASObjC Runner.app


set source_file to choose file
tell application "ASObjC Runner" to (about file source_file)
set {fName, type} to {name, name extension} of result

Yvan KOENIG (VALLAURIS, France) vendredi 20 avril 2012 14:25:13

Hi Yvan,

Version #1 returns JUST the extension (I’m guessing Version #2 does the same).

How would you get the filename WITHOUT the extension and without having to open it? (eg “Untitled.txt” as just “Untitled”)

Also, when I do “cmd + shift + 4”, my files are saved just as Picture 1.png. I am sure this is a user preference too.

Coming from a ‘Repro’ background and having used an old Sun ‘Sparc’ workstation to scan images to, I guess I have inherited the strict naming convention where you could not even use spaces in file names (using the underscore instead!)

I don’t know which is the operating system which you are using but since Lion, the screenshot are named according to the date and the time.

In the French system, by default, they are named : « Capture d’écran 2012-04-20 à 10.11.12.png »
I edited the resource defining the beginning string to remplace it by © but the main component is defined by a resource of type :« @1 Ã @2 » where @1 is the variable date and @2 is the variable time using the UTC format.

Getting the bare file name doesn’t need that we open the file. It’s a trivial task.


set source_file to choose file
tell application "ASObjC Runner" to (about file source_file)
set {fName, type} to {name, name extension} of result
if type is "" then
	set bareName to fName
else
	set bareName to text 1 thru -((count of type) + 2) of fName
end if

It return :
fName which is the complete name : « Capture d’écran 2012-04-20 à 11.12.13.png »
type (which is a bad choice of variable name) which is the name extension : « png »
and bareName which is the name deprieved of the name extension : « Capture d’écran 2012-04-20 à 11.12.13 »

The first script is returning
fName which is the complete name : « Capture d’écran 2012-04-20 à 11.12.13.png »
type (which is a bad choice of variable name) which is the name extension : « png »
To get the bare name it requires the same complementary code.


set source_file to choose file
tell application "System Events" to tell disk item (source_file as text)
	set fName to name
	name extension
end tell
set type to result
if type is "" then
	set bareName to fName
else
	set bareName to text 1 thru -((count of type) + 2) of fName
end if

If you read carefully youn will see that I didn’t defined the variable type in the block tell application “System Events” because type is a property defined in this application’s dictionary.
This is why I wrote that naming a variable « type » is a bad choice.

Yvan KOENIG (VALLAURIS, France) vendredi 20 avril 2012 21:44:37