Search code examples
javascriptselectionadobe-indesignextendscript

InDesign extendScript: How do I transform the entire selection?


By using the simple array app.selection[x], you can apply a transformation to any object in the selection, independently. But how do I apply a transformation to the entire selection together?

For example: inside InDesign, I can select two side-by-side objects and flip them horizontally, causing them to switch places and flip.

InDesign Screenshot: Example 1

Inside a script, I can target each object in the selection, but they will not switch places; they will remain in the same place and flip.

for ( var x = 0; x < app.selection.length; x++ ){
    app.selection[x].absoluteFlip = Flip.HORIZONTAL;
}

InDesign Screenshot: Example 2

I could possibly group the selection, apply a transformation, then ungroup when finished, but this seems like unnecessary bulk that could slow down the code. I can easily do it manually inside InDesign, so it should follow that there's some way to access app.selection as a single object instead of an array. Does such an object exist?


Solution

  • Not really a solution, but it's worth noting that I don't think absoluteFlip is the action being performed, but a state indicating if the item has ben flipped. It's writable so you can set the state, but I think what's happening when using the menu to flip is flipItem: http://jongware.mit.edu/idcs6js/pc_PageItem.html#flipItem, in which you can set "around" coordinates. Now getting the origin of the selection box isn't straightforward for some reason (or maybe it is but I don't know how), but you can either use first object coordinates to set the origin so you can flip it around different coordinates depending on order of selection. Or you can sort the array to find left most coordinates (or whichever is needed), like this:

        var selection_array = app.selection;
        selection_array.sort(function(a, b){return a.visibleBounds[1]-b.visibleBounds[1]})
        var flip_origin = [selection_array[0].visibleBounds[1],selection_array[0].visibleBounds[0]]
        for(i=0;i<app.selection.length;i++){
            app.selection[i].flipItem(Flip.HORIZONTAL, flip_origin);
        }
    

    Not sure it's easier or faster than grouping and ungrouping though.