What I need is a shortcut that can do the same as when I press "Distribute horizontal center" when it's "Align to Spread". I have tried "Horizontal Distribute Space" it just doesn't work.
Sorry, I am also facing the same problem.
app.alignDistributePreferences.alignDistributeBounds = AlignDistributeBounds.SPREAD_BOUNDS;
will set the alignment type for a subsequent distribute operation through the UI. Or you could use:
app.activeDocument.distribute(app.selection, DistributeOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.SPREAD_BOUNDS);
to do it in one blow, operating on the current selection.
Thank you so much, John!
Now, if I may ask an additional question (as you see I'm a novice at scripting), how do I add to the script a Select All on the spread beforehand?
Well, generally speaking it's not a good idea for scripts to modify the user's selection. Unless the goal of the script is to have the user see that other things are selected.
You can get an array of all objects on the current spread with app.activeWindow.activeSpread.allPageItems though that will include internal elements of groups. If you you want top-level items (including groups), then use app.activeWindow.activeSpread.pageItems.everyItem().getElements().
So to do as you requested, you can select all with:
but if your goal is to distribute all of them, use:
app.activeDocument.distribute( app.activeWindow.activeSpread.pageItems.everyItem().getElements(), DistributeOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.SPREAD_BOUNDS);
Wow! Thanks so much John! You saved me hours of work!