Well, there might be cases you need to execute a script twice, maybe because in the first run you were not able to fulfill all the tasks for whatever reason. We will not talk about the reasons, just about some basic solutions.
So lets further assume that the script needs a few user interactions to let the user select models and give some inputs. The second time you execute the same script you of course do not want to let the user again input all these data, you simply want to remember these "settings" and use them in the second run. But that is tricky as there is no persistent storage you can put them.
In this article I now propose a very simple solution: we use the clipboard and the SJ eval/uneval functions to store some script result into the clipboard and this way make it available to the same script on the second run. This is all very simplistic but should show how it may work.
// $EXPERIMENTAL$ $STRICT_MODE$ $WARNING_AS_ERROR$
load(".lib/ui.js");
var settings = {
alertUser : true
};
function restoreSettings() {
try {
var text = copyFromClipboard();
var restoredSettings = eval('' + text + ";"); // ; because we evaluate strict
if (restoredSettings) {
settings = restoredSettings; // play it safe
console.log("Settings restored: {0}", uneval(settings));
}
} catch (any) {
// ignore - clipboard may contain garbage
}
}
function storeSettings() {
try {
var text = uneval(settings);
copyToClipboard(text);
console.log("Settings stored: {0}", text);
} catch (any) {
// ignore - clipboard may contain garbage
}
}
function clearSettings() {
copyToClipboard("");
}
restoreSettings();
if (settings.alertUser) {
alert("Do this and that");
settings.alertUser = false;
storeSettings();
} else {
clearSettings();
}
As you can see a simple object is store/restored as JSON text from/to the clipboard. Just try to execute it multiple times after another. Try also to put some random text to the clipboard to see that it does not break.
Happy Scripting
Jan