Forenindex » Programme » Print/Bildbearbeitung » Adobe InDesign Skriptwerkstatt » Radiobutton auslesen (CS6, UI, WIN7)

Radiobutton auslesen (CS6, UI, WIN7)

cwenet
Beiträge gesamt: 1139

5. Mär 2014, 12:39
Bewertung:

gelesen: 1569

Beitrag als Lesezeichen
Guten Tag,

dank Gerald Singelmann bin ich schon weitergekommen.
Nun möchte ich den aktuell gewählten Radiobutton ermitteln.
Komme aber leider nicht mehr weiter:

Code
getDialog = function() {  
var w = new Window( "dialog", "" );
w.alignChildren = "left";
var radio1 = w.add ("radiobutton", undefined, "(intern)");
var radio2 = w.add ("radiobutton", undefined, "(extern)");
radio1.value = true;

var choices = [ [ 1, 2, 3], [4, 5, 6] ];

w.listBox = w.add( "listbox" );

var g = w.add( "group" );
w.okButton = g.add( "button", undefined, "OK" );
w.okButton.onClick = function() {
//alert("OK Button geklickt");
this.window.close( 1 );
}
w.canButton = g.add( "button", undefined, "Abbrechen" );
w.canButton.onClick = function() {
//alert("Cancel Button geklickt");
this.window.close( 2 );
}


// Standardliste anlegen
w.listBox.removeAll();
for (var n = 0; n < choices[0].length; n++) {
w.listBox.add("item", choices[0][n] )
}
w.listBox.selected = true;


radio1.onClick = function () {
w.listBox.removeAll();
for (var n = 0; n < choices[0].length; n++) {
w.listBox.add("item", choices[0][n] )
}
var radiobutton = 1;
}

radio2.onClick = function () {
w.listBox.removeAll();
for (var n = 0; n < choices[1].length; n++) {
w.listBox.add("item", choices[1][n] )
}
var radiobutton = 2;
}

return w;
}


var w = getDialog();
w.center();
if ( w.show() == 1 ) {
var sel = w.listBox.selection; // sel ist ListItem

if ( sel != null ) {
alert("Sie haben " + sel.text + " ausgewählt");
alert("Sie haben " + radiobutton + " ausgewählt");
}
}


Ich habe in den OnClick Event eine radiobutton Variable hinterlegt, die aber bei if w.show garnicht mehr ankommt.

Hat jemand bitte eine Idee?

Grüße
Christoph

Radiobutton auslesen (CS6, UI, WIN7)

Peter Kahrel
Beiträge gesamt: 182

5. Mär 2014, 17:58
Bewertung:

gelesen: 1485

Beitrag als Lesezeichen
Hello Christoph,

In this line:
Code
alert("Sie haben " + radiobutton + " ausgewählt"); 

'radiobutton' is not defined because it's assigned to a (local) variable in the function getDialog. You could make it visible in the main program by doing something like this:
Code
w.radiabutton = 1 

so that in this line:
Code
alert("Sie haben " + [b]w.radiobutton + " ausgewählt"); 

w.radiobutton is not undefined.

But that doesn't get you very far. A good approach to radiobuttons, and to make your script independent of the number of radiobuttons, is to define a group for them. You can then address them as children of that group. Here's an example:

Code
w =  new Window ('dialog'); 
rbuttons = w.add ('group');
rbuttons.add ('radiobutton {text: "Button 1", value: 1}');
rbuttons.add ('radiobutton {text: "Button 2"}');
rbuttons.add ('radiobutton {text: "Button 3"}');
rbuttons.add ('radiobutton {text: "Button 4"}');
rbuttons.add ('radiobutton {text: "Button 5"}');

buttons = w.add ('group');
buttons.add ('button {text: "OK"}');
buttons.add ('button {text: "Cancel"}');

function getSelectedButton (rgroup) {
for (var i = rgroup.children.length-1; i >= 0; i--) {
if (rgroup.children[i].value) {
return i;
}
}
return null;
}

if (w.show() == 1) {
ix = getSelectedButton(rbuttons);
alert ('Sie haben ' + rbuttons.children[ix].text + " ausgewählt");
}


But this still doesn't give you event handlers. However, these can be defined in a for loop:

