Renaming and Deleting JPGs on each folder....

Hi guys.

This is what I’m trying to accomplish. I think this might be able to be done in automator but i dont know how either so whatever way works is good with me. I want a script that goes into one folder and checks each subfolder and deletes all the JPEGs in these subfolders except for the largest one. It also needs to be able to view all the hidden files and the protected files because some of the jpgs i want to delete are hidden.

I want to be able to delete all the other JPGs in the subfolders excpet for the largest one. the largest size is usually 200 x 200. but that may vary so i just want to be able to keep the largest one inside.

How can i do this?

Thanks

Browser: Firefox 2.0.0.1
Operating System: Mac OS X (10.4)

Easily. You don’t say how deeply nested the subfolders are - do the subfolders have subfolders or is each of them flat? Also, how do you want to define “largest”? - by pixel count (height x width).

If your contained folders are only one deep, and if area is the determiner, then something like this (by no means optimal);

EDITED: replaced sort with find largest


set F to (choose folder with prompt "Choose the folder containing all others to be processed" without invisibles) -- or an alias to a folder path
-- get all the folder paths
tell application "Finder"
	try
		set picFldrs to folders of entire contents of F as alias list
	on error -- if there's only one folder in the containing folder (an AppleScript bug)
		set picFldrs to folders of entire contents of F as alias as list
	end try
end tell
-- go through each one
repeat with Fldr in picFldrs
	-- if they're all JPEGs for sure, drop the whose phrase - it's slow
	tell application "Finder" to set picList to (files of Fldr whose name extension is in {"jpg", "jpeg"}) as alias list -- use the "try" construction if there's a folder with only one image
	-- get sizes
	set Areas to {}
	tell application "Image Events"
		activate
		repeat with aPic in picList
			set I to open aPic -- get the image from the file
			set D to dimensions of I
			set end of Areas to (item 1 of D) * (item 2 of D) -- product of dimensions
			close I -- don't forget
		end repeat
	end tell
	set Big to findLargest(Areas)
	tell application "Finder"
		repeat with k from 1 to (count picList)
			if k is not Big then delete item k of picList
		end repeat
	end tell
end repeat

to findLargest(aList)
	set C to count aList
	set Temp to 0
	repeat with k from 1 to C
		tell item k of aList to if it > Temp then
			set Temp to it
			set B to k
		end if
	end repeat
	return B
end findLargest

Ok. I will definitely try this when I get home.
The subfolders vary on how deep they go. Its one music folder inside is one folder per artist and then inside that are subfolders for their albums. Each artist has different amount of albums so it changes on how deep they go.

As far as size is concerned…I woul say its by pixel size. The largest is usually 200 x 200 but it might vary as well. Thats why I just need the largest to stay in each folder and delete the rest of them.

Thank you for the help and I’ll give this a try this afternoon…

It should look something like this

MUSIC
ARTIST
ALBUM
ALBUM
ARTIST
ALBUM
ARTIST
ARTIST
ALBUM
ALBUM
ALBUM

/END

Etc…

Is just one folder with many subfolders inside.

The solution posted presumes that you pick a folder containing only images, not folders containing folders that might contain them. It’s easy to get all the files in a nested group of folders, and to get all the folders too, but then the files have to be associated with the folders to assure that only one is left in each. My script doesn’t do that.