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)