• 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 in a certain way

Engaged ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

1.8K

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

correct answers 1 Correct answer

Engaged , Oct 21, 2016 Oct 21, 2016

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 styli

...

Votes

Translate

Translate
Engaged ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

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

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

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

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

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]

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

thank you Skemicle.

Headline stays on top. The second descriptive text must always become the last paragraph.

Between those "mass-texts" must be the articles.

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

quick question.. would each new headline be in the same textFrame or would it get a separate textFrame?

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

For each article there is a separate text frame.

Each of those text frames contains (the order I want to have it after):

1. a headline

2. text #1

3. article number (color 1)

4. article number (color 2)

5. article number (color 3)

6. ...

x. text #2

Instead of different colors it could be different material, but the same article. That´s the reason why you never know how many article numbers you have to re-sort.

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
LEGEND ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

As separate text frames, it could be quicker to simply move the third para after the last! 

(^/)

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

I think so. the question ist: how?

I have to find a way to split them and move the last one at the end. But I don´t "speak" javascript that good.

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
LEGEND ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

Skemicle is right! 

Something like this:

Before:

Capture d’écran 2016-10-20 à 22.22.54.png

After:

Capture d’écran 2016-10-20 à 22.23.22.png

app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, 'Red/Green"');

 

function main()

var myDoc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = "(?s).+";

app.findGrepPreferences.appliedParagraphStyle = "Red";

myFound_red = myDoc.findGrep( );

app.findGrepPreferences = app.changeGrepPreferences = null; 

app.findGrepPreferences.findWhat = "(?s).+";

app.findGrepPreferences.appliedParagraphStyle = "Green";

myFound_Green = myDoc.findGrep( );

for (i = 0; i < myFound_red.length; i++) {   

myRed = myFound_red

myGreen = myFound_Green;

myRed.move (LocationOptions.BEFORE, myGreen); 

}

app.findGrepPreferences = app.changeGrepPreferences = null; 

}

(^/)

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

Obi-wan​,

I wasn't going to provide a working code until I saw an attempt made by cmoke​. Though I wasn't going for paragraph styles

I was thinking more along the lines of:

  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.

Yes, your script is better (if the paragraphs are formatted correctly with paragraph styles) and works all the time (multiple headlines in one textFrame), though i was just going to get cmoke started with something a bit simpler.

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

Hi Obi-wan.

Give me a moment to understand your responce...

Then I make my tries.

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

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?

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
LEGEND ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

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):

Capture d’écran 2016-10-21 à 02.59.07.png

Capture d’écran 2016-10-21 à 02.59.48.png

… 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();

            }

    }

(^/)

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 ,
Oct 20, 2016 Oct 20, 2016

Copy link to clipboard

Copied

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!

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
LEGEND ,
Oct 21, 2016 Oct 21, 2016

Copy link to clipboard

Copied

No! Here: "Blue_Para" style!

This's the Script target!

From this beginning, what is interesting is that you can" style" all your text inside the script!

(^/)

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 ,
Oct 21, 2016 Oct 21, 2016

Copy link to clipboard

Copied

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 .

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
LEGEND ,
Oct 21, 2016 Oct 21, 2016

Copy link to clipboard

Copied

Skemicle,

This code could be fine in some particular conditions!

1/ Selection

2/ if last para isn't a blank para

3/ if no other text frame to be treated

Not too realistic for my taste!

(^/)

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 ,
Oct 21, 2016 Oct 21, 2016

Copy link to clipboard

Copied

Hi Obi-wan!

I´m sure, when I get better in scripting, I will utilise your helpful script much more. But, now, it is much too complex. I would have to adapt some things in a way I cannot do now.

However I appreciate your help.

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
LEGEND ,
Oct 21, 2016 Oct 21, 2016

Copy link to clipboard

Copied

No annoyance for me! 

More important and more annoying! 

After simplifying my script according to Skemicle's excellent writing, I'm always feeling that something runs wrong [line 09 with the "if"]

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

var myTFrames = myDoc.textFrames;

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

    {

        var myTFrame = myTFrames;

       

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

            {

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

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

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

            }

    }

The difference between Skemicle's approach and mine is that I treat all the doc, not only a selection.

That's why I've tried to find a target ("Blue_Para" para style applied to the first para)!

(^/) 

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
LEGEND ,
Oct 22, 2016 Oct 22, 2016

Copy link to clipboard

Copied

LATEST

Fixed!  See:  Code to be evaluated! [002]

(^/) 

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 ,
Oct 21, 2016 Oct 21, 2016

Copy link to clipboard

Copied

Okay, this solution works better. And is short. Thank you for that!

Can you please tell me what happens in row 2 and row 4? What does that [-1] do?

"I just needed to see you attempt for yourself before i would provide this" ...and you was right!

I WANT to understand it. But the time is now too short for reading books and digging deep in the materia. I need the solution until monday. The books I have I read occasionally...

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 ,
Oct 21, 2016 Oct 21, 2016

Copy link to clipboard

Copied

line 2 should be changed to "sel.insertionPoints[-1].contents = "\r";" I realized when i went to explain that I messed up.

but [-1] says "The last item in the array" So then lines 2 and four say Set the last character in the text frame to "\r"(paragraph return) and "" (delete character) respectively.

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 ,
Oct 21, 2016 Oct 21, 2016

Copy link to clipboard

Copied

This ist how I understood the four lines in translation:

1. create a variable and fill it with the information about the selected item

2. the place you want to insert something should allways be the last character (a paragraph return)

3. pick up (from your selected item) the paragraph at the third position and move it at the end of ... (what?)

4. delete the last character in my selected item (the TextFrame)

Is this tolerably correct?

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 ,
Oct 22, 2016 Oct 22, 2016

Copy link to clipboard

Copied

Skemicle wrote:

… but [-1] says "The last item in the array"…

No. [-1] says the last item of a collection of items.

It's not an array. The last item of an array would be array[array.length-1].

[-1] notation would not work with arrays. The result would be "undefined".

Regards,
Uwe

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