Tell and If in the same statement

I have been using an AppleScript like this


	repeat with ImageCounter from 1 to everyImageCount
		set imagepath to item ImageCounter of everyImagePathList
		tell my application "System Events" to if not (exists file imagepath) then
			tell application "Capture One 11" to tell COPDocRef to tell thisCollection to set imageName to name of image ImageCounter
			loq_Results2(0, false, ("Image " & imageName & " not found at " & imagepath))
			set countImageNotFound to countImageNotFound + 1
		end if
	end repeat

I have just discovered that the line “tell my application “System Events” to if not (exists file imagepath) then”
causes an “unable to continue with loq_Results2” error when this line executes:
"loq_Results2(0, false, (“Image " & imageName & " not found at " & imagepath))”

Prefacing the second line with “my” clears the problem, as does separating the “tell” clause from the “If” clause

I don’t understand why there is an interaction. I thought the scope of the tell statement in this form was the one line only.

This works


	repeat with ImageCounter from 1 to everyImageCount
		set imagepath to item ImageCounter of everyImagePathList
		tell my application "System Events" to if not (exists file imagepath) then
			tell application "Capture One 11" to tell COPDocRef to tell thisCollection to set imageName to name of image ImageCounter
			my loq_Results2(0, false, ("Image " & imageName & " not found at " & imagepath))
			set countImageNotFound to countImageNotFound + 1
		end if
	end repeat

as does this


	repeat with ImageCounter from 1 to everyImageCount
		set imagepath to item ImageCounter of everyImagePathList
		tell my application "System Events" to set theFlag to not (exists file imagepath)
		if not theFlag then
			tell application "Capture One 11" to tell COPDocRef to tell thisCollection to set imageName to name of image ImageCounter
			loq_Results2(0, false, ("Image " & imageName & " not found at " & imagepath))
			set countImageNotFound to countImageNotFound + 1
		end if
	end repeat

Can anyone point me to a good reference on the topic of using a tell statement and an if statement in the same line.

Hi Eric.

It’s not, I’m afraid. If you start a multi-line (“compound”) statement within a single-line (“simple”) ‘tell’ or ‘if’ statement, the statements are effectively nested and the scope of the simple statement continues to the end of the inner, compound one. It’s possibly misleading that the block ends with the ‘end’ statement for the innermost statement; but this is correct because the outer, simple statement(s) don’t have separate ‘end’ statements!

My own view is that simple statements shouldn’t contain compound ones unless there’s a very good reason. It’s better to make the outer statement compound too to avoid scope problems and to make it clear in the code exactly where each scope ends.

The AppleScript Language Guide doesn’t explicitly mention compound statements within simple ones, but it’s pretty clear apart from that. See the sections on if Statements and tell Statements.