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?
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
- adding "\r" as the last character of the textFrame,
- moving paragraph[2] to the end of the textFrame,
- removing the last character from the textFrame.
That's all you need no styli
...Copy link to clipboard
Copied
Have you tried to use the move() function to set the location options at end?
Copy link to clipboard
Copied
No, I do not know that function or how to use it, yet.
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]
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.
Copy link to clipboard
Copied
quick question.. would each new headline be in the same textFrame or would it get a separate textFrame?
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.
Copy link to clipboard
Copied
As separate text frames, it could be quicker to simply move the third para after the last!
(^/)
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.
Copy link to clipboard
Copied
Skemicle is right!
Something like this:
Before:
After:
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;
}
(^/)
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:
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.
Copy link to clipboard
Copied
Hi Obi-wan.
Give me a moment to understand your responce...
Then I make my tries.
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?
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):
… 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();
}
}
(^/)
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!
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!
(^/)
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
- adding "\r" as the last character of the textFrame,
- moving paragraph[2] to the end of the textFrame,
- 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 .
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!
(^/)
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.
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)!
(^/)
Copy link to clipboard
Copied
Fixed! See: Code to be evaluated! [002]
(^/)
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...
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.
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)
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