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
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
...
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.
Copy link to clipboard
Copied
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);
}
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?
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);
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?
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)
Copy link to clipboard
Copied
Im still getting the same result.
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;
...
Copy link to clipboard
Copied
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:
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);
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.