0

I have a nearly 400 page document that I need to [randomly] reorder the pages. (If you need to know, this is a book of single page stories that need to be randomly distributed. I created a random list of pages to input into the script.)

I've been working with a modified script I found elsewhere on the internet that creates an array and moves the pages around:

var order="...list of new page numbers...";  
// Create an array out of the list:  
ranges = toSeparate (order);  
if (ranges.length != app.activeDocument.pages.length)  
{  
 alert ("Page number mismatch -- "+ranges.length+" given, "+app.activeDocument.pages.length+" in document");  
 exit(0);  
}  
// Consistency check:  
sorted = ranges.slice().sort(numericSort);  
for (a=0; a<sorted.length-1; a++)  
{  
 if (sorted[a] < sorted[a+1]-1 ||  
  sorted[a] == sorted[a+1])  
  alert ("Mismatch from "+sorted[a]+" to "+sorted[a+1]);  
}  
// alert ("New order for "+order+"\nis "+ranges.join(", "));  
// Convert from 1..x to 0..x-1:  
for (moveThis=0; moveThis<ranges.length; moveThis++)  
 ranges[moveThis]--;  
for (moveThis=0; moveThis<ranges.length; moveThis++)  
{  
 if (moveThis != ranges[moveThis])  
 {  
  try{  
   app.activeDocument.pages[ranges[moveThis]].move (LocationOptions.BEFORE, app.activeDocument.pages[moveThis]);  
  } catch(_) { alert ("problem with page "+moveThis+"/index "+ranges[moveThis]); }  
 }  
 for (updateRest=moveThis+1; updateRest<ranges.length; updateRest++)  
  if (ranges[updateRest] < ranges[moveThis])  
   ranges[updateRest]++;  
}  
function toSeparate (list)  
{  
 s = list.split(",");  
 for (l=0; l<s.length; l++)  
 {  
  try {  
  if (s[l].indexOf("-") > -1)  
  {  
   indexes = s[l].split("-");  
   from = Number(indexes[0]);  
   to = Number(indexes[indexes.length-1]);  
   if (from >= to)  
   {  
    alert ("Cannot create a range from "+from+" to "+to+"!");  
    exit(0);  
   }  
   s[l] = from;  
   while (from < to)  
    s.splice (++l,0,++from);  
  }} catch(_){}  
 }  
// s.sort (numericSort);  
 return s;  
}  
function numericSort(a,b)  
{  
 return Number(a) - Number(b);  
} 

This code worked, except that it was consistently rearranging them into the wrong random order, which, at the end of the day, is workable, but it'll just be a bigger pain in the ass to index the stories.

I suspected the problem might be caused by starting at the begginning of the document rather than the end, so I modified the script to start at the end, but then app.activeDocument.pages[ranges[moveThis]] kept coming up as undefined.

So I gave up and tried this:

   app.activeDocument.pages[298].move (LocationOptions.BEFORE, app.activeDocument.pages[366]);
   app.activeDocument.pages[33].move (LocationOptions.BEFORE, app.activeDocument.pages[365]);
   app.activeDocument.pages[292].move (LocationOptions.BEFORE, app.activeDocument.pages[364]);

And so on for every page. (This reminds me of my time in junior high using sendKeys to create programs in Visual Basic. Had I bothered to seriously learn JavaScript instead of creating shitty AOL chatroom scrollers, I probably wouldn't be on here today.)

Nevertheless, I received the following error:

Error Number: 30477 Error String: Invalid value for parameter 'reference' of method 'move'. Expected Page or Spread, but received nothing.

I'm trying to avoid having to manually move the pages, especially considering the amount of time I've already been working on this. Any suggestions on what I need to change? Thank you!

cigien
  • 57,834
  • 11
  • 73
  • 112
Cody S
  • 11
  • 1
  • Rather than move the pages around in a single document, have you considered inserting the pages randomly in a new document? Also: re: indexing, why can't you generate a table of contents automatically? – cybernetic.nomad Jun 14 '18 at 16:48
  • I haven't considered that, but I love simple solutions like that. That'll probably be what I end up doing. As for the index, it won't work that way because of how things are set up outside of indesign. – Cody S Jun 14 '18 at 17:08

1 Answers1

0

The issue might be that you are using more than one page per spread and then trying to shuffle them across spread. The better way is to use single page per spread. Here is a small snippet that works on my machine

var doc = app.activeDocument; doc.documentPreferences.facingPages = false; for (var i =0; i < 100; i++){ var index = parseInt((Math.random() * doc.spreads.length) % doc.spreads.length + '' , 10); doc.spreads[index].move(); }

What this does is

  1. Disables the facing pages options and makes one page per spread. A desirable condition as you mentioned that your stories are one page each(I am assuming that your stories will not violate this assumption).
  2. Takes a random spread from the doc and sends it to the end of the spreads in the doc. It does so 100 times. The result is what you wanted. A script to shuffle the current SPREADS randomly.