• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Apply Conditions to para/char styles with a "for" loop …

LEGEND ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

Hi all,

That's the end of the script:

scores = the_document.findGrep()

for (i = 1; i < scores.length; i >= i++) {   

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.appliedParagraphStyle = "para_AutoStyle" + (i);

app.changeToGrepPreferences.appliedConditions = ["para_AutoStyle" + (i)]; 

the_document.changeGrep();

}

for (i = 1; i < scores.length; i >= i++) {   

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.appliedParagraphStyle = "char_AutoStyle" + (i);

app.changeToGrepPreferences.appliedConditions = ["char_AutoStyle" + (i)]; 

the_document.changeGrep();

}

app.findGrepPreferences = app.changeGrepPreferences = null;

No problem with the creation of:

para_AutoStyleX para style;

char_AutoStyleX char style;

para/char_AutoStyleX condition associated.

What I want to do is apply:

"para_AutoStyle1" condition to "para_AutoStyle1" para style;

"para_AutoStyle2" condition to "para_AutoStyle2" para style;

"para_AutoStyle3" condition to "para_AutoStyle3" para style;

"char_AutoStyle1" condition to "char_AutoStyle1" para style;

"char_AutoStyle2" condition to "char_AutoStyle2" para style;

"char_AutoStyle3" condition to "char_AutoStyle3" para style;

Capture d’écran 2016-06-05 à 14.17.07.png

Missing something!! …

Thanks for help! 

TOPICS
Scripting

Views

830

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

see comments in code:

//use var to declar all variables!!

var scores = the_document.findGrep(true); //always always iterate from the back

//please review "for" syntax on:

//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for

