Session 12
Flexible Input Forms
Variables in Pause Windows
In the list of advantages presented in the previous section, I stated that variables in pause window definitions behave like everywhere else. What does that mean? Have a look at the optionMenu
line in the script we developed in the previous section:
optionMenu: "Target Directory", 1
The second argument (1) specifies that the first option is default, i.e. pre-selected. That's perfectly fine if you assume, that good is the prefered judgement (or if you assume that judgements are normally distributed).
But if you assume that the current sound is probably evaluated the same as the previous sound (or if you assume that one judgement is prefered but you don't know which), it would be convenient for the user to have the previously chosen option pre-selected, instead of always the first option. This is realized by specifying the default option with a variable instead of a literal number. And since we already have a variable which contains the index of the last chosen option, namely target_Directory, we should make use of it:
list = Create Strings as file list: "SoundFiles", "sounds/*.wav"
n = Get number of strings
for i to n
selectObject: list
filename$ = Get string: i
fileID = Read from file: "sounds/" + filename$
Play
beginPause: "Set the target directory"
comment: "Choose the appropriate directory for this sound."
# pre-select previous option
optionMenu: "Target Directory", target_Directory
option: "good"
option: "decent"
option: "poor"
endPause: "Continue", 1
selectObject: fileID
Save as WAV file: target_Directory$ + "/" + filename$
removeObject: fileID
endfor
removeObject: list
Suppose the user picked the second option decent for the first sound. In that case, target_Directory$ contains the string decent and target_Directory contains the number 2 because it's the second option. The sound is saved and the loop enters the second pass. Now, when the pause window pops up, the pre-selected option is decent, because target_Directory still contains 2. The user can just click Continue if the sound is evaluated as decent again or choose another option, say poor. In the latter case, poor will be pre-selected in the next pass—and so on.
However, the script will never make it to the second pass because target_Directory is undefined when optionMenu
is built the very first time. The script is aborted during the first pass and exits with an error message. Let's fix this and fill target_Directory with a start value, e.g. 1, to set the default option to good at the beginning:
list = Create Strings as file list: "SoundFiles", "sounds/*.wav"
n = Get number of strings
# initialize target_Directory
target_Directory = 1
for i to n
selectObject: list
filename$ = Get string: i
fileID = Read from file: "sounds/" + filename$
Play
beginPause: "Set the target directory"
comment: "Choose the appropriate directory for this sound."
optionMenu: "Target Directory", target_Directory
option: "good"
option: "decent"
option: "poor"
endPause: "Continue", 1
selectObject: fileID
Save as WAV file: target_Directory$ + "/" + filename$
removeObject: fileID
endfor
removeObject: list
This example shows that variables in pause window definitions (in contrast to input forms) are replaced by their value at runtime—like everywhere else. The last important advantage of pause windows compared to input forms are flexible button definitions.
Next: Buttons in Pause Windows