trouble modifying existing script: rename files by enclosing folder

hi all,

i found this script which gets me close:
http://macscripter.net/viewtopic.php?id=36664

my goal is a bit different. Like others looking for this script, I have a ton of finder folders with the ‘correct’ names. And, inside of each folder I have a number of files (usually 3); each of a different file type.

I’d like to get the name of the enclosing folder, and rename each of its enclosed files to exactly the same name as the folder, the only difference being their file extension.

so, regardless of the original file names inside each folder, i’d end up with items named like their parent folder:

01-atlas layout
01-atlas layout.idd
01-atlas layout.psd
01-atlas layout.jpg

02-APS design
02-APS design.idd
02-APS design.psd
02-APS design.jpg

so, here’s what little modification i’ve done so far, with the issues i’m having below it:


tell application "Finder"
	set a to every folder of entire contents of (choose folder)
	repeat with aa in a
		set Base_Name to my MakeBase(aa as string)
		set count_er to 1
		set all_files to (every file in aa)
		repeat with ff in all_files
			set ff's name to (Base_Name & "." & (ff's name extension))
			set count_er to count_er + 1
		end repeat
		
	end repeat
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

you’ll see that i’ve removed out the sequential counter. But, in doing so, I’ve also messed up the script and I can’t tell why. Now, when I run the script, the first item in each folder is completely missing its file extension; leading me to believe i’ve somehow messed up the counter. but, i can’t find the issue.

and, my 2nd issue is that i can’t seem to modify the path command to only take the final folder name and not the entire path of the folder to the file name.

any help is appreciated.

Model: iMac
AppleScript: 2.2.4
Browser: Safari 537.36
Operating System: Mac OS X (10.7)

ok, follow up. i actually fumbled around long enough to get my task done. but, i’d like to have a working script for the future when this comes up again.

so, issue one–the file extension not propogating on one file:

turned out it was NOT a counter issue, but a .PTB file that was in the folders and was unrecognized. the others are apparently. if anyone has any ideas for hardcoding in some known exemptions like that–suggestions welcome.

for issue two, have a look at the code again.

tell application "Finder"
	set a to every folder of entire contents of (choose folder)
	repeat with aa in a
		set Base_Name to my MakeBase(aa as string)
		set count_er to 1
		set all_files to (every file in aa)
		repeat with ff in all_files
			set ff's name to (Base_Name & "." & (ff's name extension))
			set count_er to count_er + 1
		end repeat
		
	end repeat
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 "_"
	-- never got variable to calculate final folder working
	-- set finalFolder to (count of text items) as text
	-- set finalFolder to finalFolder - 1
	set final_Name to seventh text item of new_Name_Raw as text
	set AppleScript's text item delimiters to astid
	return final_Name
end MakeBase

specifically this bit:

	set final_Name to seventh text item of new_Name_Raw as text

i ended up hard coding in the level of the folder that needed to be used for naming.

using “Last” tried to rename the file with its own file name (Last being the filename, not the last folder name).

So, does anyone have suggestions for getting this line to pass the second-to-last item? i tried creating a variable --the commented out bit right above the ‘set seventh text item’ bit. but, never could get it to work.

thanks again.

Hi. You can filter for the extensions you want with a whose clause, and you can select from the end with a negative value; i.e., "item -2’ would be the second from last item. I’ve reworked this with more descriptive variables and without ASTIDs”they are surplus.


set counterVar to 2 --might be 1, if you go for the original being named with an ordinal

tell application "Finder" to repeat with aFile in ((choose folder)'s entire contents's files whose name ends with ".idd" or name ends with ".jpg") as alias list
	set {contaiName, nameExten} to {aFile's container's name, aFile's name extension}
	try
		set aFile's name to contaiName & "." & nameExten
	on error --file exists
		set aFile's name to contaiName & counterVar & "." & nameExten
		set counterVar to counterVar + 1
	end try
end repeat

Edit: To prevent duplicates, the above uses a consecutive numbering system that spans folders and ignores file suffixation; i.e., a duplicate file in the first folder may end with 2 and then start with 3 in the next folder”or unlike suffix. In hindsight, this may be undesirable. This will serialize relative to the folder and file type:


tell application "Finder" to repeat with aFolder in ((choose folder)'s entire contents)'s folders as alias list
	repeat with fileExten in {".indd", ".psd", ".jpg"}
		set counterVar to 2
		repeat with afile in (aFolder's files whose name ends with fileExten) as alias list
			set {contaiName, nameExten} to {afile's container's name, afile's name extension}
			try
				set afile's name to contaiName & "." & nameExten
			on error
				set afile's name to contaiName & counterVar & "." & nameExten
				set counterVar to counterVar + 1
			end try
		end repeat
	end repeat
end repeat