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

use input value as variable

Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Hi, 

 

I would like to use the input value from a user as an integer for the margin. 

I made a dialog from scriptui.joonas.me (Thank you Joonas!)

var dialog = new Window("dialog"); 
    dialog.text = "Enter desired margin"; 
    dialog.preferredSize.width = 200; 
    dialog.orientation = "row"; 
    dialog.alignChildren = ["left","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

// GROUP1
// ======
var group1 = dialog.add("group", undefined, {name: "group1"}); 
    group1.orientation = "column"; 
    group1.alignChildren = ["fill","top"]; 
    group1.spacing = 10; 
    group1.margins = 0; 

var Margin = group1.add("statictext", undefined, undefined, {name: "Margin"}); 
    Margin.text = "% Margin"; 
    Margin.preferredSize.width = 88; 
    Margin.justify = "center"; 

var Margin1 = group1.add('edittext {justify: "center", properties: {name: "Margin1", enterKeySignalsOnChange: true}}'); 
    Margin1.helpTip = "Enter a  number for Margin"; 
    Margin1.preferredSize.width = 71; 
    Margin1.preferredSize.height = 28; 

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

var ok = group2.add("button", undefined, undefined, {name: "ok"}); 
    ok.text = "OK"; 

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

dialog.show();

Screen Shot 2021-03-27 at 4.41.19 PM.png

 

I want to capture the user input numerical and use it as a variable. 

var margin = inputValueFromUser
 
I browse through the scriptui manual, i can't seem to find the ref on how to do this. Can anyone give me some pointers please.  Cheers.
TOPICS
Scripting

Views

493

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

correct answers 1 Correct answer

Advisor , Mar 27, 2021 Mar 27, 2021

Hello ck_ny,

 

Here's something basic to get you started.....

 

var doc = app.documents[0];

var dialog = new Window("dialog"); 
    dialog.text = "Enter desired margin"; 
    dialog.preferredSize.width = 200; 
    dialog.orientation = "row"; 
    dialog.alignChildren = ["left","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

// GROUP1
// ======
var group1 = dialog.add("group", undefined, {name: "group1"}); 
    group1.orientation = "column"; 
    group1.alignChildren = ["fill","top"]; 
...

Votes

Translate

Translate
Advisor ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Hello ck_ny,

 

Here's something basic to get you started.....

 

var doc = app.documents[0];

var dialog = new Window("dialog"); 
    dialog.text = "Enter desired margin"; 
    dialog.preferredSize.width = 200; 
    dialog.orientation = "row"; 
    dialog.alignChildren = ["left","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

// GROUP1
// ======
var group1 = dialog.add("group", undefined, {name: "group1"}); 
    group1.orientation = "column"; 
    group1.alignChildren = ["fill","top"]; 
    group1.spacing = 10; 
    group1.margins = 0; 

var Margin = group1.add("statictext", undefined, undefined, {name: "Margin"}); 
    Margin.text = "% Margin"; 
    Margin.preferredSize.width = 88; 
    Margin.justify = "center"; 

var Margin1 = group1.add('edittext {justify: "center", properties: {name: "Margin1", enterKeySignalsOnChange: true}}'); 
    Margin1.helpTip = "Enter a  number for Margin"; 
    Margin1.preferredSize.width = 71; 
    Margin1.preferredSize.height = 28; 

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

var ok = group2.add("button", undefined, undefined, {name: "ok"}); 
    ok.text = "OK"; 

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

var ok = dialog.show();
if(ok == 1){

    var page = doc.pages.item(0);
    var myMargin = Margin1.text; 

            page.marginPreferences.properties = {
            top : myMargin,
            left: myMargin,
            right: myMargin,
            bottom: myMargin
       }

} else if (ok == 2){ 
exit(0);  
}

Regards,

Mike

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
Participant ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

This is great, thank you so much. Have a great wkend!

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
Advisor ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Hello,

you might want to use this one.....I modified the code to where the edittext box will only except digits also including decimal point.

 

var doc = app.documents[0];

var dialog = new Window("dialog"); 
    dialog.text = "Enter desired margin"; 
    dialog.preferredSize.width = 200; 
    dialog.orientation = "row"; 
    dialog.alignChildren = ["left","top"]; 
    dialog.spacing = 10; 
    dialog.margins = 16; 

// GROUP1
// ======
var group1 = dialog.add("group", undefined, {name: "group1"}); 
    group1.orientation = "column"; 
    group1.alignChildren = ["fill","top"]; 
    group1.spacing = 10; 
    group1.margins = 0; 

var Margin = group1.add("statictext", undefined, undefined, {name: "Margin"}); 
    Margin.text = "% Margin"; 
    Margin.preferredSize.width = 88; 
    Margin.justify = "center"; 

var Margin1 = group1.add('edittext {justify: "center", properties: {name: "Margin1", enterKeySignalsOnChange: true}}'); 
    Margin1.helpTip = "Enter a  number for Margin"; 
    Margin1.preferredSize.width = 71; 
    Margin1.preferredSize.height = 28;
    Margin1.addEventListener ("keyup", function (){
    if(this.text != ""){
     var tmpTxt = "";
      for(var c = 0; c < this.text.length; c++){
       if(parseInt(this.text[c]).toString().toLowerCase() != "nan" || this.text[c] == "."){
        tmpTxt += this.text[c];
        }
      }
     this.text = tmpTxt;
   }
});  

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

var ok = group2.add("button", undefined, undefined, {name: "ok"}); 
    ok.text = "OK"; 

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

var ok = dialog.show();
if(ok == 1){

    myMargin = Margin1.text;

    var page = doc.pages.item(0); 

            page.marginPreferences.properties = {
            top : myMargin,
            left: myMargin,
            right: myMargin,
            bottom: myMargin
    
       }

} else if (ok == 2){ 
exit(0);  
}

 

Regards,

Mike

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 ,
Mar 28, 2021 Mar 28, 2021

Copy link to clipboard

Copied

You can also use InDesign’s dialog class:

 

 

var num;
makeDialog();
$.writeln("Number = " + num)
function makeDialog(){
    var theDialog = app.dialogs.add({name:"Enter a Number", canCancel:true});
    with(theDialog.dialogColumns.add()){
        num = realEditboxes.add({editValue:0, maximumValue:100, minimumValue:0, minWidth:100});
    }

    if(theDialog.show() == true){
        num = num.editValue
        theDialog.destroy();
	}
}

var doc = app.documents[0];
var pg = doc.pages[0];
pg.marginPreferences.properties = {top:num, left:num, right:num, bottom:num}

 

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 ,
Mar 28, 2021 Mar 28, 2021

Copy link to clipboard

Copied

Here’s the template I use for the dialog class—the built in dialog has some advantages if you are trying to return different types of values:

 

Screen Shot 12.png

 

 

 

 

 

 

 

 

//dialog result variables
var angleCombo, angleEdit, CB, dropDown, integerCombo, intEdit, measureEdit, percentCombo, percentEdit, pageRadio, spreadRadio, realCombo, realEdit, textEditField, isfixed;


//check for a doc
if (app.documents.length > 0) {
    makeDialog();
} else {alert("Please open a document and try again.")}


function makeDialog(){
    //make a dialog box named
    var theDialog = app.dialogs.add({name:"Dialog Title",canCancel:true});
    with(theDialog){
        with(dialogColumns.add()){
            
            //////////////PANEL 1///////////////
            
            //the panel title
            with(dialogRows.add()){
                    staticTexts.add({staticLabel:"Panel Title", staticAlignment:StaticAlignmentOptions.LEFT_ALIGN, minWidth: 150});
            }
            //the panel
            with(borderPanels.add()){
                with(dialogColumns.add()){
                    
                    //angle Combo
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Angle Combo"});
                        angleCombo = angleComboboxes.add({stringList:["0", "90", "180"], editValue:0, maximumValue:360, minimumValue:0, smallNudge:1, largeNudge:10, minWidth:50});
                    }
                    
                    //angle Edit
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Angle Edit"});
                        angleEdit = angleEditboxes.add({editValue:0, maximumValue:360, minimumValue:0, smallNudge:1, largeNudge:10, minWidth:50});
                    }
                
                    //Checkbox control
                    with(dialogRows.add()){
                        CB = checkboxControls.add({staticLabel:"Check Box", checkedState:true, minWidth:150});
                    }
                
                    //Dropdown
                    with(dialogRows.add()){
                        var presetList = ["Mustard", "Relish", "Ketchup"]
                        dropDown = dropdowns.add({stringList:presetList, selectedIndex:0, minWidth:80});
                    }  
                }
            }
        
        
            //////////////PANEL 2 (Enabling) ///////////////
            


            with(dialogRows.add()){
                    staticTexts.add({staticLabel:"Output Width", staticAlignment:StaticAlignmentOptions.LEFT_ALIGN, minWidth: 150});
            }
        
            isFixed = enablingGroups.add({checkedState:false, minWidth:150})
            with(isFixed){
                staticTexts.add({staticLabel:"Width"});
                fixedWidth = measurementComboboxes.add({stringList:[pageWidth.toString(), "600", "800", "1200", "1800"], editUnits:MeasurementUnits.PIXELS, editValue:pageWidth});
            }
        
            //////////////PANEL 3 ///////////////
            with(borderPanels.add()){
                with(dialogColumns.add()){
                    
                    //Integer Combo
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Integer Combo"});
                        integerCombo = integerComboboxes.add({stringList:["1", "2", "3"], editValue:0, maximumValue:100, minimumValue:0, smallNudge:1, largeNudge:10, minWidth:50});
                    }
                    
                    //Integer Edit
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Integer Edit"});
                        intEdit = integerEditboxes.add({editValue:1, maximumValue:100, minimumValue:0, smallNudge:1, largeNudge:10, minWidth:50});
                    }
                
                    //Measurement Edit. Note max and min units are always points
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Measurement Edit"});
                        measureEdit = measurementEditboxes.add({editUnits:MeasurementUnits.MILLIMETERS, editValue:0, maximumValue:1000, minimumValue:0, smallNudge:1, largeNudge:10, minWidth:90});
                    }
                
                    //Percent Combo
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Percent Combo"});
                        percentCombo = percentComboboxes.add({stringList:["10", "15", "20"], editValue:10, maximumValue:100, minimumValue:0, smallNudge:1, largeNudge:10, minWidth:50});
                    }
                
                    //Percent Edit
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Percent Edit"});
                        percentEdit = percentEditboxes.add({editValue:15, maximumValue:100, minimumValue:0, smallNudge:1, largeNudge:10, minWidth:50});
                    } 
                    
                    //radiobutton group
                    with(dialogRows.add()){
                        with(radiobuttonGroups.add()){
                            pageRadio = radiobuttonControls.add({staticLabel:"Pages ", checkedState:true});
                            spreadRadio = radiobuttonControls.add({staticLabel:"Spreads "});
                        }
                    }
                
                    //Real Combo
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Real Combo"});
                        realCombo = realComboboxes.add({stringList:[".1", ".2", ".3"], editValue:0, maximumValue:1, minimumValue:0, smallNudge:.01, largeNudge:.1, minWidth:50});
                    }
                    
                    //Real Edit
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Real Edit"});
                        realEdit = realEditboxes.add({editValue:0, maximumValue:10, minimumValue:0, smallNudge:.01, largeNudge:.1, minWidth:50});
                    }
                
                    //Text Edit
                    with(dialogRows.add()){
                        staticTexts.add({staticLabel:"Message"});
                        textEditField = textEditboxes.add({editContents:"Hello World!", minWidth:150});
                    }
                }
            }
        }
    }

    //the dialog results
    var res = theDialog.show();
    if(res == true){
        angleCombo = angleCombo.editValue; 
        angleEdit = angleEdit.editValue; 
        CB = CB.checkedState;
        dropDown = presetList[dropDown.selectedIndex];
        isFixed:isFixed.checkedState,
        integerCombo = integerCombo.editValue;
        intEdit = intEdit.editValue
        measureEdit = measureEdit.editValue
        percentCombo = percentCombo.editValue
        percentEdit = percentEdit.editValue
        pageRadio = pageRadio.checkedState
        spreadRadio = spreadRadio.checkedState
        realCombo = realCombo.editValue
        realEdit = realEdit.editValue
        textEditField = textEditField.editContents
        theDialog.destroy();
        main()
	}else{
        theDialog.destroy();
    }
}



/**
* Discription 
* @Return value 
* 
*/
function main(){
    
    alert("\ncombo angle= " + angleCombo + "\nangle= " + angleEdit + "\nchecked = " + CB + "\ninteger = " + intEdit + "\nmeasure edit = " + measureEdit)

}




 

 

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
Participant ,
Mar 28, 2021 Mar 28, 2021

Copy link to clipboard

Copied

LATEST

Indesign's dialog will come in handy for future needs. So much to learn. If I were only younger.  🙂

Thank you both. 

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