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

Sort paragraphs by number height

Engaged ,
Jan 24, 2022 Jan 24, 2022

Copy link to clipboard

Copied

Hi.

 

If I have a textframe like this:

 

Title

Text Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text Text 

333333   Text   € 0,00

555555   Text   € 0,00

999999   Text   € 0,00

111111   Text   € 0,00

Text Text Text Text Text Text Text Text 

Text Text Text Text Text Text Text Text 

 

... and if I "only" like to sort the paragraphs with six-digit numbers at the beginning (descending order), is the following script a helpful approach:

 

 

letsGo() ;
function letsGo() {
    var myDoc = app.activeDocument;
    var mySelection = myDoc.selection;
    var allSelection = mySelection.length;
    var myText = mySelection[0];

    var ArtNrs = [];


    app.findGrepPreferences = app.changeGrepPreferences = null;
    app.findGrepPreferences.findWhat = '\\n';
    app.changeGrepPreferences.changeTo = "~b";
    myText.changeGrep();
    


    app.findGrepPreferences.findWhat = '\\d{6}';

    var findings = myText.findGrep(); 
    var findingsLength = findings.length;
    
    for (var i=0; i<findingsLength; i++){

        
        ArtNrs.push (findings[i].paragraphs[0]);

        findings[i].paragraphs[0].remove();

    }
alert(ArtNrs.sort());


    app.findGrepPreferences = NothingEnum.nothing; // Suchfeld leeren    
    app.changeGrepPreferences = NothingEnum.nothing; // Ersetzen-Feld leeren    

 

 

Two issues:

findings[i].paragraphs[0].remove();

doesn´t delete. And How to insert the sorted lines?

 

Thanks in advance

TOPICS
Scripting

Views

383

Translate

Translate

Report

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 ,
Jan 24, 2022 Jan 24, 2022

Copy link to clipboard

Copied

Hi cmoke73,

you could run SortParagraphs.jsx on your selection, the script that comes with every version of InDesign:

 

SortParagraphs.jsx-Script.PNG

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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 ,
Jan 24, 2022 Jan 24, 2022

Copy link to clipboard

Copied

For all sorting needed small and big, I use this FREE & Powerful Smart Sort script.
https://www.indiscripts.com/post/2020/06/introducing-smart-sort-for-indesign-cc-cs_

Votes

Translate

Translate

Report

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
Engaged ,
Jan 24, 2022 Jan 24, 2022

Copy link to clipboard

Copied

Hello guys.

 

Thank you for help.
They are both very powerful tools. And I would use it BUT: There are more than 300 items in the catalog that I need to change. I wanted to do it with a shortcut (keyboard shortcut to the script). Without the need to change any options. Do you think the script I started could do that? Of course with the appropriate correction in the script.
If you say it's not possible, then of course I have to do it differently.

Votes

Translate

Translate

Report

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 ,
Jan 26, 2022 Jan 26, 2022

Copy link to clipboard

Copied

Hi @cmoke73, you made a good start on your script but there is a bit more that needs to happen to make this work. Here is how I would do it:

 

function main() {

    var myDoc = app.activeDocument;
    var mySelection = myDoc.selection;
    var myText = mySelection[0];

    if (myText == undefined || !myText.hasOwnProperty('paragraphs')) {
        alert('Please select a text object and try again.')
        return;
    }

    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;

    // the caret ^ matches start of line
    app.findGrepPreferences.findWhat = '^\\d{6}';

    // this will be a collection of texts
    var findings = myText.findGrep();

    // here we will store the paragraphs
    var myParagraphs = [];

    // get the paragraph of each finding text
    for (var i = 0; i < findings.length; i++)
        myParagraphs.push(findings[i].paragraphs[0]);

    // get the last insertion point of the last found paragraph
    var afterPoint = myParagraphs[myParagraphs.length - 1].insertionPoints[-1];

    // sort paragraphs
    myParagraphs.sort(function (a, b) { return a.contents - b.contents });

    // duplicate paragraphs in sorted order *after* the last found paragraph
    for (var i = 0; i < myParagraphs.length; i++)
        myParagraphs[i].duplicate(LocationOptions.AFTER, afterPoint);

    // remove the original paragraphs (must do this backwards)
    for (var i = findings.length - 1; i >= 0; i--)
        findings[i].paragraphs[0].remove();

    app.findGrepPreferences = NothingEnum.nothing; // Suchfeld leeren
    app.changeGrepPreferences = NothingEnum.nothing; // Ersetzen-Feld leeren

} // end main

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Sort Art Nums');

 

The big gotcha in this is to make sure that you don't wreck the references to your original paragraphs. There are various ways of doing this, but my technique here is to just duplicate each paragraph (in the sorted order) after the found text and then remove the original found paragraphs.

 

See if it works for you.

- Mark

Votes

Translate

Translate

Report

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
Engaged ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

LATEST

Hi Mark

 

Thanks for the approach. Good idea.

I'll work my way into it. 🙂

 

 

Votes

Translate

Translate

Report

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 ,
Jan 26, 2022 Jan 26, 2022

Copy link to clipboard

Copied

Hi cmoke73,

I do not understand why you are not like to find all consecutive paragraphs you want to sort.

With something like that:

app.findGrepPreferences.findWhat = "(^\\d{6}.+\\r)+";

 Or do you want to sort the paragraphs all over the place?

And not only if they come in consecutive order?

 

Before:

 

Title
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
333333 Text € 0,00
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
555555 Text € 0,00
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
111111 Text € 0,00
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text

 

After:

 

Title
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
111111 Text € 0,00
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
333333 Text € 0,00
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text
555555 Text € 0,00
Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text

 

Could well by that I am missing something.

Because of that, please provide a sample document that does not over-simplify the case.

 

Thanks,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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
Engaged ,
Feb 01, 2022 Feb 01, 2022

Copy link to clipboard

Copied

Hi Uwe.

 

these paragraphs always come under each other or as a single line.

--- Eine ID-Datei sende ich dir per PN. ---

Votes

Translate

Translate

Report

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