Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


distribute object as per input value

Explorer ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

hello, Ive been trying to create a script that can distribute objects as per the input value on the dialog box. same as the function on the the Alignment panel. I want to have it automatically aligned to top and be equally distributed as per the input value. As of now, I am getting a lot of errors, Can anyone help me on this? Thanks in advance

TOPICS
How to, Scripting

Views

738

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
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , May 04, 2021 May 04, 2021
Try this for the dialog part—I’m using a measurementEditbox, which let’s me define any unit, which gets returned as points. I capture the edit value in the global currentSpacing variable, and then run a main() function: var currentSpacing; myDisplayDialog(); function myDisplayDialog() { var myLabelWidth = 70; var myDialog = app.dialogs.add({name:"Distribute Spacing", canCancel:true}); with(myDialog.dialogColumns.add()){ with(borderPanels.add()){ with(dialogRows...

Likes

Translate

Translate
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Would help if you posted your code and what kind of errors you are receiving. The two main functions you'll be using are Document.align() and Document.distribute(). Look at the API for those two methods. 

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
community guidelines
Explorer ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

This is the code im using. I only got those codes online and Im honestly not that good at it. currently im getting errors on the input box coz I cannot enter alpha numeric values. Lastly I cannot find a code that can top align all the selected objects.

var
currentSpacing;

myDisplayDialog();

function myDisplayDialog() {

var myLabelWidth = 70;

var myDialog = app.dialogs.add({name:"Distrubite spacing"});

with(myDialog.dialogColumns.add()){

with(borderPanels.add()){

with(dialogRows.add()){

with(dialogColumns.add()){

staticTexts.add({staticLabel:"Value"});

}

with(dialogColumns.add()){

currentSpacing = integerEditboxes.add();

}

}

}

}

var result = myDialog.show();

if(result == 1){

 
 
myInput = currentSpacing.editValue; //your variable
app.activeDocument.distribute(app.selection, DistributeOptions.HORIZONTAL_SPACE, myInput, true);

}

else if (result == 2){

exit();

}

}

if (myInput == 0){

alert("Error!\nNo Number was entered!");

exit();

}

alert(myInput);

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
community guidelines
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Try this for the dialog part—I’m using a measurementEditbox, which let’s me define any unit, which gets returned as points. I capture the edit value in the global currentSpacing variable, and then run a main() function:

 

 

 

 

var currentSpacing;

myDisplayDialog();

function myDisplayDialog() {

var myLabelWidth = 70;
var myDialog = app.dialogs.add({name:"Distribute Spacing", canCancel:true});
    with(myDialog.dialogColumns.add()){
        with(borderPanels.add()){
            with(dialogRows.add()){
                with(dialogColumns.add()){
                    staticTexts.add({staticLabel:"Value"});
                }
                with(dialogColumns.add()){
                    currentSpacing = measurementEditboxes.add({editUnits:MeasurementUnits.MILLIMETERS, editValue:0, minWidth:myLabelWidth});
                }
            }
        }       
    }

    var result = myDialog.show();
    if(result == 1){
        currentSpacing = currentSpacing.editValue; //your variable
        main();
    }else{
        myDialog.destroy();
    }
}

/**
* Run a script 
*/
function main(){
    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS
    alert("Entered measurement returned as points: " + currentSpacing)
    //app.activeDocument.distribute(app.selection, DistributeOptions.HORIZONTAL_SPACE, AlignDistributeBounds.PAGE_BOUNDS, true, currentSpacing);
}

 

 

 

 

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
community guidelines
Explorer ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

This is very helpful, thank you so much. This is the result im having, the spacing between the object became 5mm, and the objects went to the edge of the page. I want to set the measurement to inches, and hopefully not place the objects to the edge of the page. is that possible? 

1.GIF

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
community guidelines
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

It's returning as 5.6666 pts, which is equivalent to 2 mm. To fix your page issue, I assume you were using Rob's commented code. Change the first line and add the second line as follows below: 

 

app.activeDocument.distribute(app.selection, DistributeOptions.HORIZONTAL_SPACE, AlignDistributeBounds.ITEM_BOUNDS, true, currentSpacing);
app.activeDocument.align(app.selection, AlignOptions.TOP_EDGES);

 

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
community guidelines
Explorer ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

This is perfect! However, if I put (0.156 in) it converts the value to pts which is equal to (11.232pt). The spacing between the objects became 11.2 inches.

Is it possible to remove the conversion of values to pts, and have it as per the input values? or can I change the result of the conversion to inches?

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
community guidelines
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Change MILLIMETERS to INCHES in this line:

currentSpacing = measurementEditboxes.add({editUnits:MeasurementUnits.MILLIMETERS, editValue:0, minWidth:myLabelWidth});

 You can remove these two lines as well: 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS
alert("Entered measurement returned as points: " + currentSpacing)

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
community guidelines
Explorer ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Im still getting the same result. 

 

2.GIF

 

var currentSpacing;

myDisplayDialog();

function myDisplayDialog() {

var myLabelWidth = 70;
var myDialog = app.dialogs.add({name:"Distribute Spacing", canCancel:true});
with(myDialog.dialogColumns.add()){
with(borderPanels.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
staticTexts.add({staticLabel:"Value"});
}
with(dialogColumns.add()){
currentSpacing = measurementEditboxes.add({editUnits:MeasurementUnits.INCHES, editValue:0, minWidth:myLabelWidth});
}
}
}
}

var result = myDialog.show();
if(result == 1){
currentSpacing = currentSpacing.editValue; //your variable
main();
}else{
theDialog.destroy();
}
}

/**
* Run a script
*/
function main(){
app.activeDocument.distribute(app.selection, DistributeOptions.HORIZONTAL_SPACE, AlignDistributeBounds.ITEM_BOUNDS, true, currentSpacing);
app.activeDocument.align(app.selection, AlignOptions.TOP_EDGES);
}

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
community guidelines
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Add the script measurement units back in at the start of main() and set them to inches: 

 

function main(){
app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES;
...

 

 

 

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
community guidelines
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

LATEST

Hi Brian, because of the measurementEditbox special editValue, setting the script prefs to INCHES won’t work. If I enter .15in with the script prefs as inches I get this because .15in gets returned as 10.8pts, and with the script prefs set to INCHES there will be 10.8" between the objects:

 

Screen Shot 10.pngScreen Shot 9.png

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
community guidelines
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

One thing to note about the measurementEditbox is that it value is always returned as points—if the edit units are set to millimeters the user can still enter inches using a unit abbreviation, i.e. 1in. See the measurementEditbox editValue Note in the API:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#MeasurementEditbox.html

 

 

So you can set the scriptPreferences.measurementUnit to POINTS to avoid setting the user’s ruler units. The user can enter anything—5in, 5pt, 5px, 5mm—and the currentSpacing variable will come back with the entered unit translated to points. You need this even though your editUnits are set to INCHES because the editValue (your currentSpacing) always returns as points:

 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS

 

var currentSpacing;

myDisplayDialog();

function myDisplayDialog() {

var myLabelWidth = 70;
var myDialog = app.dialogs.add({name:"Distribute Spacing", canCancel:true});
    with(myDialog.dialogColumns.add()){
        with(borderPanels.add()){
            with(dialogRows.add()){
                with(dialogColumns.add()){
                    staticTexts.add({staticLabel:"Value"});
                }
                with(dialogColumns.add()){
                    currentSpacing = measurementEditboxes.add({editUnits:MeasurementUnits.INCHES, editValue:0, minWidth:myLabelWidth});
                }
            }
        }       
    }

    var result = myDialog.show();
    if(result == 1){
        currentSpacing = currentSpacing.editValue;
        main();
    }else{
        myDisplayDialog.destroy();
    }
}

/**
* Run a script 
*/
function main(){
    app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS
    app.activeDocument.distribute(app.selection, DistributeOptions.HORIZONTAL_SPACE, AlignDistributeBounds.ITEM_BOUNDS, true, currentSpacing);
    app.activeDocument.align(app.selection, AlignOptions.TOP_EDGES);

 

 

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
community guidelines
Explorer ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Thank you so much @rob day I also checked the Indesign preference if theres a default unit of measurement that I can change, but unfortunately it only reflects what unit of measurement I am using on the Ruler tool. 

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
community guidelines