Tinderbox's loop mechanism using .each() is helpful but one task can be problematic. This where the task is to loop a list of attribute title values and then act upon each attribute in turn.
The basic problem is that if the loop variable is, for example 'X' and current loop value is 'MyString', how to construct the reference $MyString, so as to acton the MyString attribute.
The answer is to use a nested action() call inside the loop, even if that seems counter-intuitive at first sight. For example, consider the task of resetting every attribute in a note's displayed attributes table. The contents of the table are stored in the Set-type system attribute $DisplayedAttributes, which can be iterated with .each():
$KeyAttributes.each(X){...};
Now the task is to set X, in each loop as a reference to the attribute named in the current value using action():
$KeyAttributes.each(X){
action("$"+X+"=;")
};
In other words, if X is value 'MyString' the action() is both concatenating some strings:
"$"+"MyString"+"=;"
…and then evaluating the resulting expression:
$MyString=;
Note that:
- eval() will not work in this context.
- the contents of the action() must concatenate to form a complete action code expression.
It may be necessary to nest quotes. Consider a list of String-type values. to set each one to a value of 'test':
$KeyAttributes.each(X){
action("$"+X+"='test';");
};
Note that the target value needs to be enclosed in quotes. As the main expression string already uses double quotes, the string needs to use single quotes. It doesn't matter if you reverse the nesting. The action would work just as well in the form:
action('$'+X+'="test";');
Just ensure the single/double set nest correctly.
The action can use loop variable more than once. Take the last scenario, but this time where all the attributes are Set or List type. Now the code is:
$KeyAttributes.each(X){
action("$"+X+"=$"+X+"+'test';");
};
…i.e. creating an action expression, in-loop, like this for X value 'MyList':
$MyList=$MyList+'test';
White space in the expression doesn't matter, as in normal action expressions, but is omitted above for brevity/clarity. The expression could also be:
$MyList = $MyList + 'test';
…and still work.