DekGenius.com
[ Team LiB ] Previous Section Next Section

6.3 Comments

AppleScript permits two kinds of comment: single-line comments and delimited comments. Everything including and after two successive hyphens on a line is a single-line comment. For example:

set a to 1 -- this a comment on the same line as a command
-- this a comment on a line by itself

The comment delimiters are (* and *). Everything between comment delimiters is a comment. Such a comment may span multiple lines. This code contains three stretches of text that are legally commented out with comment delimiters:

set a to 1 (* because we feel like it;
tomorrow we may not feel like setting a to 1 *)
(* in fact things could be very different tomorrow,
but I really can't speak to that issue just now *)
set b to 2 (* this seems a good idea too *)

A comment delimited with comment delimiters may not interrupt a command, nor precede a command on the same line. Neither of these lines will compile:

set a to (* because we feel like it *) 1
(* here's a good idea *) set a to 1

Comment delimiters attempt to be "intelligent." Comments may be nested, in which case the delimiters must match in pairs. The value of this is that you can easily comment out a stretch of script that already contains some comments:

(* outer comment
(* inner comment *)
rest of outer comment *)

A rather weird side effect of this "intelligence" is that quotation marks and vertical bars inside comment delimiters must also match in pairs:

(* "this works fine" and so does |this| *)

If you remove one quotation mark or one vertical bar from inside that comment, the code won't compile.

Single-line comments attempt no such "intelligence"; they simply cause the rest of the line to be ignored, and they take precedence over everything. Thus if you insert comment delimiters to comment out a block of code, you must be careful not to place either delimiter within a single-line comment. This won't compile:

(* set a to 1 -- and why not? *)

That's because the closing comment delimiter is itself commented out as part of the single-line comment, so the opening comment delimiter is unbalanced.

    [ Team LiB ] Previous Section Next Section