Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

457
Views
How do I get an OOP Javascript Remember Button Selected Previously?

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();

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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;

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

var choice = prefs.choice; // <-- get choice from prefs;
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;
prefs.choice = choice; // <-- save choice

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!