Highlighted

Adding Custom Dialog to Define Variable Dynamically

Engaged ,
Apr 07, 2020

Copy link to clipboard

Copied

Hi Professionals, 

I'm Beginner in Javascript for Indesign,, I wrote the following code to just select row in table based on word and then apply a Cell Style(YourCellStyle) in every row found started with the founded word.

 

var myDoc = app.activeDocument  
app.findTextPreferences = null;
//app.findTextPreferences = app.changeTextPreferences = null; 
var myInput = prompt ("Enter the Word", "", "Search For");
app.findTextPreferences.findWhat = myInput
var myFound = myDoc.findText();  
for(i=0; i<myFound.length; i++)  
{  
   if(myFound[i].parent.constructor.name == "Cell")  
    {  
       var parentRowCells = myFound[i].parent.parentRow.cells;
       for(var j=0;j<parentRowCells.length;j++)
       {
         parentRowCells[j].appliedCellStyle = "YourCellStyle";
                   }
               var overrides = myFound[i].clearOverrides();
          }  
    }  

 

Now I need to Modify the Script so Instead of forcing the user to the Cell style named (YourCellStyle) , it will show him dialog to select what ever he want from cell styles avilable in user document, so the script will be more easy, but i don't know how to do that, so please help!.

 

Best regards

Adobe Community Professional
Correct answer by Sunil Yadav | Adobe Community Professional

Hi,

Try this Scripting UI code & update according to you:

if(app.documents.length > 0){
    var allCellStyles = [];
    getCellStyle("", app.documents[0]);
    function getCellStyle(name, object){
        for(var c = 0; c < object.cellStyles.length; c++){
            if(name != ""){
                allCellStyles.push(["["+name+"]//"+object.cellStyles[c].name, object.cellStyles[c].id]);
                }
            else{
                allCellStyles.push([object.cellStyles[c].name, object.cellStyles[c].id]);
                }
            }
        }
    // DIALOG
    // ======
    var dialog = new Window("dialog"); 
        dialog.text = "Find text"; 
        dialog.orientation = "column"; 
        dialog.alignChildren = ["left","top"]; 
        dialog.spacing = 10; 
        dialog.margins = 16; 

    // GROUP1
    // ======
    var group1 = dialog.add("group", undefined, {name: "group1"}); 
        group1.preferredSize.width = 401; 
        group1.orientation = "row"; 
        group1.alignChildren = ["left","center"]; 
        group1.spacing = 10; 
        group1.margins = 0; 

    var statictext1 = group1.add("statictext", undefined, undefined, {name: "statictext1"}); 
        statictext1.text = "Find what : "; 

    var edittext1 = group1.add('edittext {properties: {name: "edittext1"}}'); 
        edittext1.preferredSize.width = 150; 

    // DIALOG
    // ======
    var checkbox1 = dialog.add("checkbox", undefined, undefined, {name: "checkbox1"}); 
        checkbox1.text = "With Cell Style"; 
        checkbox1.value = true; 

    // GROUP2
    // ======
    var group2 = dialog.add("group", undefined, {name: "group2"}); 
        group2.orientation = "row"; 
        group2.alignChildren = ["left","center"]; 
        group2.spacing = 10; 
        group2.margins = 0; 

    var statictext2 = group2.add("statictext", undefined, undefined, {name: "statictext2"}); 
        statictext2.text = "Select Cell style : "; 

    var dropdown1_array = ["-Select-"]; 
    var dropdown1 = group2.add("dropdownlist", undefined, undefined, {name: "dropdown1", items: dropdown1_array}); 
    
        for(var c = 0; c < allCellStyles.length; c++){
            dropdown1.add("item", allCellStyles[c][0]);
            }
        dropdown1.selection = 0; 
        dropdown1.preferredSize.width = 120; 

    // DIALOG
    // ======
    var divider1 = dialog.add("panel", undefined, undefined, {name: "divider1"}); 
        divider1.alignment = "fill"; 

    // GROUP3
    // ======
    var group3 = dialog.add("group", undefined, {name: "group3"}); 
        group3.preferredSize.width = 400; 
        group3.orientation = "row"; 
        group3.alignChildren = ["right","center"]; 
        group3.spacing = 10; 
        group3.margins = 0; 

    var button1 = group3.add("button", undefined, undefined, {name: "OK"}); 
        button1.text = "Clear overrides"; 

    var button2 = group3.add("button", undefined, undefined, {name: "button2"}); 
        button2.text = "Cancel"; 

    if(dialog.show() == 1){
        // Do your work here===
        }
    }
