Finder file naming convention change script

I am trying to write a script that will update a filename. I am not sure how to begin since I think the scope could expand in the future. Files are updated slightly, but there are hundreds or maybe thousands in various forms. Most are either 9 or 16 character core names with “A1” or “AA” changing to “_A” or “_1”.

03EM01302BZ08023A1.eps
to
03EM01302BZ08023_1.eps

03EM01302A2.eps
to
03EM01302_2.eps

03EM01302BZ08023AA.eps
to
03EM01302BZ08023_1.eps

Here is where is keep returning to.


copy text item delimiters of AppleScript to savedDelim
set text item delimiters of AppleScript to {":"}
set text item delimiters of AppleScript to savedDelim

set theFiles to choose file with prompt "Choose the files to process. Select multiple files with the Command (Apple) key." with multiple selections allowed without invisibles
repeat with oneFile in theFiles
	tell application "Finder"
		set fileName to name of oneFile as string
		set folderName to folder of oneFile as string
		set theDesktop to path to desktop folder as Unicode text
		set deleteFolder to (theDesktop & "Delete")
		set fileChar to characters of fileName as list
		set {ext} to {name extension} of (info for oneFile)
		
	end tell
end repeat

I think I get close to where I need to be and realize i am a bit too specific on one file…

copy text item delimiters of AppleScript to savedDelim
set text item delimiters of AppleScript to {":"}
set text item delimiters of AppleScript to savedDelim

set theFiles to choose file with prompt "Choose the files to process. Select multiple files with the Command (Apple) key." with multiple selections allowed without invisibles
repeat with oneFile in theFiles
	tell application "Finder"
		set fileName to name of oneFile as string
		set folderName to folder of oneFile as string
		set theDesktop to path to desktop folder as Unicode text
		set deleteFolder to (theDesktop & "Delete")
		set fileChar to characters of fileName as list
		set UINname to items 1 through 16 of fileChar
		display dialog "Choose the type of file being updated." & return & return & fileName buttons {"Main", "Foil"} default button 1
		set returnResult to button returned of result
		
		if returnResult is "Main" then
			set dialogResult to "Main"
			
			if character 19 of fileName is "." then --This tells us that the UIN is a 16-digit in the old-style naming-convention
				if character -4 of fileName is "." then
					if character -6 of fileName is "A" then
						set char17 to "_"
					end if
					if character -5 of fileName is "1" then
						set char18 to "A"
					end if
					if character -5 of fileName is "2" then
						set char18 to "B"
					end if
					if character -5 of fileName is "3" then
						set char18 to "C"
					end if
					if character -5 of fileName is "4" then
						set char18 to "D"
					end if
					
					set extDot to number of character "." of fileChar as string --Left off here
					set fileExt to items extDot through -1 of fileChar as string
					display dialog "Main"
					set newOneFile to "" & UINname & char17 & char18 & fileExt as string
					--tell application "Finder"
					if exists file newOneFile of folder folderName of application "Finder" then
						display dialog "The file already exists. Would you like to delete the existing " & return & fileName & "?"
						if result is "OK" then
							if not (exists deleteFolder) then
								make new folder at folder (path to desktop) with properties {name:"Delete"}
							end if
							move file (newOneFile to path to desktop & "Delete") with replacing
							
						end if
					end if
					--end tell
					try
						set name of oneFile to newOneFile --This line gives a 'Finder got an error: The operation could not be completed because there is already an item with that name.' if the file already exists.
					end try
					
				end if
			end if
			
			if character 17 of fileName is "." then --This is for the .ART1, etc. filnaming
				if character 18 of fileName is "A" and character 19 of fileName is "R" and character 20 of fileName is "T" then
					if character -6 of fileName is "A" then
						set char17 to "_"
					end if
					if character -5 of fileName is "1" then
						set char18 to "A"
					end if
					if character -5 of fileName is "2" then
						set char18 to "B"
					end if
					if character -5 of fileName is "3" then
						set char18 to "C"
					end if
					if character -5 of fileName is "4" then
						set char18 to "D"
					end if
					
					set fileExt to items -4 through -1 of fileChar
					display dialog "Item-Foil"
					set newOneFile to "" & UINname & char17 & char18 & fileExt --This line gives a 'Finder got an error: The operation could not be completed because there is already an item with that name.' if the file already exists.
					set name of oneFile to newOneFile
				end if
			end if
			
		end if
		
		if returnResult is "Foil" then
			set dialogResult to "Foil"
			if character 10 of fileName is "." then --This tells us that the UIN is a 9-digit
			end if
		end if
		
	end tell
