Can an applescript select a jpg in a folder based on physical W and D

Hello,

I have some Photoshop actions that re-size an image different sizes for different purposes. The files are always a particular height and width when completed, like 475 x 440 or 50 x 50, etc…I have 6 sizes all together.

Would it be even possible to have an AS go into that folder and find for example any image that is 50 x 50 and rename it using a certain convention, like the first 8 digits of its existing name_s_1.jpg. For the 475 x 440 ones, it would be named the first 8 digits of its existing name_d_1.jpg…etc…

Is this even possible?
thanks!
babs

Sure it’s possible. But you have to use something to read each image’s width and height. You could use Photoshop to open each image, or ImageEvents (scriptable process that doesn’t have a front-end), or a unix process/shell script (look up SIPS). There are a couple options. The Finder won’t do it, System Events won’t do it.
If I were doing it, I’d write a script that controls Photoshop to do my file-sizing AND saves/names each size to your convention at the same time. Photoshop is pretty scriptable these days (I remember back in PS6 it was almost nothing except “do action…”).
Chris

HI Chris,

Thanks… Unfortunately all the Photoshop actions have already been made, and I am not at liberty to alter them. Just freelancing here and trying to make my life easier by not having to go in and rename each one manually once its done. That is why I was hoping an AS could read the metadata in the file, without actually opening it in PS, and rename it based on that size criteria, but I can see what you are saying about it needing to open in Photoshop in order to get the size…
OK…I;ll keep plugging at it!!! Thanks for giving me some direction here!
babs

This piece of code give an easy access to the picture size.


set source_file to choose file of type {"public.jpeg"}

tell application "Image Events"
	--launch -- always use with Folder Actions
	set this_image to open file (source_file as string)
	set {oldW, oldH} to dimensions of this_image
	close this_image
end tell

activate me
display dialog "width = " & oldW & " points" & return & "height = " & oldH & " points"

Yvan KOENIG (VALLAURIS, France) jeudi 1 janvier 2011 18:08:53

Hi Yvan,

Oh that’s very interesting? I’m thinking about rather than Display dialog, having AS capture that info (on the clipboard maybe?) and do a complete If/Else Scenario?

something like If oldH = 440, then rename it first 8 digits of the file name_d_1.jpg

else … it moves to the next option…if oldH = 50, then rename if the first 8 digits of the file name_s_1.jpg

else…so and on so on…

something like that?
Does that sound feasible?

thanks so much!
babs

Hi,

I would use ‘sips’, as Chris suggested, to get the width and height dimensions. This way you do not even have to open Photoshop!

You can get pixel width of a file with this Unix command:

sips -g pixelWidth Picture1.png (-- Change “Picture1.png” to your image file)

And pixel height with this Unix command:

sips -g pixelHeight Picture1.png (-- Change “Picture1.png” to your image file)

To convert the result to mm divide by 2.8346476237

Regards
Steve.

Ooops! Sorry, looks like I missed a few posts whilst composing my reply.

Hello Steve,
Sorry, that SIPS is way over my head.
Hopefully I can figure something out on the path started here??
thanks!!!
babs :wink:

If you have Adobe Photoshop. do you have Adobe Bridge too.?

This gets the dimensions with sips


set source_file to choose file of type {"public.jpeg"}
set dimensions to paragraphs -2 thru -1 of (do shell script "sips -g  pixelHeight -g  pixelWidth " & quoted form of POSIX path of source_file & " | awk '/pixel/ {print $NF}'")


@ Mark-I do have Bridge, but when I thought about a batch rename, I didn’t see anyway to isolate certain size images in a folder, nor how would I tell it, based on the criteria choices to only use the first 8 digits of the name before adding on the rest of the criteria. But if you have any ideas I am all ears :wink:

@Stefan…hi…long time since I have been on here…hope all is well!!!
Thanks for this info…Can I use this answer showing up in the result box as a way to start thinking about the IF/Then scenario I mentioned earlier in this post?

thanks guys!
:slight_smile: babs

Barbara, in Bridge you can set up to 4 extra lines of info to be visible (usually only file name) see the app pref’s for the choices available. Now you can set and see the pixel dimensions in the GUI. Then choose to sort window by dimensions. This should give you groups of same sized images you can easily select. Either drag each bundle out and rename with something else ( script ) you may be able to rename with Bridge but you will need to use the string and regexp options.

HI Mark,
I think this is doable…I have some familiarity with GREP…
so I will work on it and get back here if I need help!
thanks so much!
babs

Hi Mark…
Worked fine…

I did this search:

(?<=^\d{8}).+$

and replaced it with nothing and with the 3 additional options, I just make it :wink:
thanks!!!
babs