else{
    alert ("Please open a document then proceed!!!", "No document Opened", true);
    }

Best

Sunil

TOPICS
How to, Scripting

Views

778

Likes

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

Adding Custom Dialog to Define Variable Dynamically

Engaged ,
Apr 07, 2020

Copy link to clipboard

Copied

Hi Professionals, 

I'm Beginner in Javascript for Indesign,, I wrote the following code to just select row in table based on word and then apply a Cell Style(YourCellStyle) in every row found started with the founded word.

 

var myDoc = app.activeDocument  
app.findTextPreferences = null;
//app.findTextPreferences = app.changeTextPreferences = null; 
var myInput = prompt ("Enter the Word", "", "Search For");
app.findTextPreferences.findWhat = myInput
var myFound = myDoc.findText();  
for(i=0; i<myFound.length; i++)  
{  
   if(myFound[i].parent.constructor.name == "Cell")  
    {  
       var parentRowCells = myFound[i].parent.parentRow.cells;
       for(var j=0;j<parentRowCells.length;j++)
       {
         parentRowCells[j].appliedCellStyle = "YourCellStyle";
                   }
               var overrides = myFound[i].clearOverrides();
          }  
    }  

 

Now I need to Modify the Script so Instead of forcing the user to the Cell style named (YourCellStyle) , it will show him dialog to select what ever he want from cell styles avilable in user document, so the script will be more easy, but i don't know how to do that, so please help!.

 

Best regards

Adobe Community Professional
Correct answer by Sunil Yadav | Adobe Community Professional

Hi,

Try this Scripting UI code & update according to you:

if(app.documents.length > 0){
    var allCellStyles = [];
    getCellStyle("", app.documents[0]);
    function getCellStyle(name, object){
        for(var c = 0; c < object.cellStyles.length; c++){
            if(name != ""){
                allCellStyles.push(["["+name+"]//"+object.cellStyles[c].name, object.cellStyles[c].id]);
                }
            else{
                allCellStyles.push([object.cellStyles[c].name, object.cellStyles[c].id]);
                }
            }
        }
    // DIALOG
    // ======
    var dialog = new Window("dialog"); 
        dialog.text = "Find text"; 
        dialog.orientation = "column"; 
        dialog.alignChildren = ["left","top"]; 
        dialog.spacing = 10; 
        dialog.margins = 16; 

    // GROUP1
    // ======
    var group1 = dialog.add("group", undefined, {name: "group1"}); 
        group1.preferredSize.width = 401; 
        group1.orientation = "row"; 
        group1.alignChildren = ["left","center"]; 
        group1.spacing = 10; 
        group1.margins = 0; 

    var statictext1 = group1.add("statictext", undefined, undefined, {name: "statictext1"}); 
        statictext1.text = "Find what : "; 

    var edittext1 = group1.add('edittext {properties: {name: "edittext1"}}'); 
        edittext1.preferredSize.width = 150; 

    // DIALOG
    // ======
    var checkbox1 = dialog.add("checkbox", undefined, undefined, {name: "checkbox1"}); 
        checkbox1.text = "With Cell Style"; 
        checkbox1.value = true; 

    // GROUP2
    // ======
    var group2 = dialog.add("group", undefined, {name: "group2"}); 
        group2.orientation = "row"; 
        group2.alignChildren = ["left","center"]; 
        group2.spacing = 10; 
        group2.margins = 0; 

    var statictext2 = group2.add("statictext", undefined, undefined, {name: "statictext2"}); 
        statictext2.text = "Select Cell style : "; 

    var dropdown1_array = ["-Select-"]; 
    var dropdown1 = group2.add("dropdownlist", undefined, undefined, {name: "dropdown1", items: dropdown1_array}); 
    
        for(var c = 0; c < allCellStyles.length; c++){
            dropdown1.add("item", allCellStyles[c][0]);
            }
        dropdown1.selection = 0; 
        dropdown1.preferredSize.width = 120; 

    // DIALOG
    // ======
    var divider1 = dialog.add("panel", undefined, undefined, {name: "divider1"}); 
        divider1.alignment = "fill"; 

    // GROUP3
    // ======
    var group3 = dialog.add("group", undefined, {name: "group3"}); 
        group3.preferredSize.width = 400; 
        group3.orientation = "row"; 
        group3.alignChildren = ["right","center"]; 
        group3.spacing = 10; 
        group3.margins = 0; 

    var button1 = group3.add("button", undefined, undefined, {name: "OK"}); 
        button1.text = "Clear overrides"; 

    var button2 = group3.add("button", undefined, undefined, {name: "button2"}); 
        button2.text = "Cancel"; 

    if(dialog.show() == 1){
        // Do your work here===
        }
    }
