Copy link to clipboard
Copied
Hello everyone!
I want apply my custom Paragraph Styles to some text, this text have a similar Styles but not the same.
Is possible create many conditionals and apply my Custom Paragraphs Styles depending on Text Values?
For example: in my file Indesign.indd I want find the texts with the next values and apply my Custom Paragraph Style called Text Number locate in my folder NEW STYLES.
fontStyle == "Roman",
pointSize <= 10.5,
spaceAfter <=1,
spaceBefore <= 3.292,
leftIndent <= 7.408,
rightIndent <= 0,
firstLineIndent <= -7.408,
hyphenation: false,
justification == Justification.LEFT_ALIGN || justification == Justification.LEFT
Please this script is very important for me, I appreciate any help.
Thanks so much!
Hi Loic, is working, you help me a lot really deep thank you!
...// The Script apply paragraph styles based in the text options. For example if your text are in bold, the script will apply the Paragraph Quotation Bold.
// It is a easy way to typeset long documents with your custom styles.
// Files to download: https://github.com/firedevelop/id0000013-Adobe-InDesign-Scripts-Examples/tree/master/id0000074-Apply-Paragraph-Styles-Depending-on-Text-Values
// Thanks to the author: Loic.Aigo
Copy link to clipboard
Copied
As far as I know you won't be able to set logical values. But you could still look at some base set or properties and loop through results to see if they have any of the multiple possible values.
Copy link to clipboard
Copied
Hi Loic,
Do you have any code o example, just for start from scratch? sorry, I am beginner.
I search a lot in Google but I can't find examples.
Thanks!
Copy link to clipboard
Copied
There are probably many similar samples inside the forum.
As it seems you only have one static value (i.e. hyphenation), you could start this way:
//MAIN FUNCTION
var main = function() {
//VARS
var doc = app.properties.activeDocument,
fgp = app.findGrepPreferences.properties,
texts = [], n = 0, nthText, myParaStyle,
paraStyleName = "some para style name";
//Exit if no documents open
if ( !doc ) {
alert("You need an open document" );
return;
}
//Create ParaStyle if needed. Otherwise use it
myParaStyle = doc.paragraphStyles.item ( paraStyleName );
if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );
//Setting grep find preferences
app.findGrepPreferences.properties = {
allowArbitraryHyphenation:true,
findWhat:".+",
}
//Finding texts with static properties
texts = doc.findGrep();
n = texts.length;
//Looping through found texts to inspect further properties with values ranges.
while ( n-- ) {
nthText = texts
; if ( nthText.pointSize <= 10.5
// && nthText "other property"…
// && nthText "other property"…
// Now it's your turn to do the dirty work 😉
//…
) {
//Applying style
nthText.appliedParagraphStyle = myParaStyle;
}
}
//reverting UI values in the F/C panel
app.findGrepPreferences.properties = fgp;
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
HTH
Loic
Copy link to clipboard
Copied
Hi Loic!
I start to with your code. Look my file is very simple. But when I run the script the style Text Old didn't change to the style Text New
At the moment, I only modify this line:
paraStyleName = "Text New";
Thanks so much!
Copy link to clipboard
Copied
Hi,
Some other settings may corrupt the lookup process. Let's try resetting any existing props:
//MAIN FUNCTION
var main = function() {
//VARS
var doc = app.properties.activeDocument,
fgp = app.findGrepPreferences.properties,
texts = [], n = 0, nthText, myParaStyle,
paraStyleName = "some para style name";
//Exit if no documents open
if ( !doc ) {
alert("You need an open document" );
return;
}
//Create ParaStyle if needed. Otherwise use it
myParaStyle = doc.paragraphStyles.item ( paraStyleName );
if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );
//Setting grep find preferences
app.findGrepPreferences = null;
app.findGrepPreferences.properties = {
allowArbitraryHyphenation:true,
findWhat:".+",
}
//Finding texts with static properties
texts = doc.findGrep();
n = texts.length;
//Looping through found texts to inspect further properties with values ranges.
while ( n-- ) {
nthText = texts
; if ( nthText.pointSize <= 10.5
// && nthText "other property"…
// && nthText "other property"…
// Now it's your turn to do the dirty work 😉
//…
) {
//Applying style
nthText.appliedParagraphStyle = myParaStyle;
}
}
//reverting UI values in the F/C panel
app.findGrepPreferences.properties = fgp;
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Copy link to clipboard
Copied
Hi Loic!
the code you added haven't any changes. I compare both and are the same, is not working
thanks you!
Copy link to clipboard
Copied
You may have missed line #22:
app.findGrepPreferences = null;
The fact the code doesn't work may imply some F/C settings exclude the text from match. Is the hyphenation turned off on your text. Send your file if needed.
Copy link to clipboard
Copied
Hi Loic!
thanks por the line. But the script don't do nothing.
Please look, this is my styles:
And I add my custom style in the line #8:
paraStyleName = "Text New";
I use Adobe Indesign CC 2018.
Here are my indesign File: Download
Thanks for your patience!
Copy link to clipboard
Copied
My bad, here is the fix :
//MAIN FUNCTION
var main = function() {
//VARS
var doc = app.properties.activeDocument,
fgp = app.findGrepPreferences.properties,
texts = [], n = 0, nthText, myParaStyle,
paraStyleName = "Text New";
//Exit if no documents open
if ( !doc ) {
alert("You need an open document" );
return;
}
//Create ParaStyle if needed. Otherwise use it
myParaStyle = doc.paragraphStyles.item ( paraStyleName );
if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );
//Setting grep find preferences
app.findGrepPreferences = null;
app.findGrepPreferences.properties = {
findWhat:".+",
}
//Finding texts with static properties
texts = doc.findGrep();
n = texts.length;
//Looping through found texts to inspect further properties with values ranges.
while ( n-- ) {
nthText = texts
; if ( !nthText.hyphenation
&&
nthText.pointSize <= 10.5
// && nthText "other property"…
// && nthText "other property"…
// Now it's your turn to do the dirty work 😉
//…
) {
//Applying style
nthText.appliedParagraphStyle = myParaStyle;
}
}
//reverting UI values in the F/C panel
app.findGrepPreferences.properties = fgp;
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Copy link to clipboard
Copied
Hi Loic,
I am sorry, I try several times your new code, but still not working.
Thank you!
Copy link to clipboard
Copied
At this point, it doesn't make much sense. I tried to download your file but it went corrupted. May you send it as idml ?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Just turn hyphenation off in your sample and the scripts works fine as it's a requirement…
if ( !nthText.hyphenation…
Copy link to clipboard
Copied
Hi Loic,
amazing! the script start to work. Now I try to add more styles, but I get a error. Is correct my new code?
I added the lines #2 and #4:
texts = [], n = 0, nthText, myParaStyle,
texts2 = [], n = 0, nthText2, myParaStyle2,
paraStyleName = "Text New";
paraStyleName2 = "Quotation New";
also line #23:
myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );
if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 );
also line #54:
if ( !nthText2.hyphenation
&& nthText2.pointSize <= 10.5
&& fontStyle == "Italic"
// && nthText "other property"…
// Now it's your turn to do the dirty work
//…
) {
//Applying style
nthText2.appliedParagraphStyle = myParaStyle2;
}
FULL CODE:
//MAIN FUNCTION
var main = function() {
//VARS
var doc = app.properties.activeDocument,
fgp = app.findGrepPreferences.properties,
texts = [], n = 0, nthText, myParaStyle,
texts2 = [], n = 0, nthText2, myParaStyle2,
paraStyleName = "Text New";
paraStyleName2 = "Quotation New";
//Exit if no documents open
if ( !doc ) {
alert("You need an open document" );
return;
}
//Create ParaStyle if needed. Otherwise use it
myParaStyle = doc.paragraphStyles.item ( paraStyleName );
if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );
myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );
if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 );
//Setting grep find preferences
app.findGrepPreferences = null;
app.findGrepPreferences.properties = {
findWhat:".+",
}
//Finding texts with static properties
texts = doc.findGrep();
n = texts.length;
//Looping through found texts to inspect further properties with values ranges.
while ( n-- ) {
nthText = texts
; nthText2 = texts2
;
if ( !nthText.hyphenation
&& nthText.pointSize <= 10.5
// && nthText "other property"…
// && nthText "other property"…
// Now it's your turn to do the dirty work
//…
) {
//Applying style
nthText.appliedParagraphStyle = myParaStyle;
}
if ( !nthText2.hyphenation
&& nthText2.pointSize <= 10.5
&& fontStyle == "Italic"
// && nthText "other property"…
// Now it's your turn to do the dirty work
//…
) {
//Applying style
nthText2.appliedParagraphStyle = myParaStyle2;
}
}
//reverting UI values in the F/C panel
app.findGrepPreferences.properties = fgp;
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
This is my new styles, I upload my indesign files.
Look:
Thank you!
Copy link to clipboard
Copied
//MAIN FUNCTION
var main = function() {
//VARS
var doc = app.properties.activeDocument,
fgp = app.findGrepPreferences.properties,
texts = [], n = 0, nthText, myParaStyle,
texts2 = [], n = 0, nthText2, myParaStyle2,
paraStyleName = "Text New";
paraStyleName2 = "Quotation New";
//Exit if no documents open
if ( !doc ) {
alert("You need an open document" );
return;
}
//Create ParaStyle if needed. Otherwise use it
myParaStyle = doc.paragraphStyles.item ( paraStyleName );
if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );
myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );
if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 );
//Setting grep find preferences
app.findGrepPreferences = null;
app.findGrepPreferences.properties = {
findWhat:".+",
}
//Finding texts with static properties
texts = doc.findGrep();
n = texts.length;
//Looping through found texts to inspect further properties with values ranges.
while ( n-- ) {
nthText = texts
;
if ( !nthText.hyphenation
&& nthText.pointSize <= 10.5
// && nthText "other property"…
// && nthText "other property"…
// Now it's your turn to do the dirty work
//…
) {
if ( nthText.fontStyle == "Italic" ) {
nthText.appliedParagraphStyle = myParaStyle2;
}
else {
nthText.appliedParagraphStyle = myParaStyle;
}
//Applying style
}
}
//reverting UI values in the F/C panel
app.findGrepPreferences.properties = fgp;
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Copy link to clipboard
Copied
Hi Loic!
I am very happy because your script is working
Now I start to test deeply, and I find some obstacle, please can you help me?
Like you can see my Styles to apply are inside a folders:
To fix this, I did this:
paraStyleName = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName("Text New");
paraStyleName2 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName("Quotation New");
Why is not working?
Thanks!
Copy link to clipboard
Copied
It's likely because script paraStyleName variable expects a string so it's used to target a specific style. In your case, you are passing a script reference so later on, the script tries to set a reference to a paragraph style based on a string which is actually a paragraph style instance.
You need to rewrap the script so instead of passing a string, you can pass the style itself:
var paraStyle = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName("Text New");
Then
nthText.appliedParagraphStyle = paraStyle;
Copy link to clipboard
Copied
Hi Loic, is working, you help me a lot really deep thank you!
// The Script apply paragraph styles based in the text options. For example if your text are in bold, the script will apply the Paragraph Quotation Bold.
// It is a easy way to typeset long documents with your custom styles.
// Files to download: https://github.com/firedevelop/id0000013-Adobe-InDesign-Scripts-Examples/tree/master/id0000074-Apply...
// Thanks to the author: Loic.Aigon from Adobe Forums
//MAIN FUNCTION
var main = function() {
//VARS
var doc = app.properties.activeDocument,
fgp = app.findGrepPreferences.properties,
texts = [], n = 0, nthText, myParaStyle,
texts2 = [], n = 0, nthText2, myParaStyle2,
texts3 = [], n = 0, nthText3, myParaStyle3,
texts4 = [], n = 0, nthText4, myParaStyle4,
paraStyleName = "Text New";
paraStyleName2 = "Text New 12";
paraStyleName3 = "Quotation New Bold";
paraStyleName4 = "Quotation New Italic";
//Exit if no documents open
if ( !doc ) {
alert("You need an open document" );
return;
}
//Create ParaStyle if needed. Otherwise use it
var paraStyle = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName(paraStyleName);
myParaStyle = doc.paragraphStyles.item ( paraStyleName );
// if ( !myParaStyle.isValid ) myParaStyle = doc.paragraphStyles.add ( paraStyleName );
var paraStyle2 = doc.paragraphStyleGroups.itemByName("TEXT NEW").paragraphStyles.itemByName(paraStyleName2);
myParaStyle2 = doc.paragraphStyles.item ( paraStyleName2 );
// if ( !myParaStyle2.isValid ) myParaStyle2 = doc.paragraphStyles.add ( paraStyleName2 );
var paraStyle3 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName(paraStyleName3);
myParaStyle3 = doc.paragraphStyles.item ( paraStyleName3 );
// if ( !myParaStyle3.isValid ) myParaStyle3 = doc.paragraphStyles.add ( paraStyleName3 );
var paraStyle4 = doc.paragraphStyleGroups.itemByName("QUOTATION NEW").paragraphStyles.itemByName(paraStyleName4);
myParaStyle4 = doc.paragraphStyles.item ( paraStyleName4 );
// if ( !myParaStyle4.isValid ) myParaStyle4 = doc.paragraphStyles.add ( paraStyleName4 );
//Setting grep find preferences
app.findGrepPreferences = null;
app.findGrepPreferences.properties = {
findWhat:".+",
}
//Finding texts with static properties
texts = doc.findGrep();
n = texts.length;
//Looping through found texts to inspect further properties with values ranges.
while ( n-- ) {nthText = texts
; if (nthText.fontStyle =="Roman" && nthText.pointSize <= 10.5 ){nthText.appliedParagraphStyle = paraStyle;}
if (nthText.pointSize == 12){nthText.appliedParagraphStyle = paraStyle2;}
if (nthText.fontStyle == "Bold"){nthText.appliedParagraphStyle = paraStyle3;}
if (nthText.fontStyle == "Italic") {nthText.appliedParagraphStyle = paraStyle4;}
}
//reverting UI values in the F/C panel
app.findGrepPreferences.properties = fgp;
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Some images about the script: