Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

make selection and move page items...

Explorer ,
Aug 26, 2009 Aug 26, 2009

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

TOPICS
Scripting
4.9K
Translate
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

Guide , Aug 29, 2009 Aug 29, 2009

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

Translate
Community Beginner ,
Aug 28, 2009 Aug 28, 2009

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;

  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

Translate
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 ,
Aug 30, 2009 Aug 30, 2009

Hi mmsidon,

Sorry i was out of station for past 3 days. I have checked your script and i did very few changes and its working greatly. Thanks for your kind support.

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

var doc=app.activeDocument;
for (i=0; i<doc.pages.length; i++) {
  thisPage = doc.pages;
  items1 = thisPage.pageItems;
  if ( thisPage.pageItems.length> 1 )      {    
   myMovingItem = thisPage.groups.add(thisPage.pageItems);
  } else {
    myMovingItem = thisPage.PageItems.firstItem();
  }
}
//-------------------------------------

I have to work for getting bounds, X Y movement and then let you know. Once again i am thanking you.

Regards

Thiyagu

Translate
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
Guide ,
Aug 29, 2009 Aug 29, 2009

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

Translate
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 ,
Aug 30, 2009 Aug 30, 2009

Hi Marc Autret,

Wow!! its working greatly. Thank you so much. Your syntaxes are little bit tricky to understand but it has done the movements perfectly without grouping. Can you please explain the below syntax just i want to understand the things.

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]);
    }

Also I need to do vertical movement 9pts. i.e. Y axis movement. How it can be done using your script?

Thanks in advance.

Regards

Thiyagu

Translate
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
Community Expert ,
Aug 30, 2009 Aug 30, 2009

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.

Translate
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
Guide ,
Aug 30, 2009 Aug 30, 2009

[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

Translate
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
LEGEND ,
Aug 30, 2009 Aug 30, 2009

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

Translate
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
Guide ,
Aug 31, 2009 Aug 31, 2009

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

Translate
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
Community Expert ,
Aug 31, 2009 Aug 31, 2009

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 😛

Translate
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
Guide ,
Aug 31, 2009 Aug 31, 2009

[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

Translate
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
Community Expert ,
Aug 31, 2009 Aug 31, 2009

No, that wouldn't work . I misplaced a parenthesis, which may well be critical, as it should change the precendence of the ...

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'.

Your expression, I meant. Harbs' snidely side shows I didn't really memorize the OMV from first pag...

Translate
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
Guide ,
Aug 31, 2009 Aug 31, 2009

[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

Translate
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
Community Expert ,
Aug 31, 2009 Aug 31, 2009
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

Translate
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
Guide ,
Aug 31, 2009 Aug 31, 2009

[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.

Translate
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
Community Expert ,
Aug 31, 2009 Aug 31, 2009

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?

Translate
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
LEGEND ,
Aug 31, 2009 Aug 31, 2009
LATEST

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

Translate
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
LEGEND ,
Aug 31, 2009 Aug 31, 2009

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

Translate
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
Guide ,
Aug 30, 2009 Aug 30, 2009

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

Translate
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