Skip to main content
Inspiring
October 20, 2016
Answered

Sort paragraphs in a certain way

  • October 20, 2016
  • 2 replies
  • 2814 views

Hi there.

My current thread deals with the re-sorting of the paragraphs in a text frame.

This is like it looks before:

HEADLINE

First text (description). xxxxxxxxxxxxx

xxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx xxxxxx

xxxxxxxxxxxxx xxxxxxxxxxx xxxxxxxxx xxxxxxx

xxxxxxxxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx xxx

xxxxxxxxxxxxxxx xxxxxxxx xxxxxxx.

Second descriptive text xxxxxx xxxxxxxx x

xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxx xxx

xxxxxxxxxxxxx xxxxxxxxx xxxxxxxxxxxxxx xxxxxxxx

Article 1

Article 2

Article 3

Article n

This is how I want to have it after re-sorting:

HEADLINE

First text (description). xxxxxxxxxxxxx

xxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx xxxxxx

xxxxxxxxxxxxx xxxxxxxxxxx xxxxxxxxx xxxxxxx

xxxxxxxxxxxxxxxxxxx xxxxxxxxxx xxxxxxxx xxx

xxxxxxxxxxxxxxx xxxxxxxx xxxxxxx.

Article 1

Article 2

Article 3

Article n

Second descriptive text xxxxxx xxxxxxxx x

xxxxxxxxxx xxxxxxxxxx xxxxxxxxxx xxxxxxx xxx

xxxxxxxxxxxxx xxxxxxxxx xxxxxxxxxxxxxx xxxxxxxx

The number of articles changes. Each of them is a paragraph for itself.

Next step will be the new formating of the paragraphs,  if it is important for this thread...

I guess it works with an array in which all paragraphs are stored. Then they must be called up in a special order.

Only: I do not know the correct syntax for that... Can anybody help me in the easiest way?

This topic has been closed for replies.
Correct answer Skemicle

Your original script attempt doesn't work only because you are applying the paragraph style to the contents rather than the paragraph.. apply the appliedParagraph style to myParagraphs[2] rather than myArray[2].

BUT... You don't need to do anything with styles for the script to do what you want.. Back to my comment to Obi-wan

  1. adding "\r" as the last character of the textFrame,
  2. moving paragraph[2] to the end of the textFrame,
  3. removing the last character from the textFrame.

That's all you need no styling necessary:

sel = app.selection[0];

sel.characters[-1].contents = "\r";

sel.paragraphs[2].move(LocationOptions.AT_END);

sel.characters[-1].contents = "";

I just needed to see you attempt for yourself before i would provide this .

2 replies

cmoke73Author
Inspiring
October 20, 2016

I would begin like this (It does not really work, but there are also no error messages):

var doc = app.documents; 

var dieAuswahl = app.selection[0] ;

var ersterAbsatz = dieAuswahl.paragraphs[0];   

var myParagraphs = app.selection[0].paragraphs;

var myArray = new Array();

myArray = myParagraphs.everyItem().contents;

myArray[2].appliedParagraphStyle = "Red"; //try to apply a paragraph style to the 3rd paragraph - nothing happens

// an other way???

//~ for ( i=0; i<myParagraphs.length; i++) {

//~     myArray;

//~     }

 

but how can I pick out now the last para and move it?

Obi-wan Kenobi
Legend
October 21, 2016

This version seems to work but apparently a problem somewhere!!

To make it work, we need to limit the treatment of the script on the good text frames, e.g., only the text frames with a specific para style!

In my sample, the "Blue_Para" paras are the target!

So … if we consider all text frames are independent frames (no threaded):

… using this script, not perfect [to be validated]:

var myDoc = app.documents.item(0);

var myTFrames = myDoc.textFrames;

for (t = 0 ; t < myTFrames.length; t++)

    {

        var myTFrame = myTFrames;

        var myParas = myTFrame.parentStory.paragraphs; 

       

        if (myParas[0].appliedParagraphStyle.name == "Blue_Para")

            {

                app.findGrepPreferences = app.changeGrepPreferences = null;

                app.findGrepPreferences.findWhat = ".\\z(?!\\r)";

                app.changeGrepPreferences.changeTo = "$0\r";

                myTFrame.changeGrep();

               

                var n = myTFrame.parentStory.paragraphs.length;

           

                myParas[2].move(LocationOptions.AFTER, myParas[n-1]);

               

                app.findGrepPreferences = app.changeGrepPreferences = null;

                app.findGrepPreferences.findWhat = "\\r\\z";

                app.changeGrepPreferences.changeTo = "";

                myTFrame.changeGrep();

            }

    }

(^/)

cmoke73Author
Inspiring
October 21, 2016

Cool, it works!

It took a little to analyze your script. The precondition is to designate all the paragraphs with the style "red". So the headlne has to re-styled before you use that script. Isn´t it?

thank you!

Skemicle
Inspiring
October 20, 2016

Have you tried to use the move() function to set the location options at end?

cmoke73Author
Inspiring
October 20, 2016

No, I do not know that function or how to use it, yet.

Skemicle
Inspiring
October 20, 2016

You can get some information on it at: Adobe InDesign CS5 (7.0) Object Model JS: Paragraph​. If the second descriptive text paragraph is always the third paragraph, behind the Headline and first descriptive text; you can reference it as app.selection[0].paragraphs[2]