-
1. Re: Apply character styling to part of paragraph
Jump_Over Oct 23, 2013 3:30 PM (in response to Dan-BTP)Hi,
1st at all - you can not use "+=" operand since your insertionPoints[-1].contents is probably undefined initially. This will return an error "undefined is not an object". You can use "=" or "story.contents += "
2nd - you are able to apply charStyle to any text object
(not to a string which is a text's object contents ==> separate text property in fact)
So you can name/match somehow this part of text which should be a target.
But the easiest way is to apply desired charStyle to story.insertionPoints[-1] before adding contents.
This object properties will be parent to all new one created while adding.
So let assume you have a specific charStyle named mCharStyle
and mBasic = app.activeDocument.characterStyles.item(0) // "None";
Your code could look like:
story.insertionPoints[-1].contents = pgfStr; story.insertionPoints[-1].contents = "\t"; story.insertionPoints[-1].applyCharacterStyle(mCharStyle); story.insertionPoints[-1].contents = refStr; story.insertionPoints[-1].applyCharacterStyle(mBasic); // back to "None" story.insertionPoints[-1].contents = "\r";
Jarek
-
2. Re: Apply character styling to part of paragraph
Dan-BTP Oct 23, 2013 7:48 PM (in response to Jump_Over)Thank you! That is definitely a big help. Now I am having trouble applying the paragraph style at the beginning so that the character style for the last part of the text overrides the paragraph style. Here is my code:
if (currentPgfType == "verse") {
pgfStyleName = "Verse";
}
else {
pgfStyleName = "Poem";
}
pgfPointSize = intPointSize/10.0;
pgfLeading = pgfPointSize + lineSpacing;
messagePgfStyle = new ParagraphStyle();
messagePgfStyle.name = pgfStyleName;
messagePgfStyle.leading = pgfLeading + "pt";
messagePgfStyle.pointSize = pgfPointSize + "pt";
refCharStyle = new CharacterStyle();
refCharStyle.pointSize = (pgfPointSize - 1.5) + "pt";
story.insertionPoints[-1].applyParagraphStyle(messagePgfStyle);
story.insertionPoints[-1].contents = pgfStr;
if (currentPgfType == "verse") {
story.insertionPoints[-1].contents = "\t";
story.insertionPoints[-1].applyCharacterStyle(refCharStyle);
story.insertionPoints[-1].contents = refStr;
story.insertionPoints[-1].applyCharacterStyle(basicCharStyle);
}
story.insertionPoints[-1].contents = "\r";
It tells me that messagePgfStyle on the applyParagraphStyle statement is nothing. What am I doing wrong in defining and assigning the paragraph style?
Thanks again for your help!
Dan
-
3. Re: Apply character styling to part of paragraph
Jump_Over Oct 24, 2013 2:08 AM (in response to Dan-BTP)Hi,
Hmm...,
It looks like a part of code (some variables not defined)
Script creates a new para and charStyle but as "virtual" objects. If the goal is to apply them to ID document texts ==> use (i.e) :
messagePgfStyle = app.activeDocument.paragraphStyles.add(messagePgfStyle);
But why to do it?
Your doc has styles "Poem" and "Verse" defined already, right?
If your goal is to apply one of them to the last paragraph of story ==> do it after if...else statement.
story.paragraphs[-1].applyParagraphStyle(app.activeDocument.paragraphStyles.item(pgfStyleName));
If your goal is to modify last para properties after that (style definition stay untouched) ==> do it assigning new values to last para of story (not to a style):
story.paragraphs[-1].leading = pgfPointSize + lineSpacing;
In case of modifying contents at the end ==> char style should be added to your doc (i.e.):
refCharStyle = app.activeDocument.characterStyles.add({ name: "refCharStyle", pointSize: (pgfPointSize - 1.5) + "pt" });Jarek
-
4. Re: Apply character styling to part of paragraph
Dan-BTP Oct 24, 2013 5:15 AM (in response to Jump_Over)Thank you for your response.
My InDesign document has 365 pages. In my script, I am creating each of these pages and adding one text frame to each page. In the story for each text frame, I am adding paragraphs of text that I am reading from text files.
I have Verse and Poem paragraph styles defined, which I want to apply to the paragraphs on each page. However, the point size and leading can be different on every page. So those two properties I have to override for each paragraph on each page.
Furthermore, within each of the Verse paragraphs, there is some text that I call a reference. The point size of the reference is 1.5 pt. smaller than the point size of the paragraph, whatever that is. So I can't apply the reference point size as a character style, since it too changes on each page.
I hope I am describing my situation clearly enough so that you can help me fix my script to accomplish what I need. Thank you!
-
5. Re: Apply character styling to part of paragraph
Jump_Over Oct 24, 2013 6:08 AM (in response to Dan-BTP)Hi,
Dan-BTP wrote:
... the point size and leading can be different on every page...
Are you able to find any rule of these differences?
...
what to do depends on what kind of rule could be defined
...
The point size of the reference is 1.5 pt. smaller than the point size of the paragraph, whatever that is.
Near to the end of script you can code for example:
// Your target is "reference" which is a text inbetween tabulator and end of story (or other criteria). // So you could define: app.findGrepPreferences = null; // just for clear it app.findGrepPreferences.findWhat = "(?<=\\t).+\\s\\Z"; // to catch text following tab in last para of story // and after: mFound = app.activeDocument.findGrep(); // so all desired text objects are in an array "mFound". // you could iterate (backward) through it: len = mFound.length; while (len-->0) // ">0" in case of nothing found mFound[len].pointSize -= 1.5; // whatever that is - is 1.5 smaller
Jarek
-
6. Re: Apply character styling to part of paragraph
Dan-BTP Oct 24, 2013 6:57 AM (in response to Jump_Over)Thank you! Using findGrep was a technique that never crossed my mind but worked well!

