Copy link to clipboard
Copied
Hello to all
I want to make a form that would be a granular user to choose from a specific list of values ​​to be passed as a parameter. Here's a script, it creates what I need, but the names of the radio buttons are not unique.Can you please tell how to do so at the close of this form (window) I could get the value of the selected item from the list of radiobutton?
#target photoshop
app.bringToFront();
var par = Array("Select your choise","test 1","test 2", "test 3");
var rez = ask(par);
alert(rez)
function ask(par){
wask = new Window('dialog', '');
wask.orientation = "column";
wask.alignment="top";
wask.spacing=0;
grs0 =wask.add('group');
grs0.spacing=0;
grs0.add('statictext', undefined, par[0]);
grs0.alignment="top";
grs0.preferredSize.height = 40;
grs1 =wask.add('group');
grs1.spacing=0;
grs1.alignment="left";
grs1.preferredSize.height = 20*par.length;
grs1.orientation = "column";
for(var i=1;i<par.length;i++){
var g = grs0 +i;
r= "rb"+i;
r = grs1.add("radiobutton", undefined, par);
r.alignment="left";
}
bt1 = wask.add ("button", undefined, "Select");
bt2 = wask.add ("button", undefined, "Close");
bt1.onClick = function(){
}
wask.show();
}
I think there are at least two ways to do something like this. One way would be to give the controls names. You can then find them by name.
...var par = Array("Select your choise","test 1","test 2", "test 3");
var rez = ask(par);
if( rez == 1) alert(wask.findElement('rb1').value);
function ask(par){
wask = new Window('dialog', '');
wask.orientation = "column";
wask.alignment="top";
wask.spacing=0;
grs0 =wask.add('group');
grs0.spacing=0;
grs0.add('statictext', undefined, par[0]);
Copy link to clipboard
Copied
I think there are at least two ways to do something like this. One way would be to give the controls names. You can then find them by name.
var par = Array("Select your choise","test 1","test 2", "test 3");
var rez = ask(par);
if( rez == 1) alert(wask.findElement('rb1').value);
function ask(par){
wask = new Window('dialog', '');
wask.orientation = "column";
wask.alignment="top";
wask.spacing=0;
grs0 =wask.add('group');
grs0.spacing=0;
grs0.add('statictext', undefined, par[0]);
grs0.alignment="top";
grs0.preferredSize.height = 40;
grs1 =wask.add('group');
grs1.spacing=0;
grs1.alignment="left";
grs1.preferredSize.height = 20*par.length;
grs1.orientation = "column";
for(var i=1;i<par.length;i++){
r = grs1.add("radiobutton", undefined, par,{name:'rb'+i});
r.alignment="left";
}
bt1 = wask.add ("button", undefined, "Select",{name:'ok'});
bt2 = wask.add ("button", undefined, "Close",{name:'cancel'});
return wask.show();
}
Another way would be to use an eval statement in the loop that creates the controls. Something like
var par = Array("Select your choise","test 1","test 2", "test 3");
var rez = ask(par);
if( rez == 1) alert(wask.rb1.value);
function ask(par){
wask = new Window('dialog', '');
wask.orientation = "column";
wask.alignment="top";
wask.spacing=0;
grs0 =wask.add('group');
grs0.spacing=0;
grs0.add('statictext', undefined, par[0]);
grs0.alignment="top";
grs0.preferredSize.height = 40;
grs1 =wask.add('group');
grs1.spacing=0;
grs1.alignment="left";
grs1.preferredSize.height = 20*par.length;
grs1.orientation = "column";
for(var i=1;i<par.length;i++){
eval('wask.rb'+i+' = grs1.add("radiobutton", undefined, par['+i+']);');
eval('wask.rb'+i+'.alignment="left";');
}
bt1 = wask.add ("button", undefined, "Select",{name:'ok'});
bt2 = wask.add ("button", undefined, "Close",{name:'cancel'});
return wask.show();
}
Copy link to clipboard
Copied
small codding 🙂
var mess="Result:\n";
if( rez == 1) {
for(var i=1;i<par.length;i++){
element = "rb"+i;
// alert(i + "= " + wask.findElement(element).value);
mess += i + "= " + wask.findElement(element).value+"\n";
}
alert(mess);
}
but for example 2 I don't can show results 😞
Copy link to clipboard
Copied
findElement works with the controls name. If you want to use that method with the second example you need to add names to the contols.
Copy link to clipboard
Copied
How to optimization this code (reduce the number of rows and remove duplicate pieces of code)?
I wont to dynamically create elements, then marked to selection element by click and give value selected element (text="X"). Unselected element are "redraw" to text="";
If change value "e <7" (row 108) - script is correctly worked at 6 boxes. Boxes 7-9 are dynamically created (65-71), but I don't can change values (111,113)
#target photoshop
main()
function main(){
var w = new Window('dialog','Example', undefined, {borderless: false, closeButton: false}); // {borderless: true, closeButton: false}
w.orientation="column";
w.spacing=0;
p0 = w.add('group');
p0.spacing=0;
p0.orientation="column";
wgrBorder0= p0.add('group');
wgrBorder0.orientation = 'row';
wgrBorder0.alignment='left';
wgrBorder0.spacing=0;
var p = p0.add('group');
p.spacing=0;
p.orientation="column";
wgrBorder1= p.add('group');
wgrBorder1.orientation = 'row';
wgrBorder1.alignment='left';
wgrBorder1.spacing=0;
wgrBorder2= p.add('group');
wgrBorder2.orientation = 'row';
wgrBorder2.alignment='left';
wgrBorder2.spacing=0;
wgrBorder3= p.add('group');
wgrBorder3.orientation = 'row';
wgrBorder3.alignment='left';
wgrBorder3.spacing=0;
myEditText1 = wgrBorder1.add('edittext', undefined, {borderless: false});
myEditText1.graphics.font = ScriptUI.newFont ("Arial", 'BOLD', 18)
myEditText1.text = "";
myEditText1.preferredSize = {width:20, height:20};
myEditText1.enabled = false;
myEditText2 = wgrBorder1.add('edittext', undefined, {borderless: false});
myEditText2.graphics.font = ScriptUI.newFont ("Arial", 'BOLD', 18)
myEditText2.text = "";
myEditText2.preferredSize = {width:20, height:20};
myEditText2.enabled = false;
myEditText3 = wgrBorder1.add('edittext', undefined, {borderless: false});
myEditText3.graphics.font = ScriptUI.newFont ("Arial", 'BOLD', 18)
myEditText3.text = "";
myEditText3.preferredSize = {width:20, height:20};
myEditText3.enabled = false;
myEditText4 = wgrBorder2.add('edittext', undefined, {borderless: false});
myEditText4.graphics.font = ScriptUI.newFont ("Arial", 'BOLD', 18)
myEditText4.text = "";
myEditText4.preferredSize = {width:20, height:20};
myEditText4.enabled = false;
myEditText5 = wgrBorder2.add('edittext', undefined, {borderless: false});
myEditText5.graphics.font = ScriptUI.newFont ("Arial", 'BOLD', 18)
myEditText5.text = "";
myEditText5.preferredSize = {width:20, height:20};
myEditText5.enabled = false;
myEditText6 = wgrBorder2.add('edittext', undefined, {borderless: false});
myEditText6.graphics.font = ScriptUI.newFont ("Arial", 'BOLD', 18)
myEditText6.text = "";
myEditText6.preferredSize = {width:20, height:20};
myEditText6.enabled = false;
// me experence to optimisation code on element 7 - 9. Elements are created, but it not is fined in function reDraw()
for (cell=7;cell<10;cell++){
r = wgrBorder3.add('edittext', undefined, {name:'myEditText'+cell});
r.graphics.font = ScriptUI.newFont ("Arial", 'BOLD', 18)
r.text = "";
r.preferredSize = {width:20, height:20};
r.enabled = false;
}
//
myEditText1.onClick = function(){
reDraw(1)
}
myEditText2.onClick = function(){
reDraw(2)
}
myEditText3.onClick = function(){
reDraw(3)
}
myEditText4.onClick = function(){
reDraw(4)
}
myEditText5.onClick = function(){
reDraw(5)
}
myEditText6.onClick = function(){
reDraw(6)
}
btnOK = w.add('button',undefined,'OK');
btnCanc = w.add('button',undefined,'Cancel');
btnOK.onClick= function(){//********************************************
w.close();
return ;// selected element !!!!!!!!!!!
}
//******************************************************************************
btnCanc.onClick= function(){
w.close()
}
w.show();
function reDraw(idElement){//------------------------------------------------------
for(var e=1;e<10;e++){
var element =eval("myEditText" + e);
if(idElement == e)
element.text ="X";
else{
element.text ="";
}
for(var e=1;e<10;e++){
var element =eval("myEditText" + e);
if(idElement == e)
element.text ="X";
else{
element.text ="";
}
}
}
}