Syntax Differences: One Line v. Two

Could somebody explain why the effects of this bit of code:

tell application “Microsoft Excel” to activate

differs from this:

tell application “Microsoft Excel”
activate
end tell

The first compiles as is; the second won’t compile without the end statement. More important, there seems to be an effect on the compilation of later code depending on which version is used.

Hello

In the first sample, there is an “implicit” end tell at the end of line.
So, every trailing code is NOT speaking to the named application.

Yvan KOENIG (from FRANCE dimanche 1 octobre 2006 19:20:34)

This is standard AppleScript, and something that applies to more than just the ‘telll’ statement.

In short, the one-line format can be used any time there’s one (and only one) statement to execute. If you want more than one statement you start them on the next line and use an ‘end’ clause to tell AppleScript where the condition changes.

For example, the samething applies to:

if a=1 then set b to 2
set c to 3

vs.

if a=1 then
set b to 2
set c to 3
end if

In the first case, only the one statement on the ‘if’ line (‘set b to 2’) will execute if a = 1. The second statement (‘set c to 3’) will always execute.
In the second case, b and c will both be set only if a=1. If a equals any other value neither b nor c will change.

The same thing happens with a ‘tell application’ block. With the multi-line format all statements up to the ‘end tell’ are passed to the application for execution. In the one-line format, only the one command is sent to the app.

Side note on applications; You can also use lines like this:

activate application "Microsoft Excel"
-- or
launch application "Microsoft Excel"

See also:
Control Statements
Commands

Thanks for the explanations.