Processing images in nested folders

I’m using a script that I put together about a year ago with quite a bit of help from this board. It is working great, but I need to make a change that I can’t figure out.

Right now, I drop a folder of images on my script, and all is well. But now I want the script to process several sub-folders full of images. How can I make that simple change?

Here is what i have now


on run
	open {choose folder}
end run

on open the_folder
	set the_folder to item 1 of the_folder
	set the_images to list folder the_folder without invisibles
	repeat with i from 1 to count of the_images
		set the_image to (the_folder as string) & (item i of the_images) as alias
		try
			tell application "Finder"

 -- Image processing here

	     end try
	end repeat
end open

I’m very new to writing scripts so I’m getting a little frustrated at this point. I’ve tried to get the count of folders within the folder but I’m just spinning my wheels. Can somebody get pointed in the right direction?

Thanks.

The following code, based loosely on this code by JJ, should provide the recursion that you need. Simply modify the processFile routine at the end of the script so that it addresses a single file. The rest of the script shouldn’t need to be modified and it will process the contents of all subfolders.

on run
	open {choose folder}
end run

on open the_folder
	repeat with item_ in the_folder
		my processUnknownItem(item_)
	end repeat
end open

to processUnknownItem(item_)
	if folder of (info for item_) is true then
		my processFolder(item_)
	else
		my processFile(item_)
	end if
end processUnknownItem

to processFolder(folder_)
	tell application "Finder"
		set count_ to count (get items of folder_)
		if count_ > 1 then
			set items_ to (items of folder_) as alias list
		else
			if count_ is 1 then
				set items_ to ((item 1 of folder_) as alias) as list
			else
				if count_ is 0 then
					set items_ to {}
				end if
			end if
		end if
	end tell
	
	repeat with item_ in items_
		my processUnknownItem(item_)
	end repeat
end processFolder

to processFile(file_)
	-- Image processing here
	-- do something to file_ (file_ is an alias reference)
end processFile

– Rob