end repeat

Any thoughts on how to keep this a bit more focused and simple at the same time?

Levon

Model: Dual 2.3GHz G5
AppleScript: 2.1.1/Script Debugger 4
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

If you have up to thousands of pictures, allow room for at least 7 additional characters in your file naming convention. Tag the front AND back of your filename.

For example:
Use a 2-4 letter code on the front to catagorize your eps graphics (BI-Business Icons, BB-Business Backgrounds, SW-Sports Water, SB-Sports Backgrounds, S1-Sports People, or by date 2000 etc.). Next use have your core name, then a delimeter, I like the underscore too; “_”. Finally, use a plain ol’ 4-6 digit serial number (for a 1000’s of files).

BI 03EM01302BZ08023 _ 0001 .eps – by category BI
20061112 03EM01302BZ08023 _ 0001 .eps – by date
2006BI01 03EM01302BZ08023 _ 0001 .eps – combo date, category and serialized

This will sort nicely by category or date or whatever else you can come up with. This is just an example as I don’t know the content of the eps graphics; how diverse or narrow the image categories are. But, tagging the front AND back of the filename greatly improves your naming options.

good luck!
contactzero

Maybe this is a bit more descriptive. I have folders within folders. The top-level folder will be chosen. Inside will be a few dozen sub-folders. These will have names like TK07_Apr_ParentBroch_G5M_SP or 05SR01509BZ07029. Inside these sub-folders are a variety of design files like Quark XPress, Illustrator, Photoshop documents, among others. Usually, the filenames will be sequential in a list, with the first 8 characters or so being the same in a folder. I want to be able to quickly change specific characters (character position) to another character for an entire folder, whether it is the top-level’s contained folders or the sub-folder’s contained files. Here is a bit of what I am thinking about now.

set FileSample to choose folder with prompt "Choose a folder that you want to process."
set folderName to name of (info for FileSample)

copy text item delimiters of AppleScript to savedDelim
set text item delimiters of AppleScript to {":"}
set text item delimiters of AppleScript to savedDelim

repeat with x in FileSample
	set itemInfo to info for item x of FileSample
	
	if x is folder then
		display dialog "Which character position should be changed?" default answer ""
		set charPos to text returned of result
		set displayPos to charPos
		set charPos to charPos as real
		
		display dialog "Change position " & displayPos & " to what?" default answer ""
		set newChar to text returned of result
		
		display dialog "Change position " & displayPos & " of the UIN to \"" & newChar & "\"?" buttons {"No", "Yes"} default button "Yes" with icon 2
		if button returned of result is "No" then
			display dialog "No changes made per your request." buttons {"OK"} default button 1 with icon 1
			return
		end if
	end if
	
	if x is file then
		
		if file type of x is {"XPRJ", "XPR3"} then
			
		end if
		
		if name extension of x is in {"tif", "eps", "ai", "ART1", "ART2", "ART3", "ART4"} then
			
			
		end if
		
	end if
	
end repeat

Model: Dual 2.3GHz G5
AppleScript: 2.1.1/Script Debugger 4
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.4)

Hi does this help at all?

set filename to "03EM01302BZ08023A1.eps"
set search_string to text returned of (display dialog "enter the text you would like to search for" default answer "A1")
set search_string_count to count characters of search_string
set replacement_string to text returned of (display dialog "enter replacement text" default answer "_1")

set myoffset to offset of search_string in filename
set string_part1 to characters 1 thru (myoffset - 1) of filename as string
set string_part2 to characters (myoffset + search_string_count) thru end of filename as string
set new_string to string_part1 & replacement_string & string_part2 as string

Using the example filenames you give I think this should search and replace as you require. If this is the kind of thing you need then we can build on it!
Nik