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

Export All Conditional Text to a Text file

Community Beginner ,
May 31, 2018 May 31, 2018

Copy link to clipboard

Copied

I am trying to write a script that using find Grep to export all the conditional text, but I have a problem.

I can only find the text with one condition applied, but not the text with two or more conditions applied

Could someone please point me in the right direction?

    

    myDoc = app.activeDocument;

    var Report = new File ( myFiles.parent+"/"+myFiles.name.replace ( /\.indd$/i, ".txt" ) )

    Report.open( "w" );

    var myConditions = myDoc.conditions;

    myConditions.everyItem().visible = false;

    l = myConditions.length;

    //cycle thru conditions

    while(l--){

        myCondition =     myConditions;

        //set actual condition visible to true

        myCondition.visible = true;

        myConditionName = myCondition.name;

    app.findGrepPreferences = NothingEnum.nothing;

    app.findChangeGrepOptions.includeMasterPages = true;

        app.findGrepPreferences.appliedConditions=[myCondition];

        var myFind = myDoc.findGrep();

        Report.writeln(myConditionName)

        for(var a=myFind.length-1; a>=0; a--){

            Report.writeln(myFind.contents+"\r");

            }

        Report.writeln("\r\r");

        app.findGrepPreferences = NothingEnum.nothing;

        }

    myCondition.visible = false;

TOPICS
Scripting

Views

1.4K

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

correct answers 1 Correct answer

Community Expert , Jun 05, 2018 Jun 05, 2018

Hi,

appliedConditions is a property of object Text and therefore a property of textStyleRange.

Further: The value of appliedConditions is an Array.

A textStyleRange is a range of text with mutual formatting.

So you could go through all textStyleRanges of a Story and look for appliedConditions.

The result will tell you what conditions are applied if any. You could even add the textStyleRanges to the result.

Let's test that with a story at hand. A simple text frame where several conditions are applied.

...

Votes

Translate

Translate
Engaged ,
Jun 04, 2018 Jun 04, 2018

Copy link to clipboard

Copied

Hi VWu,

Please replace the second line.

var Report = new File("~/Desktop/Conditinal_Report.txt" );

Thanks,

Prabu G

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.

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
Community Beginner ,
Jun 04, 2018 Jun 04, 2018

Copy link to clipboard

Copied

Sorry I didn’t include the full code in the beginning, I have a script point to a folder so that I can collect all files in folder and subfolders as myFiles.

Thank you for your reply and Sorry for the confusion!

My problem is not that I cannot generate the report,

my problem is I cannot find the text with two or more conditions applied.

I can only find the text with one condition applied.

Revised full code as below. Any help would be appreciated.

var myFolder = Folder.selectDialog( "Select a folder to process" );

if ( myFolder != null ) {

     var myFiles = [];

      GetSubFolders(myFolder); }

  function GetSubFolders(theFolder) {

     var myFileList = theFolder.getFiles();

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

          var myFile = myFileList;

          if (myFile instanceof Folder){

               GetSubFolders(myFile);

          }

          else if (myFile instanceof File && myFile.name.match(/\.indd$/i)) {

               myFiles.push(myFile);

          }

     }

}

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

    var newDoc =  app.open(myFiles);

    myDoc = app.activeDocument;

    var Report = new File ( myFiles.parent+"/"+myFiles.name.replace ( /\.indd$/i, ".txt" ) ) 

    Report.open( "w" );

    var myConditions = myDoc.conditions;

    myConditions.everyItem().visible = false;

    l = myConditions.length;

    while(l--){

        myCondition =     myConditions;

        myCondition.visible = true;

        myConditionName = myCondition.name;

        app.findGrepPreferences = NothingEnum.nothing;

        app.findChangeGrepOptions.includeMasterPages = true;

        app.findGrepPreferences.appliedConditions=[myCondition];

        var myFind = myDoc.findGrep();

        Report.writeln(myConditionName)

        for(var a=myFind.length-1; a>=0; a--){

            Report.writeln(myFind.contents+"\r");

            }

        Report.writeln("\r\r\r");

        app.findGrepPreferences = NothingEnum.nothing;

        }

    myCondition.visible = false;

    }

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
Community Expert ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

Hi,

appliedConditions is a property of object Text and therefore a property of textStyleRange.

Further: The value of appliedConditions is an Array.

A textStyleRange is a range of text with mutual formatting.

So you could go through all textStyleRanges of a Story and look for appliedConditions.

The result will tell you what conditions are applied if any. You could even add the textStyleRanges to the result.

Let's test that with a story at hand. A simple text frame where several conditions are applied. To parts of the text even more than one condition is applied:

