Copy link to clipboard
Copied
Hi all,
Some issues with that code:
var myDoc = app.activeDocument;
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = "P1 - Blue";
myFound = app.findGrep();
for (i = 0; i < myFound.length; i++) {
myCRFormat = myDoc.crossReferenceFormats.item ('Paragraph Number 1');
myTargetParagraph = myFound;
myReference = myTargetParagraph.insertionPoints[-1];
Target = myDoc.paragraphDestinations.add (myTargetParagraph);
Ref = myDoc.crossReferenceSources.add (myReference, myCRFormat);
myDoc.hyperlinks.add (Ref, Target);
}
I would like to insert a CR at the end of all the paras with "P1 - Blue" para style applied!
As you see, It works but something weird happens: the CR is placed with "incrementation"!
Any deep though will be appreciated!
Thanks in advance!
Copy link to clipboard
Copied
You found your paragraphs, but then when you add CRs, you mess up the paragraphs: the indexes go all over the place. Probably iterating from end to start through the paragraphs will solve it:
for (var i < myFound.length-1; i >= 0; i--) {
Peter
Copy link to clipboard
Copied
Hi Peter,
I'm going to test this! Thanks!
"Bonus" question: How could I do to insert the CR at the beginning of the paras?
Best regards!
Copy link to clipboard
Copied
Maybe something like this:
myTargetParagraph.insertionPoints[0].contents = '\r';
P.
Copy link to clipboard
Copied
Hi Peter,
It doesn't work! … And you're right: The Cross-References insertion creates a "beautiful mess"!
... But I had an idea:
Since only the first insertion runs correctly, I "loop on a first insertion", that is to say, my script searches every occurrence of the "P1 - Blue" paragraph style [myFound_0 ], then it launches a new research, find: ^[^~j], which gives me "myFound_1" and the script only treats its first occurrence "myFound_1 [0]".
As it inserts a CR whose format is delimited by two non-joiners [ "Paragraph Number" format = ^j<paraNum />^>^8^>^j ], the new search (loop) won't return the first treated and will directly jump to the next, and so on! …
Really twisted [as me!] but it works! Surely a better and more "orthodox" solution!
[don't forget I'm a really [JS] newbie! ]
The only things to finally do is create:
• the "1 Invisible" char style [to make the CR invisible];
• the Cross-Reference format [to catch the "P1 - Blue" para style auto-numbering];
• the Header variable "P1 - Blue".
So, in 1 click, you can create complete auto-numbering headers! Cool!
var myDoc = app.activeDocument;
var myStyle = "P1 - Blue";
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.findWhat = "<FEFF>";
app.findTextPreferences.appliedParagraphStyle = myStyle;
app.changeTextPreferences.changeTo = "";
myDoc.changeText();
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "~j[^~j]+~j";
app.findGrepPreferences.appliedParagraphStyle = myStyle;
app.changeGrepPreferences.changeTo = "";
myDoc.changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = myStyle;
myFound_0 = app.findGrep();
for (i = 0; i < myFound_0.length; i++) {
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "^[^~j]";
app.findGrepPreferences.appliedParagraphStyle = myStyle;
myFound_1 = app.findGrep();
myTargetParagraph = myFound_1[0];
myCRFormat = myDoc.crossReferenceFormats.item ('Paragraph Number');
myReference = myTargetParagraph.insertionPoints[-1];
Target = myDoc.paragraphDestinations.add (myTargetParagraph);
Ref = myDoc.crossReferenceSources.add (myReference, myCRFormat);
myDoc.hyperlinks.add (Ref, Target);
}
Copy link to clipboard
Copied
I forgot! To "refresh": Ctrl-B
It's the shortcut I've assigned to this script:
app.activeDocument.spreads.everyItem().textFrames.everyItem().move(undefined, [5, 0]);
app.activeDocument.spreads.everyItem().textFrames.everyItem().move(undefined, [-5, 0]);
Cool!
Copy link to clipboard
Copied
Hi Obi
I was surprised to see that you marked a question you asked as answered when you so much hate to do so.
After seeing you marked your own answer as correct and not someone else I was not so surprised.
Peter answered your question (both of them) correctly. Show a bit of appreciation and mark his answer as correct.
I know you wrote you are twisted but it's not too late to change.
Regards
Trevor
Copy link to clipboard
Copied
Hi Trevor,
A little sorry if I've not marked as correct an answer you gave me by the past!
I don't think this situation is very important even if it's really helpful to do it!
About my decision to mark my answer as correct, it was more to alert you [the experts!]! I'm a [JS] newbie but I'm learning quickly! If there's a (surely) better way to code my idea, I'll totally appreciate comments, especially from you! 😉
About Peter, he totally knows how much I respect him and study all his comments with a great interest!
Copy link to clipboard
Copied
Michel, I read your commentary with head shake. To mark your own comments as correct, is no good forum practice!
Copy link to clipboard
Copied
… So! Is this better?
"Hi all,
Waiting for someone who could give you an answer that I eventually could mark as "correct", you could take a look at my post#4 where you could find something that could work! At your own risks! ;-)"
Copy link to clipboard
Copied
Bonjour Obi-wan Kenobi
Il y a un raccourci pour rafraichir l’écran ? ("I forgot! To "refresh": Ctrl-B")
Bonne année à vous.
Philippe