0

I have a script that replaces all the content in Indesign that i have tagged with labels in table cells which works great. The only thing lacking is when i give 2 cells the same label, the script only replaces the content of the first label it finds. So i want the script to replace every content that has the same label. Does anyone have an idea on how to achieve this?

var main = function() {
    var doc = app.properties.activeDocument,
    root, xe, price,
    tags = <tags>

<PVDF8HNAPK40>100</PVDF8HNAPK40>
<PVDF8HNAPK50>100</PVDF8HNAPK50>
<PVDF8HNAPK63>100</PVDF8HNAPK63>
<PVDF8HNAPK75>100</PVDF8HNAPK75>

</tags>, tag, xes;


    if ( !doc ) return;

    root = doc.xmlElements[0];

    tags = tags.children(), n = tags.length();

    while ( n-- ) {
        tag = tags[n];
        xes = root.evaluateXPathExpression ( ".//"+ String(tag.name()) );
        if ( xes.length ) {
            xe = xes[0];
            xe.contents = String(tag).replace (/\./g, "," );

        }
    }
}


var u;

app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Christophe
  • 13
  • 6

1 Answers1

0

It looks as if all you need is just another loop. To make things even, I've replaced the outer while loop by a for loop as well.

var main = function() {
    var doc = app.properties.activeDocument,
    root, xe, price, tag, xes,
    tags = <tags>
        <PVDF8HNAPK40>100</PVDF8HNAPK40>
        <PVDF8HNAPK50>100</PVDF8HNAPK50>
        <PVDF8HNAPK63>100</PVDF8HNAPK63>
        <PVDF8HNAPK75>100</PVDF8HNAPK75>
    </tags>;

    if ( !doc ) return;

    root = doc.xmlElements[0];
    tags = tags.children();

    for (var t = 0; t < tags.length(); t++) {
        tag = tags[t];
        xes = root.evaluateXPathExpression ( ".//"+ String(tag.name()) );
        for (var i = 0; i < xes.length; i++) {
            xe = xes[i];
            xe.contents = String(tag).replace (/\./g, "," );
        }
    }
}

var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );

Disclaimer: I do not have InDesign. This is untested code.

Tomalak
  • 332,285
  • 67
  • 532
  • 628