ConditionalTextSample-1.PNG

If you run the script snippet below on the selected text frame you can see the result in the JavaScript Console of your ESTK. It will contain the names of the applied conditions plus all the text ranges in an "associative" array. The result array could be used to duplicate the text ranges to a different document in a sorted way.

// Text frame of story selected:

var textStyleRanges = app.selection[0].parentStory.textStyleRanges.everyItem().getElements();

var textStyleRangesLength = textStyleRanges.length;

var resultArray = [];

for(var n=0;n<textStyleRangesLength;n++)

{

    // No condition applied, loop on:

    if( textStyleRanges.appliedConditions.length == 0 ){ continue };

    var conditions =  textStyleRanges.appliedConditions;

    var conditionsLength = conditions.length;

    var conditionNames = [];

  

    for( var c = 0; c<conditionsLength; c++)

    {

        conditionNames = conditions.name;

    };

    var conditionsNamesString = conditionNames.join(" & ");

  

    if( conditionsNamesString in resultArray )

    {

        resultArray[ conditionsNamesString ] = resultArray[ conditionsNamesString ].concat( [textStyleRanges] );

    }

    else

    {

        resultArray[ conditionsNamesString ] = [textStyleRanges];

    }

};

// Write the results to the JavaScript-Console of the ESTK

for( x in resultArray )

{

    $.writeln( x +"\t"+ resultArray );

};

Here the result from the JavaScript-Console:

Cond1 & Cond3    [object TextStyleRange]

Cond2    [object TextStyleRange],[object TextStyleRange]

Cond3    [object TextStyleRange],[object TextStyleRange]

Cond2 & Cond3    [object TextStyleRange]

Cond1    [object TextStyleRange],[object TextStyleRange]

Cond1 & Cond2 & Cond3    [object TextStyleRange]

Best,
Uwe

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
Community Expert ,
Jun 05, 2018 Jun 05, 2018

Copy link to clipboard

Copied

Also see into DOM documentation for TextStyleRange :

Adobe InDesign CS6 (8.0) Object Model JS: TextStyleRange

and its property contents:

Adobe InDesign CS6 (8.0) Object Model JS: TextStyleRange

Note: To visit all text style ranges of a document keep in mind that Story is not the only object you have to look at.

See also into table text Cells and Footnotes.

Regards,
Uwe

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
Community Beginner ,
Jun 06, 2018 Jun 06, 2018

Copy link to clipboard

Copied

Thank you for your help Uwe!

I still have problem understanding the logic starting from line 21 to line 37,

and not yet familiar with how to get the contents of textStyleRange

but I will definitely do more readings.

Really appreciated it!

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
Community Expert ,
Jun 06, 2018 Jun 06, 2018

Copy link to clipboard

Copied

LATEST

Hi VWu ,

to get the contents of a textStyleRange is easy.

contents is a property of textStyleRange.

Do a text frame, type some text, format some words or characters differently and run the following snippet after selecting the frame:

var story = app.selection[0].parentStory;

var textStyleRanges = story.textStyleRanges.everyItem().getElements();

var lengthOfStyleRanges = textStyleRanges.length;

for( var n=0; n<lengthOfStyleRanges; n++ )

{

    $.writeln( textStyleRanges.contents );

};

To understand what I did in lines 21 to line 37 examine and run this little snippet from the ESTK:

// There is nothing special to the name of this variable:

var associativeArray = [];

var x;

// Next we store key and value pairs.

// Key must be a string, value can be anything. In this case an array with a string:

associativeArray["animals"] = [ "dog" ];

associativeArray["plants"] = [ "tree" ];

$.writeln( associativeArray.length ); // Returns 0

$.writeln( "animals" in associativeArray ); // Returns true

$.writeln( "mushrooms" in associativeArray ); // Returns false

// for in-loop to get all key/value-pairs:

for( x in associativeArray )

{

    $.writeln( x +" : "+ associativeArray );

    $.writeln( x +" : "+ associativeArray.constructor.name );

};

// Adding something to the value of "animals" and not replacing the value:

if( "animals" in associativeArray )

{

    associativeArray["animals"] = associativeArray["animals"].concat( ["cat"] );

}

else{ associativeArray["animals"] = ["cat"] };

for( x in associativeArray )

{

    $.writeln( x +" : "+ associativeArray );

    $.writeln( x +" : "+ associativeArray.constructor.name );

};

So basically in lines 21 to 37 I am building key strings and assign an array of formatted chunks of text as value, the textStyleRanges.

Regards,
Uwe

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