else{
    alert ("Please open a document then proceed!!!", "No document Opened", true);
    }

Best

Sunil

TOPICS
How to, Scripting

Views

779

Likes

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
Apr 07, 2020 0
Adobe Community Professional ,
Apr 07, 2020

Copy link to clipboard

Copied

This should get you started. You'll have to explore how show and destroy work, and how you get selections from a dropdown window. 

 

var dialog = new Window("dialog"); 
dialog.text = "Cellular Biology";
var cellStyleDropDownNames = app.activeDocument.cellStyles.everyItem().name; 
var cellStyleDropDown = dialog.add("dropdownlist", undefined, undefined, {name: "cellStyleDropDown", items: cellStyleDropDownNames}); 

 
More on Dialogs: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Dialog.html

More on Dropdowns: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Dropdown.html

Likes

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
Reply
Loading...
Apr 07, 2020 1
Engaged ,
Apr 07, 2020

Copy link to clipboard

Copied

Thanks Eng.Brian for your Answer!, I modified the code a lot but the Cell Styles (list) is not showing and another problem happened, i cant search for the text, it shows error about the length? , here is my code :

var myDoc = app.activeDocument  
//Prepare for the Dialog
var myDialog = app.dialogs.add({name: "Automatic Row Styling", canCancel: true});
with(myDialog) {
    with(dialogColumns.add()) {
        with(borderPanels.add()) {
            staticTexts.add({staticLabel: "Enter First Row Word"});
            myInput = textEditboxes.add({editContents: "ABC"});
                }
            with(borderPanels.add()) {
                //Adding Cell Styles
                staticTexts.add({staticLabel: "Select Cell Style ?"});
                var cellStyleDropDownNames = myDoc.cellStyles.everyItem().name; 
                var cellStyleDropDown = dropdowns.add("dropdownlist", undefined, undefined, {name: "cellStyleDropDown", items: cellStyleDropDownNames});   
                }
            }
        }
//Display the Dialog and Assign Values
myDialog.show();
var myFound = myInput
for(i=0; i<myFound.length; i++)  
{  
   if(myFound[i].parent.constructor.name == "Cell")  
    {  
       var parentRowCells = myFound[i].parent.parentRow.cells;
       for(var j=0;j<parentRowCells.length;j++)
       {
         parentRowCells[j].appliedCellStyle = cellStyleDropDown;
                   }
               var overrides = myFound[i].clearOverrides();
          }  
    }  

 

Likes

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
Reply
Loading...
Apr 07, 2020 0
Adobe Community Professional ,
Apr 08, 2020

Copy link to clipboard

Copied

Hi,

Instead of :

parentRowCells[j].appliedCellStyle = cellStyleDropDown;

 

Apply like this :

 

parentRowCells[j].appliedCellStyle = cellStyleDropDown.selection.toString();

 

Best

Sunil

