chcking if multiple conditions are true

couldn’t find anything in the OS X forums, nor in the Finder dictionary.

How would I have a routine run only if multiple conditions are true

(I’m trying to give myself a reminder to save every 5 minutes)

first I have to check if the app is running
then, I want to check if a menu item is inactive

thought I would nest one if statement inside the other, like so:


	tell application "System Events"
		if exists application process "QuarkXPress" then
			if WarnMe then
				if menu item 7 of menu "File" of application "QuarkXPress" is enabled then
					tell application "QuarkXPress"
						beep
					end tell
				end if
			end if
			set Idle_interval to 5 * minutes
			set WarnMe to true
		else
blah, blah, rest of the script

but that got me an error, something about the way I worded the GUI part

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.

Hi,

if you want to access any menu item with GUI scripting, the target process must be frontmost.
And the syntax to check a menu item is different


tell application "System Events"
	if exists application process "QuarkXPress" and WarnMe then
		set frontmost of process "QuarkXPress" to true
		if menu item 7 of menu "File" of menu bar 1 of process "QuarkXPress" is enabled then beep
	end if
	set Idle_interval to 5 * minutes
	set WarnMe to true
.

set WarnMe to true
activate application "QuarkXPress"
tell application "System Events"
	if (name of processes whose visible is true) contains "QuarkXPress" then
		tell process "QuarkXPress" to if WarnMe then if (get value of attribute "AXEnabled" of menu item "Revert to Saved" of menu 1 of menu bar item "File" of menu bar 1) then beep
	end if
end tell


if you activate the application, the process exists anyway :wink:

Let’s jam a bunch into one line

activate application "QuarkXPress"
tell application "System Events" to if (exists application process "QuarkXPress") and WarnMe and (get value of attribute "AXEnabled" of menu item "Revert to Saved" of menu 1 of menu bar item "File" of menu bar 1 of process "QuarkXPress") then beep
set Idle_interval to 5 * minutes
set WarnMe to true

hmpfh, oh yeah - really didn’t think about that one.

Okay change my last posting to this then

tell application "System Events" to if (exists application process "QuarkXPress") and WarnMe and (get value of attribute "AXEnabled" of menu item "Revert to Saved" of menu 1 of menu bar item "File" of menu bar 1 of process "QuarkXPress") then beep
set {Idle_interval, WarnMe} to {5 * minutes, true}

Rather than using the GUI menu items just ask the open doc if its modified property is true if so save else nothing has changed and ignore.

Hmm, good idea. Not every using Quark I had no idea you could peek at that.

tell application "System Events" to if (exists application process "QuarkXPress") then tell application "QuarkXPress" to if modified of (get properties of front project) and WarnMe then beep
set {Idle_interval, WarnMe} to {5 * minutes, true}

damn…

I shoulda thought of that.

tell document 1 of application “QuarkXPress”
get properties
end tell

Sorry, and thanks.