for (var i = 0; i < scores.length; i++) {

  //final note, it's always a good idea NOT to use Strings for appling paragraph styles

  //instead use indesign objects

  var pStyle=the_document.paragraphStyles.item("para_AutoStyle" + i);

  if (!pStyle.isValid){

  continue;

  }

  scores.appliedParagraphStyle = pStyle;

  scores.appliedConditions = [("para_AutoStyle" + i)];

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

Hi Vlad,

Thanks for your answer!

It seems find to me but I can't make it work! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

Hi

What's the details of the findWhat of the findGrep?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

Hi Trevor,

The script has created a set of "n" para styles called "para_AutoStyle1", "para_AutoStyle2", … "para_AutoStylen", with a set of "n" conditions with the same name. Idem about the "m" created char styles: "char_AutoStyle1", "char_AutoStyle2", … "char_AutoStylem", with a set of "m" conditions called with the same name.

What I want to do in the last part of the script is to apply these conditions respectively to the para/char with these styles applied in the doc!

So, the script searches all paras with "para_AutoStyle1", "para_AutoStyle2", … "para_AutoStylen" and applies respectively the conditions "para_AutoStyle1", "para_AutoStyle2", … "para_AutoStylen", then searches all the text with "char_AutoStyle1", "char_AutoStyle2", … "char_AutoStylem" and applies respectively the conditions "char_AutoStyle1", "char_AutoStyle2", … "char_AutoStylem".

Maybe I'm wrong! would it be better to apply the conditions before?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

app.findGrepPreferences.findWhat = "????????????????";

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

Find format: "para_AutoStyle1" para style and change to format "para_AutoStyle1" condition;

Find format: "para_AutoStyle2" para style and change to format "para_AutoStyle2" condition;

Find format: "para_AutoStylen" para style and change to format "para_AutoStylen" condition;

Find format: "char_AutoStyle1" para style and change to format "char_AutoStyle1" condition;

Find format: "char_AutoStyle2" para style and change to format "char_AutoStyle2" condition;

Find format: "char_AutoStylem" para style and change to format "char_AutoStylem" condition;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

Oh dear, oh là là mon ami!

From your opening question it was clear what you want to do.

My question is you have the line scores = the_document.findGrep()

The grep must have been set somehow either by manually inserting it in the find grep panel or by scripting.

Clear so far?

If by scripting you would have used something like app.findGrepPreferences.findWhat = "????????????????";

But your would have had all those question marks between the quotes, you'd have something else right? What?

If you did some manual work on the find grep panel what did you type in by the find option?

Regards

Trevor

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

Trevor (and Vlad),

I think I would not have written this line:

  1. scores = the_document.findGrep() 

I've no concerned grep find/replace before this line! [not for this point!]

Maybe this has misled you!

My question is in fact: can I use a loop "for" to search format as "para_AutoStyle1", "para_AutoStyle2", "para_AutoStyle3", … "para_AutoStylen" and change format to the found text applying a condition with the same name?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

var doc, paraStyles, charStyles, numberOfParagraphStyles, numberOfCharacterStyles, autoStyle,

    l, n, basePaga, baseChar, regXP, regXC, style, autoStyles, autoCondition, autoConditions;

doc = app.activeDocument;

basePaga = 'para_AutoStyle';

baseChar = 'char_AutoStyle';

paraStyles = doc.paragraphStyles;

charStyles = doc.characterStyles;

regXP = /^para_AutoStyle\d+$/;

regXC = /^char_AutoStyle\d+$/;

autoConditions = [];

autoStyles = [];

numberOfParagraphStyles = paraStyles.length;

numberOfCharacterStyles = charStyles.length;

while (numberOfParagraphStyles--) {

    style = paraStyles[numberOfParagraphStyles];

    // check if the styles is a desired style

    autoStyle = style.name.match(regXP);

    if (autoStyle) { autoStyle = autoStyle[0]; } else {

        continue; }

    autoCondition = doc.conditions.itemByName(autoStyle);

    if (!autoCondition.isValid) {

        continue; }

    autoStyles.push(style);

    autoConditions.push(autoCondition);

}

l = autoStyles.length;

app.findGrepPreferences = app.changeGrepPreferences = null;

for (n = 0; n < l; n++) {

    app.findGrepPreferences.appliedParagraphStyle = autoStyles;

    app.changeGrepPreferences.appliedConditions = [autoConditions];

    doc.changeGrep();

}

// Do the same for the char Styles !!!!!

// while / push / for

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 05, 2016 Jun 05, 2016

Copy link to clipboard

Copied

P.s. don't forget to set the ChangeConditionsMode to acording to whether you want to add or replace the existing conditions.

Probably before the paragraph styles loop you would want:

app.changeGrepPreferences.changeConditionsMode = ChangeConditionsModes.REPLACE_WITH;

and before the character styles loop:

app.changeGrepPreferences.changeConditionsMode = ChangeConditionsModes.ADD_TO;

HTH

Trevor

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 06, 2016 Jun 06, 2016

Copy link to clipboard

Copied

Ok this is a more generic implementation for greping any properties with a shared name.

I used a while instead of a for loop but you can still mark the answer as correct.

// https://forums.adobe.com/message/8793520#8793520

// A Generic method of applying changeGrep to mapped properties of items with a shared name

// By Trevor http://www.creative-scripts.com Custom Scripts

/**

* [mapProperties description]  Maps items of the same name in 2 valid DOM collections

* @param  String   collection_1_Name  The 1st property to map for example paragraphStyles or characterStyles etc.

* @param  String   collection_2_Name  The 2nd property to map for example conditions, fillColor etc.

* @param  Regex    regX        A regex to filter the collections for example /^para_AutoStyle\d+$/;

* @return Object               {prop_1: filteredCollection1, prop_2: filteredCollection2};

*                                       filteredCollection1 is an Array of the the 1st collection items whose name matches the regX

*                                       filteredCollection2 is an Array of the the 2nd collection items

*                                                           whose names are the same of the items in filteredCollection1

*/

function mapProperties(collection_1_Name, collection_2_Name, regX){

    var collection_1, filteredCollection1, filteredCollection2, l, doc, prop_1, prop_2, name;

    doc = app.properties.activeDocument;

    if (!doc) {

        exit();

    }

    collection_1 = doc[collection_1_Name];

    l = collection_1.length;

    filteredCollection1 = [];

    filteredCollection2 = [];

    while(l--) {

        prop_1 = collection_1;

        name = prop_1.name.match(regX);

        if (!name) {continue;}

        name = name[0];

        prop_2 = doc[collection_2_Name].itemByName(name);

        if (!prop_2.isValid) {continue;}

        filteredCollection1.push(prop_1);

        filteredCollection2.push(prop_2);

    }

    return {prop_1: filteredCollection1, prop_2: filteredCollection2};

}

/**

* [grepMapped description]

* @param  String findProperty   name of the findGrepPreferences property

* @param  String changeProperty name of the changeGrepPreferences property

* @param  Object map            {prop_1 , prop_2};

*                                prop_1 is an Array of the the find collection items

*                                prop_2 is an Array of the the change collection items

*                                                           whose names are the same of the items in prop_1

* @param  Boolean findIsArray    If the mapped property needs to be in an array form like in the case of conditions

* @param  Boolean changeIsArray  If the mapped property needs to be in an array form like in the case of conditions

* @return void

*/

function grepMapped(findProperty, changeProperty, map, findIsArray, changeIsArray){

    var l, p1, p2, doc;

    doc = app.activeDocument;

    l = map.prop_1.length;

    while(l--) { // figure out for your self how to use a for loop instead of a while

        p1 = (findIsArray)? [map.prop_1] : map.prop_1;

        p2 = (changeIsArray)? [map.prop_2] : map.prop_2;

        app.findGrepPreferences[findProperty] = p1;

        app.changeGrepPreferences[changeProperty] = p2;

        try { // might get into trouble if there's no finds

            doc.changeGrep();

        } catch (e) {}

    }

}

function zapEm(){

    var paraCondsMap, charCondsMap;

    paraCondsMap = mapProperties('paragraphStyles', 'conditions', /^para_AutoStyle\d+$/);

    charCondsMap = mapProperties('characterStyles', 'conditions', /^char_AutoStyle\d+$/);

    app.findGrepPreferences = app.changeGrepPreferences = null;

    app.changeGrepPreferences.changeConditionsMode = ChangeConditionsModes.REPLACE_WITH;

    grepMapped('appliedParagraphStyle', 'appliedConditions', paraCondsMap, false, true);

    app.findGrepPreferences = app.changeGrepPreferences = null;

    app.changeGrepPreferences.changeConditionsMode = ChangeConditionsModes.ADD_TO;

    grepMapped('appliedCharacterStyle', 'appliedConditions', charCondsMap, false, true);

}

app.doScript(zapEm, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT,'Map Styles');

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 06, 2016 Jun 06, 2016

Copy link to clipboard

Copied

Hi Trevor,

Thanks a lot for the time you've spent on my question!

I've not tested your codes yet! [I'm living in France!] Tonight! I'll promise I'll post comments asap! [great for my own learning!]

Thanks again!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 09, 2016 Jun 09, 2016

Copy link to clipboard

Copied

LATEST

Ok you might be living in France (not that I see the connection) but you can still mark the answer as correct and also the answer over here Find the first line of a table on each page when the table runs on several pages…

Ta very much,

Trevor

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines