/***********************************************************************/
/*                                                                     */
/*      PageWalker :: Makes instances of a page item moving            */
/*                    from page to page                                */
/*                                                                     */
/*      [Ver: 1.10]    [Author: Marc Autret]    [Modif: 07/06/09]      */
/*      [Lang: EN]     [Req: InDesign CS/CS4]   [Creat: 05/29/08]      */
/*                                                                     */
/*      Installation and usage:                                        */
/*                                                                     */
/*      1) Place the current file into Scripts/Scripts Panel/          */
/*                                                                     */
/*      2) Run InDesign                                                */
/*                                                                     */
/*      3) Create and position the "first" page item                   */
/*                                                                     */
/*      4) Duplicate and place the page item at its ending position,   */
/*         on the same page                                            */
/*                                                                     */
/*      5) Run the script from Window > Automatisation > Scripts       */
/*         (double-click on the script name)                           */
/*                                                                     */
/*      Bugs & Feedback : marc{at}indiscripts{dot}com                  */
/*                        www.indiscripts.com                          */
/*                                                                     */
/***********************************************************************/

var SCRIPT = 
	{
	name: "PageWalker",
	version: "1.10"
	};

Application.prototype.main = function()
{
if (this.selection.length != 2)
	{
	alert("You must select exactly 2 page items");
	return;
	}

var objs = [this.selection[0], this.selection[1]];
	
	
var b0 = objs[0].geometricBounds;
var b1 = objs[1].geometricBounds;
var d = [ /*xMove*/ b1[1]-b0[1] , /*yMove*/b1[0] - b0[0] ];

var pg0 = this.activeWindow.activePage;
var pgs = this.activeDocument.pages;

var p1 = null;
var r;

while ( p1 == null )
	{
	r = prompt
		(
		"The first occurrence is on page " + pg0.name +
		".\nEnter the page for the last one :",
		app.activeDocument.pages[-1].name,
		SCRIPT.name
		);
		
	if (!r) return;
	
	try	{
		p1 = pgs.itemByName(r).documentOffset;
		}
	catch(ex)
		{
		alert("The page \"" + r + "\" doesn't exist! Please try again...");
		p1 = null;
		}
	}

var p0 = pg0.documentOffset;
var n = p1 - p0;

if ( n <= 0 )
	{
	alert("The last page must be after the first one!");
	return;
	}

var dx = d[0] / n;
var dy = d[1] / n;

for ( var i = 1 ; i <= n ; i++ )
	{
	objs[0].duplicate(pgs[p0+i],[i*dx,i*dy]);
	}

objs[1].remove();

}

app.main();
