Skip navigation
Home/Support/

Forums

1298 Views 18 Replies Latest reply: Aug 31, 2009 10:55 AM by Harbs. RSS
dhishok User 222 posts since
Jul 26, 2005
Currently Being Moderated

Aug 26, 2009 11:45 AM

make selection and move page items...

Hi everyone,

 

Currently we are working with InDesign CS3 documents. I have to select all page items of each page and to move it with 9 pts both horizontal as well as vertical position. If it is recto page move it to -9 pts and if it is verso page have to move it +9pts. So what i am doing is, trying to select the all page elements and make into a group. After this have to get the geometric bounds of the group and move it by 9 pts. I got some error while make my selection into group. Find the script below

 

//------------------------------------

var doc=app.activeDocument;

var mySelection=doc.pageItems.everyItem().select();

var myGroup=doc.groups.add(mySelection);

//------------------------------------

 

can anyone look into this help me out. Also suggest me if there is any better way to do this.

 

Regards

Thiyagu

  • mmsidon Calculating status... 3 posts since
    Aug 28, 2009
    Currently Being Moderated
    1. Aug 28, 2009 11:44 PM (in response to dhishok)
    Re: make selection and move page items...

    i Thiyagu,

     

    you are quite inspired by doing manual work.... (this is quite good to learn, but too complicated in production)

     

    You don't have to "select" the items you want to group.

    Selection is (and should be) rarely used in scipting!

     

    First of all IMHO the error (which you could have written in your message as well, so we can identifiy problems better) when grouping is that a group has to be bound to a page instead of a document.

     

    So you will have to iterate through the pages and then group pageItems on each page! (otherwise you also cannot make a different move on different pages!)


    //------------------------------------

    var doc=app.activeDocument;


    for (i=0; i<doc.pages.count; i++) {

      thisPage = doc.pages[i];

      items1 = thisPage.PageItems;


      if ( thisPage.PageItems.length> 1 )    
      {

        myMovingItem = thisPage.groups.add(thisPage.PageItems);

      } else {

        myMovingItem = thisPage.PageItems.firstItem();

      }


         [... move your "myMovingItem" here !!! and ungroup the group if you want...]

    }

    //------------------------------------

     

     

    (Code is written by memory and NOT TESTED !)

     

    Greetings,

    Marc

  • Marc Autret Participant 382 posts since
    Jun 12, 2009
    Currently Being Moderated
    2. Aug 29, 2009 7:08 AM (in response to dhishok)
    Re: make selection and move page items...

    Try this:

    var dx = 9, u = 'pt';
     
    Object.prototype.offsetX = function()
         {
         if (this.parent.constructor != Page) return;
         var sgn = 1 - 2*(this.parent.documentOffset%2);
         this.move(undefined,['' + sgn*dx + u,0]);
         }
     
    var items = app.activeDocument.pageItems.everyItem().getElements();
    var obj;
    while(obj=items.pop()) obj.offsetX();
    

    Marc

  • [Jongware]-GfBROo Star 5,362 posts since
    Dec 17, 2006
    Currently Being Moderated
    5. Aug 30, 2009 3:00 PM (in response to dhishok)
    Re: make selection and move page items...

    From the OMV:

     

    void move ([to: {Array of 2 Units | Spread | Page | Layer} ][, by: Array of Measurement Unit
    (Number or String)=any])
    

     

    As you can see, Marc sets the first argument (to) to undefined, and the 2nd to the distance, in an array of 2 units.

    The sign stuff is needed in case the zero point of your document is inbetween spreads (and what a dirty trick this is, I'm tempted to copy it as my own! ). On left pages it will be -1, on right pages +1.

    The variable dx is a global value (Marc: would it be possible to put this in the declaration, as "function( dx, dy)"?). The second value in the array is set to 0, and that's where you would put the y distance. The y distance does not need any special sign handling as it always will move down with a positive value.

  • Marc Autret Participant 382 posts since
    Jun 12, 2009
    Currently Being Moderated
    6. Aug 30, 2009 6:51 PM (in response to [Jongware]-GfBROo)
    Re: make selection and move page items...

    [Jongware] wrote:

     

    Marc: would it be possible to put this in the declaration, as "function( dx, dy)"?

     

    Of course it would

     

    var unit = 'pt';

    Object.prototype.offsetXY = function(/*int*/dx, /*int*/dy)
         {
         if (this.parent.constructor != Page) return;
         var sgn = 1 - 2*(this.parent.documentOffset%2);
         this.move(undefined,['' + sgn*dx + u, '' + dy + unit]);
         }

    var items = app.activeDocument.pageItems.everyItem().getElements();
    var obj;
    while(obj=items.pop()) obj.offsetXY(9,9); // dx = +/-9, dy = +9

     

    The code could/should be improved in various ways. I don't like global variables, too.

     

    @+

    Marc

  • Marc Autret Participant 382 posts since
    Jun 12, 2009
    Currently Being Moderated
    7. Aug 30, 2009 8:03 PM (in response to dhishok)
    Re: make selection and move page items...

    thiyagus5 wrote:

     

    Can you please explain the below syntax just i want to understand the things.

     

    Object.prototype.offsetX = function()
        {
        /* 1 */ if (this.parent.constructor != Page) return;
        /* 2 */ var sgn = 1 - 2*(this.parent.documentOffset%2);
        /* 3 */ this.move(undefined,['' + sgn*dx + u,0]);
        }

     

     

    First, I prototype an offsetX method at the Object level, so everything is supposed to be offset-able. That makes things easier, because we don't know a priori what kind of items we will have to move: the Object method will ‘catch’ anything from the syntax <anyPageItem>.offsetX()

     

    In the body of the function, line 1, I ignore the object (this) if it is not a page child item, because your goal is to move only page items. Ideally, we should also test if the object can move :

    /* 1bis */ if (! (move in this) ) return;
    

     

    Line 2, I need to get the sign of the x-move according to the even/odd parity of the page index in the document (this.parent.documentOffset). Here I supposed that the document was structured by facing pages (2-pages spreads). The point is that N%2 returns 0 if N is an even number, 1 if it is an odd number (% is the modulo operator). Then, I use the transformation  X -> 1 - 2X  returning +1 if X=0 and -1 if X = 1. That's the sign of the translation.

    var sgn = 1 - 2 * (this.parent.documentOffset % 2 );
    

    Finally, line 3, we apply the sign to dx and we make the move! (Note that you may use a negative dx if you need to reverse the process.)

     

    Regards,

    Marc

  • Harbs. Contributor 4,940 posts since
    Apr 19, 2004
    Currently Being Moderated
    8. Aug 31, 2009 12:21 AM (in response to [Jongware]-GfBROo)
    Re: make selection and move page items...

    Yes, the sign stuff is a nice trick, but it'll fail miserably on a 

    right bound document, so I'd avoid standardizing this trick (unless of 

    course you'd put in a pageBinding condition)...

     

    Harbs

  • Marc Autret Participant 382 posts since
    Jun 12, 2009
    Currently Being Moderated
    9. Aug 31, 2009 3:37 AM (in response to Harbs.)
    Re: make selection and move page items...

    Harbs. wrote:

     

    Yes, the sign stuff is a nice trick, but it'll fail miserably on a 

    right bound document, so I'd avoid standardizing this trick (unless of 

    course you'd put in a pageBinding condition)...

     

    I totally agree with you. This script is not to be standardized, it's just a proposal. There are many other circumstances where it will fail miserably.

    I was just trying to answer a specific question.

     

    @+

    Marc

  • [Jongware]-GfBROo Star 5,362 posts since
    Dec 17, 2006
    Currently Being Moderated
    10. Aug 31, 2009 3:53 AM (in response to Marc Autret)
    Re: make selection and move page items...

    I agree -- it's not the hot trick I thought it was. No prob, there are lots of ways to skin a cat.

     

    Since the object's immediate parent should be a Page, you could check for (obj.parent.index) & 1 -- it'll be 0 for even pages, 1 for odds.

     

    [Add] And because of my C background, I would use this

     

    sign = (obj.parent.index) & 1 ? 1 : -1;

     

    which, honestly!, isn't produced by pressing random keys :-P

  • Harbs. Contributor 4,940 posts since
    Apr 19, 2004
    Currently Being Moderated
    11. Aug 31, 2009 4:25 AM (in response to Marc Autret)
    Re: make selection and move page items...

    Hi Marc,

     

    I understood that. My response was to Jong; not to your solution per se.

     

    FWIW, if you want to make that work with right-bound docs, this should 

    do the trick:

    
    Object.prototype.offsetXY = function(/*int*/dx, /*int*/dy)
          {
          *if* (this.parent.constructor != Page) return;
          var sgn = this.parent.side == PageSideOptions.LEFT_HAND ? -1 : 1;
          this.move(undefined,['' + sgn*dx + u, '' + dy + unit]);
          }

     

    Harbs

  • Marc Autret Participant 382 posts since
    Jun 12, 2009
    Currently Being Moderated
    12. Aug 31, 2009 5:11 AM (in response to [Jongware]-GfBROo)
    Re: make selection and move page items...

    [Jongware] wrote:

     

    (...)

     

    sign = (obj.parent.index) & 1 ? 1 : -1;

     

    (...)

     

    Wow! That's a pretty elegant approach.

    In this case, what about :

    sign = (obj.parent.index) ? 1 : -1;

    (not tested)

     

    Anyway, I think Harbs does the right thing in using the Page.side property. Technically, it is probably the best way.

     

    @+

    Marc

  • [Jongware]-GfBROo Star 5,362 posts since
    Dec 17, 2006
    Currently Being Moderated
    13. Aug 31, 2009 6:38 AM (in response to Marc Autret)
    Re: make selection and move page items...

    No, that wouldn't work [a]. I misplaced a parenthesis, which may well be critical, as it should change the precendence of the operators! I meant to say

     

    sign = (obj.parent.index & 1) ? 1 : -1;

     

    so the (obj.parent.index & 1) gets evaluated first. I'm not sure at all it even works without the parentheses ...

    In your example, it still would do something, but only the first page would be correct :-) The ?..: operator is shorthand for a full if .. then .. else expression; the value "0", for the first page, evaluates to 'false', and any other value (for all other pages) evaluate to 'true'.

     

    [a] Your expression, I meant. Harbs' snidely side shows I didn't really memorize the OMV from first page to last -- first time I've ever heard of that!

  • Marc Autret Participant 382 posts since
    Jun 12, 2009
    Currently Being Moderated
    14. Aug 31, 2009 6:57 AM (in response to [Jongware]-GfBROo)
    Re: make selection and move page items...

    [Jongware] wrote:

     

    (..) The ?..: operator is shorthand for a full if .. then .. else expression (...)

    Hey, Jong, I knew that ;-)

     

    I only suggested to replace your condition (Page.index & 1) simply by (Page.index), since the index is supposed to be 0 or 1. (That's the index of the page in its spread, right?) Then, my code should work:

     

    var sign = (obj.parent.index) ? 1 : -1;

     

    JS will evaluate the condition to true if index=1, to false if index=0. So, the binary "AND 1" operation seems useless in this specific situation.

     

    Did I miss something?

     

    @+

    Marc

  • [Jongware]-GfBROo Star 5,362 posts since
    Dec 17, 2006
    Currently Being Moderated
    15. Aug 31, 2009 7:14 AM (in response to Marc Autret)
    Re: make selection and move page items...
    Did I miss something?

     

    Eh. From the OMV:

    "The index of the Page within its containing object." Actually, I was assuming this would be the index of the page in the document? I never thought a Page parent could be a Spread (and yes, the OMV tells me it is a Spread).

     

    (1 min later) Okay, you are so right about this. This script

     

    alert (app.activeDocument.layoutWindows[0].activePage.index);

     

    shows nothing but 0 and 1. And that's for a regular document -- I imagine I'd get higher values for those combined to side-by-sides by dragging in the pages panel.

     

    I'm pretty sure I used it at least once my way, but then again -- hey, it works (for a normal book).

     

    Let's not confuse poor Thiyagu any further

  • Marc Autret Participant 382 posts since
    Jun 12, 2009
    Currently Being Moderated
    16. Aug 31, 2009 8:41 AM (in response to [Jongware]-GfBROo)
    Re: make selection and move page items...

    [Jongware] wrote:

     

    (...)

    Actually, I was assuming this would be the index of the page in the document? I never thought a Page parent could be a Spread (and yes, the OMV tells me it is a Spread).

     

    (...)

    Long ago, I made diagrams to understand this kind of relationships:

     

    http://marcautret.free.fr/geek/indd/app2page/fig03.png

    http://marcautret.free.fr/geek/indd/pageitem/fig02.png

    This needs to be updated, but it remains useful as an overview.

  • [Jongware]-GfBROo Star 5,362 posts since
    Dec 17, 2006
    Currently Being Moderated
    17. Aug 31, 2009 9:28 AM (in response to Marc Autret)
    Re: make selection and move page items...

    It's pretty. What is it?

     

    (I'm joking -- of course I know, it's the schematics of my Intel(R) Core(TM)2 Duo CPU!)

     

    Jokes aside, you made that by hand? (Sweet.) Perhaps you could use the parent/child hierarchies in my version of the OMV Help to update it?

  • Harbs. Contributor 4,940 posts since
    Apr 19, 2004
    Currently Being Moderated
    18. Aug 31, 2009 10:55 AM (in response to Marc Autret)
    Re: make selection and move page items...

    Yes index works, but again, only for left bound docs (and docs with no 

    more than two pages per spread...)

     

    For the index within the doc, you'd use documentOffset.

     

    Harbs

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points