Methods I’ve been known to use:
–Nesting IFs, like you have
–Testing each condition separately, set a variable to a value, then check both values at the same time via “if [this=something] and [that=something] then…”
–In extremely complex conditionals tests: test a condition with nested IFs grouped in a logical manner. If the first grouping fails, move onto another nest. If it passes, set a global variable (“flag”) that all other conditional groups test for…if they detect a “pass” the group of nested conditionals is skipped. An example:
--
--START DETECTIVE WORK
--
--is it a native Illustrator file? (including CCM-added ".eps" extensions)
if file_info is {} then
set file_info to amI_AIorPDF(path_to_check, file_extension)
end if
--is it an Illustrator EPS file?
if file_info is {} then
set file_info to amI_AIeps(path_to_check, file_extension)
end if
--is it a Bar Code Pro EPS file?
if file_info is {} then
set file_info to amI_BCPeps(path_to_check, file_extension)
end if
Each of the handlers called is a set of conditional tests. If the handler finds what it is looking for, file_info gets values. If it has values, every handler after that will skip, thus skipping whole nests of additional conditional tests. I had to use this method for Clorox File Repair when it quickly became clear I had more than 40 possible conditions to test against and the nesting was getting ugly. An example of one of the handlers:
--
-- FILE DETECTION SUB-HANDLERS
--
-- Am I a native Illustrator or an Acrobat PDF file?
--
on amI_AIorPDF(file_path, file_extension)
my logMe("BEGIN amI_AIorPDF handler", 2)
--Is it even possible I am an Illustrator of PDF at all?
if (file_extension is in g_extensions_illustrator_native) or (file_extension is in g_extensions_pdf) then
--I'm either Illustrator or PDF, which am I? (based on AIPDF search)
set header_grep to grepForString(file_path, g_headers_pdf_valid)
if item 1 of header_grep is greater than 0 then
--I'm a PDF file
my logMe("¢ PDF file found", 2)
--what version?
set header_grep to grepForString(file_path, g_headers_pdf_version)
if item 2 of header_grep is "%PDF-1.6.%" then
my fileProgressUpdate(file_path, "PDF file detected (7)", 0.0)
my logMe("¢ Version: 7", 2)
else if item 2 of header_grep is "%PDF-1.5.%" then
my fileProgressUpdate(file_path, "PDF file detected (6)", 0.0)
my logMe("¢ Version: 6", 2)
else if item 2 of header_grep is "%PDF-1.4.%" then
my fileProgressUpdate(file_path, "PDF file detected (5)", 0.0)
my logMe("¢ Version: 5", 2)
else if item 2 of header_grep is "%PDF-1.3.%" then
my fileProgressUpdate(file_path, "PDF file detected (4)", 0.0)
my logMe("¢ Version: 6", 2)
end if
--all PDF files use the same File Type, File Creator, and Extension
set file_type to "PDF " -- note the trailing space...File Type must always be exactly four characters long
set file_creator to "CARO"
set file_extension to ".pdf"
my logMe("¢ RETURN: " & file_type & " | " & file_creator & " | " & file_extension, 2)
my logMe("END amI_AIorPDF handler", 2)
return {file_type, file_creator, file_extension}
else
--Am I a native Illustrator file or an EPS file? (MediaBin sometimes adds ".eps" to end of native Illustrator files)
set header_grep to grepForString(file_path, g_headers_illustrator_eps)
if item 1 of header_grep is 0 then
--Am I native Illustrator file?
set header_grep to grepForString(file_path, g_headers_illustrator_native_version)
if item 1 of header_grep is greater than 0 then
my logMe("¢ Illustrator file found", 2)
--what version?
--these checks are out of numerical order because we didn't use Illustrator 9 or 10 much, so the versions we used more are checked first
if item 2 of header_grep is "Creator: Adobe Illustrator(R) 13" then
my fileProgressUpdate(file_path, "Illustrator file detected (CS3)", 0.0)
my logMe("¢ Version: 13 (CS3)", 2)
set file_type to "PDF " -- note the trailing space...File Type must always be exactly four characters long
else if item 2 of header_grep is "Creator: Adobe Illustrator(R) 12" then
my fileProgressUpdate(file_path, "Illustrator file detected (12)", 0.0)
my logMe("¢ Version: 12", 2)
set file_type to "PDF " -- note the trailing space...File Type must always be exactly four characters long
else if item 2 of header_grep is "Creator: Adobe Illustrator(R) 11" then
my fileProgressUpdate(file_path, "Illustrator file detected (CS1)", 0.0)
my logMe("¢ Version: 11 (CS1)", 2)
set file_type to "PDF " -- note the trailing space...File Type must always be exactly four characters long
else if item 2 of header_grep is "Creator: Adobe Illustrator(R) 8" then
my fileProgressUpdate(file_path, "Illustrator file detected (8)", 0.0)
my logMe("¢ Version: 8", 2)
set file_type to "TEXT"
else if item 2 of header_grep is "Creator:Adobe Illustrator(TM) 3.2" then
my fileProgressUpdate(file_path, "Illustrator file detected (3.2)", 0.0)
my logMe("¢ Version: 3.2", 2)
set file_type to "TEXT"
else if item 2 of header_grep is "Creator: Adobe Illustrator(R) 9" then
my fileProgressUpdate(file_path, "Illustrator file detected (9)", 0.0)
my logMe("¢ Version: 9", 2)
set file_type to "PDF " -- note the trailing space...File Type must always be exactly four characters long
else if item 2 of header_grep is "Creator: Adobe Illustrator(R) 10" then
my fileProgressUpdate(file_path, "Illustrator file detected (10)", 0.0)
my logMe("¢ Version: 10", 2)
set file_type to "PDF " -- note the trailing space...File Type must always be exactly four characters long
end if
--all Illustrator files have the same Creator and Extension
set file_creator to "ART5"
set file_extension to ".ai"
my logMe("¢ RETURN: " & file_type & " | " & file_creator & " | " & file_extension, 2)
my logMe("END amI_AIorPDF handler", 2)
return {file_type, file_creator, file_extension}
else
--not an Illustrator or PDF file (no extension clause)
my logMe("¢ Not a native Illustrator or PDF file", 2)
my logMe("END amI_AIorPDF handler", 2)
return {}
end if
else
--I'm probably an EPS file, not Illustrator
my logMe("¢ This is probably an EPS file.", 2)
my logMe("END amI_AIorPDF handler", 2)
return {}
end if
end if
else
my logMe("¢ No valid extensions.", 2)
my logMe("END amI_AIorPDF handler", 2)
return {}
end if
end amI_AIorPDF
And I have more than a dozen of handlers like these. Doing this all a huuuuuge string of nested conditions just made my head hurt, so I grouped certain tests together into handlers, then put the handlers in an order that would find what I needed fastest…most likely conditionals to pass at the top, most likely to fail at the bottom.