Script to insert grep style in all Paragraph styles
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to insert grep style in all Paragraph styles
- This topic has 12 replies, 3 voices, and was last updated 12 years, 6 months ago by Kasyan Servetsky.
-
AuthorPosts
-
-
October 18, 2011 at 5:50 am #60802Mayoor ChhedaMember
Is it possible to construct a script to insert a single grep style (say color the Figure numbers in text) in all available Paragraph styles in a single step
-
October 18, 2011 at 11:58 am #60804Kasyan ServetskyMember
Yes, it is. Here's an example:
Main();
function Main() {
var doc = app.activeDocument,
parStyles = doc.allParagraphStyles,
charStyle = doc.characterStyles.item(“Figure numbers”),
parStyle, i, grepStyle;if (!charStyle.isValid) doc.characterStyles.add({name:”Figure numbers”, fontStyle:”Bold”});
for (i = 1; i < parStyles.length; i++) {
parStyle = parStyles[i];
grepStyle = parStyle.nestedGrepStyles.add();
grepStyle.grepExpression = “d+”;
grepStyle.appliedCharacterStyle = charStyle;
}
}The script adds a grep style to every paragraph style including styles in groups.
-
October 18, 2011 at 12:01 pm #60805Kasyan ServetskyMember
I forgot that the forum removes backslashes — add two backslashes before d+.
grepStyle.grepExpression = “\d+”;
-
October 18, 2011 at 1:17 pm #60807Matt MayerchakParticipant
Kasyan – thanks! I'll definitely try this one.
-
October 19, 2011 at 12:24 am #60813Mayoor ChhedaMember
Kasyan, thanks, but one query. Do I need to make changes to the script above so that it includes the name of the character style I want in the Grep style.
Also, can we construct a script similar to the above one which can change the font color of text in all the styles / style groups.
-
October 19, 2011 at 2:14 am #60815Kasyan ServetskyMember
What I posted is just an example of how to do this. I suppose that you want to write the script yourself and will modify the character style and the grep-expression according to your needs.
If you want someone to write a script for you, you have to be more specific: tell us what InDesign version you are on, what character style and grep-expression you want to apply, etc.
Yes, it's easy to make a script to change the font color of text in all the styles. What is the swatch name? Does it already exist in the document, or the script should create it if it doesn't exist yet?
-
October 19, 2011 at 5:24 am #60821Mayoor ChhedaMember
I work with Indesign CS4 on a Windows machine. I would like to change the font colour of the text (Figure No : *) wherever it appears in the text. I thought that the best way to do this would be by a Grep style. Am I right?. We can do this via Find/Change, but I wanted to insert a grep style in all my Paragraph styles (there are about 50 of them) so that the colour of the above text in Parantheses changes automatically whenever I make changes to the book.
Ragarding the second query, I have a few Paragraph styles where the text colour of the font is a swatch C60, M40, Y0, K0. I want to change this in all those Paragraph styles to Swatch C15, M100, Y100, K0 (which already exists in the document). Can we script it.
-
October 20, 2011 at 1:08 am #60833Kasyan ServetskyMember
Query 2:
Main();
function Main() {
var doc = app.activeDocument,
parStyles = doc.paragraphStyles,
swatch_1 = doc.swatches.item(“C=60 M=40 Y=0 K=0”),
swatch_2 = doc.swatches.item(“C=15 M=100 Y=100 K=0”),
i, parStyle;if (swatch_1.isValid && swatch_2.isValid) {
for (i = 0; i < parStyles.length; i++) {
parStyle = parStyles[i];
if (parStyle.fillColor == swatch_1) parStyle.fillColor = swatch_2;
}
}
else {
alert(“A swatch doesn't exist.”);
}
}//———————————————–
Query 1:
I still don't understand what you want to achieve. Scripting grep style is quite straightforward — as in the user interface, you have to set only two parameters: appliedCharacterStyle (the character style applied to the text) and grepExpression (the GREP expression used to apply automatic styling).
Probably, for the first parameter, you might want to use ^\d+ to apply the character style to any number of digits at the beginning of a line. I don't get what you mean by (Figure No : *) — a number followed by colon and some text?
First you have to create and test a grep-expression in InDesign to make sure that it works as expected. Then you can copy and paste it to the script and escape backslashes (double them in script).
Second parameter — character style:
grepStyle.appliedCharacterStyle = doc.characterStyles.item(“Type in your character style here”);
I used the following line in my example just to be on a safe side: if the character style doesn't exist in the document, create it, name it as “Figure numbers” and apply Bold to it. You can remove it if you're sure that the style is always present in the document, otherwise the script would throw an error.
if (!charStyle.isValid) doc.characterStyles.add({name:”Figure numbers”, fontStyle:”Bold”});
-
October 20, 2011 at 7:28 am #60848Mayoor ChhedaMember
Kasyan, I get an error when saving the above script as jsx – it says that the script contains Unicode information & should not be saved as ANSI.
Regarding Query 1 , the Figure numbers refer to the captions of placed graphics such as (Figure 2.1), (Figure 2.2) & so on
-
October 20, 2011 at 7:54 am #60852Kasyan ServetskyMember
Don't use Notepad to save scripts — use ExtendScript Toolkit instead.
Use this grep-expression in the script then:
grepStyle.grepExpression = “Figuresd+.d+”;
which means word 'Figure' followed by a space, one ore more digit, a period and one ore more digit.
-
October 20, 2011 at 7:57 am #60853Kasyan ServetskyMember
I forgot that the forum removes backslashes again.
Here's another attempt:
grepStyle.grepExpression = “Figure\s\d+\.\d+”;
-
October 20, 2011 at 10:49 am #60860Mayoor ChhedaMember
Kasyan, In Script for Query 2 (for text color), I get an Undetermined string constant in lines 5 & 6 for Swatch_1 & Swatch_2 in ExtendScript Toolkit & the script throws an error on running
-
October 20, 2011 at 4:05 pm #60864Kasyan ServetskyMember
The forum software spoils the code: before the closing parentheses it replaced inch marks with quotes. If you change them back to parentheses, it should work. Here I posted the script to avoid problems with coping and pasting to/from the forum.
-
-
AuthorPosts
- You must be logged in to reply to this topic.