Likes

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
Reply
Loading...
Apr 08, 2020 1
Engaged ,
Apr 08, 2020

Copy link to clipboard

Copied

Thank you Sunil but Actually the Cell Styles doesnt show in the dialog also the input text doesnt working and error about length not supported

 

Best regards

Hasanain

Likes

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
Reply
Loading...
Apr 08, 2020 0
Adobe Community Professional ,
Apr 08, 2020

Copy link to clipboard

Copied

Hi,

Try this Scripting UI code & update according to you:

if(app.documents.length > 0){
    var allCellStyles = [];
    getCellStyle("", app.documents[0]);
    function getCellStyle(name, object){
        for(var c = 0; c < object.cellStyles.length; c++){
            if(name != ""){
                allCellStyles.push(["["+name+"]//"+object.cellStyles[c].name, object.cellStyles[c].id]);
                }
            else{
                allCellStyles.push([object.cellStyles[c].name, object.cellStyles[c].id]);
                }
            }
        }
    // DIALOG
    // ======
    var dialog = new Window("dialog"); 
        dialog.text = "Find text"; 
        dialog.orientation = "column"; 
        dialog.alignChildren = ["left","top"]; 
        dialog.spacing = 10; 
        dialog.margins = 16; 

    // GROUP1
    // ======
    var group1 = dialog.add("group", undefined, {name: "group1"}); 
        group1.preferredSize.width = 401; 
        group1.orientation = "row"; 
        group1.alignChildren = ["left","center"]; 
        group1.spacing = 10; 
        group1.margins = 0; 

    var statictext1 = group1.add("statictext", undefined, undefined, {name: "statictext1"}); 
        statictext1.text = "Find what : "; 

    var edittext1 = group1.add('edittext {properties: {name: "edittext1"}}'); 
        edittext1.preferredSize.width = 150; 

    // DIALOG
    // ======
    var checkbox1 = dialog.add("checkbox", undefined, undefined, {name: "checkbox1"}); 
        checkbox1.text = "With Cell Style"; 
        checkbox1.value = true; 

    // GROUP2
    // ======
    var group2 = dialog.add("group", undefined, {name: "group2"}); 
        group2.orientation = "row"; 
        group2.alignChildren = ["left","center"]; 
        group2.spacing = 10; 
        group2.margins = 0; 

    var statictext2 = group2.add("statictext", undefined, undefined, {name: "statictext2"}); 
        statictext2.text = "Select Cell style : "; 

    var dropdown1_array = ["-Select-"]; 
    var dropdown1 = group2.add("dropdownlist", undefined, undefined, {name: "dropdown1", items: dropdown1_array}); 
    
        for(var c = 0; c < allCellStyles.length; c++){
            dropdown1.add("item", allCellStyles[c][0]);
            }
        dropdown1.selection = 0; 
        dropdown1.preferredSize.width = 120; 

    // DIALOG
    // ======
    var divider1 = dialog.add("panel", undefined, undefined, {name: "divider1"}); 
        divider1.alignment = "fill"; 

    // GROUP3
    // ======
    var group3 = dialog.add("group", undefined, {name: "group3"}); 
        group3.preferredSize.width = 400; 
        group3.orientation = "row"; 
        group3.alignChildren = ["right","center"]; 
        group3.spacing = 10; 
        group3.margins = 0; 

    var button1 = group3.add("button", undefined, undefined, {name: "OK"}); 
        button1.text = "Clear overrides"; 

    var button2 = group3.add("button", undefined, undefined, {name: "button2"}); 
        button2.text = "Cancel"; 

    if(dialog.show() == 1){
        // Do your work here===
        }
    }
else{
    alert ("Please open a document then proceed!!!", "No document Opened", true);
    }

Best

Sunil

Likes

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
Reply
Loading...
Apr 08, 2020 2
medos20 LATEST
Engaged ,
Apr 08, 2020

Copy link to clipboard

Copied

Thank you very much indeed for your effort and very clean code easy to understand and follow, thanks a lot

Best Regards

medos20

Likes

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
Reply
Loading...
Apr 08, 2020 0