Optimize help: Rename images by folder name

Discussion in 'Mac Programming' started by VideoBeagle, Sep 1, 2014.

  1. VideoBeagle macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #1
    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
     
  2. VideoBeagle thread starter macrumors 6502a

    Joined:
    Aug 17, 2010
    Location:
    App Q&A testing by request.
    #2
    Hmm..realized this doesn't go more than one folder deep...need to think on that.
     

Share This Page