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

Alphabetically sort pages by text frame content

Explorer ,
Jul 28, 2021 Jul 28, 2021

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?

 
TOPICS
Scripting
350
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 ,
Jul 28, 2021 Jul 28, 2021

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

 

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
Advocate ,
Jul 28, 2021 Jul 28, 2021

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

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 ,
Jul 29, 2021 Jul 29, 2021
LATEST

Sorry guys, but to be honest I don't know how to put it all together. 😞

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