Code
w =  new Window ('dialog'); 
rbuttons = w.add ('group');
rbuttons.add ('radiobutton {text: "Button 1", value: 1}');
rbuttons.add ('radiobutton {text: "Button 2"}');
rbuttons.add ('radiobutton {text: "Button 3"}');
rbuttons.add ('radiobutton {text: "Button 4"}');
rbuttons.add ('radiobutton {text: "Button 5"}');

buttons = w.add ('group');
buttons.add ('button {text: "OK"}');
buttons.add ('button {text: "Cancel"}');

function getSelectedButton (rgroup) {
for (var i = rgroup.children.length-1; i >= 0; i--) {
if (rgroup.children[i].value) {
return i;
}
}
return null;
}

for (var i = rbuttons.children.length-1; i >= 0; i--) {
rbuttons.children[i].onClick = function () {
alert ('Sie haben ' + rbuttons.children[getSelectedButton (rbuttons)].text + " ausgewählt");
}
}

w.show();


With an approach like the above you can fix your script.

Peter

Radiobutton auslesen (CS6, UI, WIN7)

cwenet
Beiträge gesamt: 1139

5. Mär 2014, 20:16
Bewertung:

gelesen: 1451

Beitrag als Lesezeichen
Dear Peter,

thank you very much for your help. My code looks like this and it works fine:

Code
var choices =  
[ [
"Information 1",
"Information 2",
"Information 3"
],
[
"Information 4",
"Information 5",
"Information 6",
"Information 7"
] ];

var user_win = $.getenv("USERNAME");
//var user_win = $.getenv("COMPUTERNAME");
getDialog = function() {
var w = new Window( "dialog", "User "+user_win );
w.alignChildren = "left";

rbuttons = w.add ('group');
rbuttons.add ('radiobutton {text: "(intern)", value: 1}');
rbuttons.add ('radiobutton {text: "extern)"}');


w.listBox = w.add( "listbox" );
w.listBox.preferredSize = [ 300, 150 ];
var g = w.add( "group" );
w.okButton = g.add( "button", undefined, "OK" );
w.okButton.onClick = function() {
//alert("OK Button geklickt");
this.window.close( 1 );
}
w.canButton = g.add( "button", undefined, "Abbrechen" );
w.canButton.onClick = function() {
//alert("Cancel Button geklickt");
this.window.close( 2 );
}


// Standardliste anlegen
w.listBox.removeAll();
for (var n = 0; n < choices[0].length; n++) {
w.listBox.add("item", choices[0][n] )
}

rbuttons.children[0].onClick = function () {
w.listBox.removeAll();
for (var n = 0; n < choices[0].length; n++) {
w.listBox.add("item", choices[0][n] )
}
}

rbuttons.children[1].onClick = function () {
w.listBox.removeAll();
for (var n = 0; n < choices[1].length; n++) {
w.listBox.add("item", choices[1][n] )
}
}

return w;
}

function getSelectedButton (rgroup) {
for (var i = rgroup.children.length-1; i >= 0; i--) {
if (rgroup.children[i].value) {
return i;
}
}
return null;
}

var w = getDialog();
w.center();
if ( w.show() == 1 ) {
var sel = w.listBox.selection; // sel ist ListItem

if ( sel != null ) {
//alert("Sie haben " + sel.text + " ausgewählt");
//alert ('Sie haben ' + rbuttons.children[getSelectedButton (rbuttons)].text + " ausgewählt");

var myPage = app.activeWindow.activePage;
var myTextframe = myPage.textFrames.add();

myTextframe.geometricBounds = [0,0,20,40];

if (rbuttons.children[getSelectedButton (rbuttons)].text == "(intern)") {
myTextframe.contents = "User : "+user_win+"\r"+ sel.text;
}else{
myTextframe.contents = "User:"+"\r"+ sel.text;
}


//Objektformat zuweisen
var theOStyles = app.activeDocument.objectStyles.everyItem().name;
var myOStyleName = "User "+rbuttons.children[getSelectedButton (rbuttons)].text;
var myOStyle = app.activeDocument.objectStyles.item(myOStyleName);
myTextframe.applyObjectStyle(myOStyle, true);

}
}


Best regards
Christoph

(Dieser Beitrag wurde von cwenet am 5. Mär 2014, 20:18 geändert)