Copy link to clipboard
Copied
Hello,
Is it possible to swap an entire page and its contents position with another page withing the same document?
Any help would be greatly appreciated.
Copy link to clipboard
Copied
Try this:
function swapPages(p0, p1)
{
if (p0 == p1) return true;
var pages = app.activeDocument.pages;
var pg0 = pages.itemByName(''+p0);
var pg1 = pages.itemByName(''+p1);
if ( pg0.documentOffset > pg1.documentOffset ) return swapPages(p1,p0);
try {
pg1.move(LocationOptions.BEFORE,pg0);
pg0.move(LocationOptions.AFTER,pages.itemByName(''+p1));
return true;
}
catch(ex) {return false;}
}
// usage:
swapPages(5,8);
@+
Marc
Copy link to clipboard
Copied
Thanks!
Copy link to clipboard
Copied
Hello Marc,
I wanted to give your function (thank you for that!) a little UI using a prompt to feed in the two page numbers.
First I tried the following:
var a = prompt(
"",
"1,"+app.activeDocument.pages.length, //Predefined page numbers: first & last page"Insert TWO page numbers for swapping pages sepparated by a comma."+"\n"+"First and last page of the document are:"
);swapPages(a);
That doesn't work as expected. I had a test with a 12-pages document and "$.writeln(a);" wrote "1,12" to the console of the ESTK.
Error: "p0 is not defined"
Why isn't it posible to use the string "1,12" to feed the function?
I was looking for a solution, so I enhanced the code by:
var b=a.split(",");swapPages(b[0],b[1]);
That is working now. But still the question remains…
Uwe
Copy link to clipboard
Copied
If you want a UI, you can try this script: http://forums.adobe.com/message/2376190#2376190
Copy link to clipboard
Copied
Laubender wrote:
That is working now. But still the question remains…Uwe
Because you fed the function a single string rather than two arguments.
Harbs
Copy link to clipboard
Copied
Thank you, Harbs. So now I know that the comma in the function call cannot be replaced by a string.
Uwe
Copy link to clipboard
Copied
Oops. Forget the error message. This was a mistake. Error was "Object is invalid".
Uwe