如何获得 OOP Javascript 记住之前选择的按钮?
How do I get an OOP Javascript Remember Button Selected Previously?
请看下面的代码。我想要它,这样 javascript 就会记住上次 运行.
时选择的按钮
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();
我习惯于将首选项保存在 json 文件中,并在脚本开始时从文件中读取它们。
你的情况应该是这样的:
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();
}
以防万一。当然,如果您愿意,您可以使用循环从首选项设置单选按钮,反之亦然。像这样:
// 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)
请看下面的代码。我想要它,这样 javascript 就会记住上次 运行.
时选择的按钮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();
我习惯于将首选项保存在 json 文件中,并在脚本开始时从文件中读取它们。
你的情况应该是这样的:
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();
}
以防万一。当然,如果您愿意,您可以使用循环从首选项设置单选按钮,反之亦然。像这样:
// 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)