Home > Blockchain >  How do I get an OOP Javascript Remember Button Selected Previously?
How do I get an OOP Javascript Remember Button Selected Previously?

Time:10-08

Please see the code below. I'd like to have it so the javascript remembers the selected button from the last time the scrip was run.

var doc = app.activeDocument;
var choice;
var w = new Window("dialog");
w.text = "Please Select Your Gemini Save Location Below";
var g = w.add("group");
var a = g.add("radiobutton", undefined, "MN");
var b = g.add("radiobutton", undefined, "WA");
var c = g.add("radiobutton", undefined, "CA");
var d = g.add("radiobutton", undefined, "TX");
var e = g.add("radiobutton", undefined, "Remote");
var button = w.add("button", undefined, "OK");
var radiobuttons = [a, b, c, d, e];
a.checkedState = true;
for (var i = 0; i < radiobuttons.length; i  ) {
    (function (i) {
        radiobuttons[i].onClick = function () {
            choice = radiobuttons[i].text;
        };
    })(I);
}

w.show();

CodePudding user response:

I'm used to save prefs in a json file and read them from the file at the start of a script.

For you case it would be something like this:

var doc = app.activeDocument;

var prefs = load_prefs()                         // try to loads saved prefs
|| {a: true, b:false, c:false, d:false, e:false} // or set default prefs

var choice;
var w = new Window("dialog");
w.text = "Please Select Your Gemini Save Location Below";
var g = w.add("group");
var a = g.add("radiobutton", undefined, "MN");
var b = g.add("radiobutton", undefined, "WA");
var c = g.add("radiobutton", undefined, "CA");
var d = g.add("radiobutton", undefined, "TX");
var e = g.add("radiobutton", undefined, "Remote");
var button = w.add("button", undefined, "OK");

// set the radiobutton from the prefs
a.value = prefs.a;
b.value = prefs.b;
c.value = prefs.c;
d.value = prefs.d;
e.value = prefs.e;

var radiobuttons = [a, b, c, d, e];
for (var i = 0; i < radiobuttons.length; i  ) {
    (function (i) {
        radiobuttons[i].onClick = function () {
            choice = radiobuttons[i].text;
        };
    })(i);
}

w.show();

alert(choice);

// set the prefs from radiobuttons
prefs.a = a.value;
prefs.b = b.value;
prefs.c = c.value;
prefs.d = d.value;
prefs.e = e.value;

save_prefs(prefs); // save the prefs


// functions to load and save prefs

function load_prefs() {
    var file = File(Folder.temp   '/prefs.json')
    return (file.exists) ? $.evalFile(file) : false;
}

function save_prefs(prefs) {
    var file = File(Folder.temp   '/prefs.json')
    file.open('w');
    file.encoding = 'UTF-8';
    file.write(prefs.toSource());
    file.close();
}

Just in case. Of course you can use a loop to set radiobuttons from prefs and vice versa if you like. Something like this:

// prefs
var prefs = {a:true, b:false, c:false, d:false, e:false};

// radiobuttons
var a = {}, b = {}, c = {}, d = {}, e = {};

// set radiobuttons from prefs
var radiobuttons = {a,b,c,d,e};
for (var btn in radiobuttons) radiobuttons[btn].value = prefs[btn];

console.log(radiobuttons)

// change values of some radiobuttons
a.value = false;
b.value = true;

// set prefs from radiobuttons
for (var pr in prefs) prefs[pr] = radiobuttons[pr].value;

console.log(prefs)

  • Related