I have this really, really UGLY extension detection routine I could use some help streamlining.
Before I display the ugly code, here’s what it has to do:
–Determine the file extension, taking into account 2-digit, 3-digit, and 4-digit extensions (.ai, .eps, .indd as examples)
–It also has to take into account “special” extensions we used for quite a while (long story) that are 6-digit (like .eps_lr)
–It also has to take into account the possibility of false extensions added by our asset management software, which causes a whopping 10-digit extension (like .eps_lr.tif, notice the TWO periods)
The last one requires a bit of explanation. The asset manager often plays games with the extensions…sometimes adding it’s own, sometimes replacing with it’s own. Usually it replaces (xxx.eps becomes xxx.pdf), but because those 6-digit special extensions aren’t recognized, it simply tacks-on an extension (xxx.eps_lr becomes xxx.eps_lr.pdf).
So in the third case I need to know the “real” extension (.eps_lr) and not the false part on the end added by the asset manager. Ugly, yes? :rolleyes:
My methodology as shown is to first capture the extension, then suss-out the false, super-long extensions as needed.
Oh, as a reminder to folks not used to my scripts, logMe is a generic log file writer I use. Feel free to strip that out to give feedback, I can always put stuff back as needed. There is ALOT of logging because I needed to track every logic step this script made to be 100% certain it got it right, there is NO margin for error either in this script or the larger parent script it is part of (this is a chunk of my File Type/Creator Type Repair software from Code Exchange).
Okay, here’s what I have, which works like a charm, but isn’t exactly pretty:
--
-- lr/hr file extension parser
--
on lrhrParse(name_type, path_to_fix)
tell application "Finder"
set file_to_fix to name of path_to_fix as string
set name_length to length of file_to_fix as number
end tell
if name_type is "long" then
set extension_length to 10
else
set extension_length to 6
end if
if name_length is greater than extension_length then
set this_extension to (text items (name_length - extension_length) thru name_length of file_to_fix) as string
--check validity of extension (trap for false "." characters)
set extension_validate to (text items 5 thru 7 of this_extension) as string
if extension_validate is not in {"_lr", "_hr"} then
return "<invalid>"
else
return this_extension
end if
else
return "too short"
end if
end lrhrParse
--
-- Determines "real" extension
--
on extensionFinder(path_to_fix)
logMe("extensionFinder Handler Called", 3)
--get some basic Finder information
tell application "Finder"
set file_to_fix to name of path_to_fix as string
set name_length to length of file_to_fix as number
end tell
logMe("name length = " & name_length, 4)
--parse end of file name for possible extension types
--
--define possible "_lr/_hr + CCM" extension, watch for files names too short for this check
set lrhr_long_extension to lrhrParse("long", path_to_fix)
logMe("lrhr_long_extension = " & lrhr_long_extension, 4)
--define possible _lr/_hr extension, watch for files names too short for this check
set lrhr_short_extension to lrhrParse("short", path_to_fix)
logMe("lrhr_short_extension = " & lrhr_short_extension, 4)
--define any Finder-recognized extension
set standard_2_extension to (text items (name_length - 2) thru name_length of file_to_fix) as string --like Illustrator's .ai
logMe("standard_2_extension = " & standard_2_extension, 4)
set standard_3_extension to (text items (name_length - 3) thru name_length of file_to_fix) as string --standard 3-letter extensions
logMe("standard_3_extension = " & standard_3_extension, 4)
set standard_4_extension to (text items (name_length - 4) thru name_length of file_to_fix) as string --like InDesign's .indd
logMe("standard_4_extension = " & standard_4_extension, 4)
--figure out which extension type captured above is the "real" one
--
--is it "_lr/_hr + CCM"?
if text item 1 of lrhr_long_extension is "." then
logMe("EXTENSION TYPE: CADS-style Long (CADS-style + CCM)", 4)
--remove the extra CCM extension
set fixed_long_extension to (text items 1 thru ((length of lrhr_long_extension) - 4) of lrhr_long_extension as string)
set my_extension to fixed_long_extension
logMe("¢ CONVERT TO CADS-style SHORT: " & fixed_long_extension, 4)
else
-- is it simply _lr/_hr?
if text item 1 of lrhr_short_extension is "." then
logMe("EXTENSION TYPE: CADS-style Short", 4)
set my_extension to lrhr_short_extension
else
--is it something like InDesign (.indd)
if text item 1 of standard_4_extension is "." then
logMe("EXTENSION TYPE: Finder Long", 4)
set my_extension to standard_4_extension
else
--is it a typical 3-digit Finder?
if text item 1 of standard_3_extension is "." then
logMe("EXTENSION TYPE: Finder Typical", 4)
set my_extension to standard_3_extension
else
--is it something like Illustrator (.ai)
if text item 1 of standard_2_extension is "." then
logMe("EXTENSION TYPE: Finder Short", 4)
set my_extension to standard_2_extension
else
--no extension
logMe("EXTENSION TYPE: none", 4)
set my_extension to "none"
end if
end if
end if
end if
end if
logMe("extensionFinder Handler Finished", 3)
return my_extension as string
end extensionFinder