Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

458
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda