Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

VideoBeagle

macrumors 6502a
Original poster
Aug 17, 2010
823
18
App Q&A testing by request.
So I found a script at Mac Scripter that would go into a folder of subfolders, and rename the files it finds (in the subfolders) based on the subfolder name.
http://macscripter.net/viewtopic.php?id=36664

I reworked it so that it will work on images in the top level folder as well, work even if there is no subfolder, and only change image names.

It works fine, but I think it's likely kludgey code and could be written with better style....but I don't know enough for that :eek:

anyone have any advice on optimizing this?

Code:
set all_files to {}
set filelist to {}
set foldlist to {}
set ImgExtension to {"jpg", "psd", "gif", "png"}

tell application "Finder"
	set mifold to choose folder
	try
		set filelist to (every file of mifold where name extension is in ImgExtension)
		set foldlist to every folder of mifold #as list
	end try
	if foldlist is not {} then
		repeat with eachFolder in foldlist
			--set Base_Name to my MakeBase(eachFolder as string)
			set count_er to 1
			set all_files to (get every document file in eachFolder where name extension is in ImgExtension)
			set finalNamePrefix to (name of (eachFolder) & "_")
			repeat with thisFile in all_files
				--set thisFile's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
				set thisFile's name to (finalNamePrefix & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
				set count_er to count_er + 1
			end repeat
			
		end repeat
	end if
	if filelist is not {} then
		set count_er to 1
		#set all_files to (get every document file in eachFolder)
		set finalNamePrefix to (name of (mifold) & "_")
		repeat with thisFile in filelist
			--set thisFile's name to (Base_Name & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
			set thisFile's name to (finalNamePrefix & (text -3 thru -1 of ("000" & (count_er as string))) & "." & (thisFile's name extension))
			set count_er to count_er + 1
		end repeat
	end if
	beep 3
	
end tell

to MakeBase(txt)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set new_Name_Raw to every text item of txt
	set AppleScript's text item delimiters to "_"
	set final_Name to every text item of new_Name_Raw as text
	set AppleScript's text item delimiters to astid
	return final_Name
end MakeBase
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.