I stumbled upon an efficient construct for setting multiple variables to a single value. It is likely old ground for experienced scripters, but since I couldn’t find it by searching, I thought it might be a useful tidbit to share.
Let’s say you have ten disparate variables x1, x2, …, x10 that you would like to zero. The most obvious way is to set each variable to 0 explicitly:
set {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10} to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
-- then when it is time to zero them:
set {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10} to {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
The problem with this approach is that if you add or subtract variables to the zeroing assignment statement, you must be sure that the number of zeros matches the number of variables (simple, but I’ve goofed up with that too many times in the real world.)
Packaging the variables into a list and processing the list in a repeat loop in the standard fashion doesn’t do the trick:
set {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10} to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
set theList to {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10}
repeat with currItem in theList
set currItem's contents to 0
end repeat
{x1, x2, x3, x4, x5, x6, x7, x8, x9, x10} --> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Instead, package references to the variables in a list, and process those references in a repeat loop:
set {x1, x2, x3, x4, x5, x6, x7, x8, x9, x10} to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
set theList to {a reference to x1, a reference to x2, a reference to x3, a reference to x4, a reference to x5, a reference to x6, a reference to x7, a reference to x8, a reference to x9, a reference to x10}
repeat with currItem in theList
set currItem's contents's contents to 0
end repeat
{x1, x2, x3, x4, x5, x6, x7, x8, x9, x10} --> {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}