Copy link to clipboard
Copied
Hi everyone.
I need to sort a several dozen of 300 page documents by content in specific text frame. Each page contains specific text frame with static caption - filename. Such text frames have a script label "Caption".
It looks like in example below:
Page 1 - IMG_0051.jpg
Page 2 - IMG_9999.jpg
Page 3 - IMG_0199.jpg
Page 300 - IMG_0004.jpg
I'd like to sort pages alphabetically like this:
Page 300 - IMG_0004.jpg
Page 1 - IMG_0051.jpg
Page 3 - IMG_0199.jpg
Page 2 - IMG_9999.jpg
Can anyone have an idea how to do this by script?
Copy link to clipboard
Copied
Write a custom sort function, a function to get contents by label, then either duplicate the sorted page array to a new document, or move the alpha sorted page before the beta sorted page. Lots could go wrong, and not exactly a trivial task. A sample sort algorithm might look like this:
You'd need a separate function to get the contents of the each label for the page, and pass them instead of the contentsByLabel line I have.
var pages = app.documents[0].pages.everyItem().getElements();
pages.sort(function(x, y) {
if (x.contentsByLabel < y.contentsByLabel) {
return -1;
}
if (x.contentsByLabel > y.contentsByLabel) {
return 1;
}
return 0;
});
Copy link to clipboard
Copied
For this particular requirement, you can achieve like this:
var sortedPage = [];
var sortedString = [];
var myDoc = app.documents[0];
for(var p = 0; p < myDoc.pages.length; p++){
for(var t = 0; t < myDoc.pages[p].textFrames.length; t++){
var txt = myDoc.pages[p].textFrames[t].contents.toString();
if(txt != ""){
if(txt < sortedString[sortedString.length-1]){
var index = sortedString.length-1;
while(index > 0 && txt < sortedString[index-1]) index--;
sortedString.splice(index, 0, txt);
sortedPage.splice(index, 0, myDoc.pages[p]);
}
else{
sortedString.push(txt);
sortedPage.push(myDoc.pages[p]);
}
}
}
}
//~ alert(sortedPage);
alert(sortedString);
Best
Sunil
Copy link to clipboard
Copied
Sorry guys, but to be honest I don't know how to put it all together. 😞
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more