• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do I read ScriptUI checkbox values?

Guest
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

From the follow code, how do I read the Boolean of whether a box was checked or not? For each box that's checked, I would like to create a rectangle on the active doc filled with that color. I just can't figure out how to access the user's input. Or am I tackling this problem from the wrong direction?

#target Illustrator

myDlg = new Window('dialog', 'Add Pantone Swatches');
myDlg.orientation = 'column';
myDlg.alignment = 'right';

with(myDlg.add('group'))
{
    orientation = 'row';
    with(add('panel'))//First structure of Window is materialized
    {
        orientation = 'column';
        add('statictext', undefined, "Please select which swatches to use:");
        with(add('group'))
        {
            orientation = 'column';
            for(i=app.activeDocument.swatches.length-1; i !=0; i--){
                add('checkbox', undefined, String(app.activeDocument.swatches.name));
                }           
        }
    }
}
with(myDlg.add('group'))
{
    orientation = 'row';
    add('button', undefined, "OK", {name: 'ok'});//Ok and Cancel Buttons
    add('button', undefined, "Cancel",{name: 'cancel'});
}

if(myDlg.show())
{
    if(app.documents.length == 0)
        app.documents.add(DocumentColorSpace.CMYK);
}

TOPICS
Scripting

Views

2.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Enthusiast ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

"checkbox" is not good choice for your purpose, you'd better use "listBox", the listBox selection items's index are what you need.

And you use too much "with" statement!

#target Illustrator app.documents.length || app.documents.add(DocumentColorSpace.CMYK); var doc =  app.activeDocument, sw = doc.swatches,      myDlg = new Window('dialog', 'Add Pantone Swatches'); myDlg.orientation = 'column'; myDlg.alignment = 'right'; with(myDlg.add('panel'))//First structure of Window is materialized {      orientation = 'column';      add('statictext', undefined, "Please select which swatches to use:");      var swatchBox = add('listBox', undefined, sw, {multiselect: true}); } with(myDlg.add('group')) {     orientation = 'row';     add('button', undefined, "OK", /*{name: 'ok'}*/).onClick = drawRect;//Ok and Cancel Buttons     add('button', undefined, "Cancel",{name: 'cancel'}); } function drawRect (){      var items = swatchBox.selection, len = items.length, i = 0;      for (; i < len; i ++){           doc.pathItems.rectangle(0, i*100, 100, 100).fillColor = sw[items.index].color;      }      myDlg.close(0); } myDlg.show();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

I agree, too much use of "with"

moluapple, listbox is empty, needs to be populated, right?.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 03, 2011 Sep 03, 2011

Copy link to clipboard

Copied

moluapple, listbox is empty, needs to be populated, right?.

But sw is an Array, and have converted to listItems for listBox, isn't it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

here's a sample of a way to check wich check boxes are checked

#target Illustrator

myDlg = new Window('dialog', 'Add Pantone Swatches');

myDlg.orientation = 'column';

myDlg.alignment = 'right';

var checkboxes = []; // added array to hold all check boxes

with(myDlg.add('group'))

{

    orientation = 'row';

    with(add('panel'))//First structure of Window is materialized

    {

        orientation = 'column';

        add('statictext', undefined, "Please select which swatches to use:");

        with(add('group'))

        {

            orientation = 'column';

               

            for(i=app.activeDocument.swatches.length-1; i >=0; i--){ // changed != with >=

                checkboxes = add('checkbox', undefined, String(app.activeDocument.swatches.name)); // assign all check boxes to a variable array

                }           

        }

    }

}

with(myDlg.add('group'))

{

    orientation = 'row';

    btnOk = add('button', undefined, "OK", {name: 'ok'});// assigned Ok button to a variable

    add('button', undefined, "Cancel",{name: 'cancel'});

}

// added a function to get the values of the checked check boxes

btnOk.onClick = function()

     {

          var msg = 'Checked Swatches are:\r';

          for (j=0; j<checkboxes.length-1;j++)

               {

                    if (checkboxes.value == true)

                         msg = msg + checkboxes.text + '\r';

                    

               }

          alert(msg);

     }

if(myDlg.show())

{

    if(app.documents.length == 0)

        app.documents.add(DocumentColorSpace.CMYK);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 27, 2015 May 27, 2015

Copy link to clipboard

Copied

Carlos, I've adapted this (post 3) to add some user options to a script. Everything works great except if the Cancel button is clicked the script still executes. How can I make the Cancel button stop everything? Thanks for your time.

Edit: Cancel that, the trick seems to be putting the commands inside the function that only executes if OK is clicked.

Edit 2: myDlg.close(); is super helpful at the end of the onClick function(). As usual, thanks a ton for all the content here. It's a lifesaver.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2015 May 27, 2015

Copy link to clipboard

Copied

LATEST

thanks for answering your own questions

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines