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

Export text from grouped items

New Here ,
Dec 14, 2016 Dec 14, 2016

Copy link to clipboard

Copied

I have found the following script, that does a really good job of exporting all text from an InDesign document in a chosen format. The only problem seems to be, that text from grouped objects is not exported. Could anybody help to tweak the script to handle these items as well?

if(app.documents.length != 0){

     if(app.documents.item(0).stories.length != 0){

          myGetFileName(app.documents.item(0).name);

     }

}

//========================= FUNCTIONS ===========================

function myGetFileName(myDocumentName){

     var myFilePath = File.saveDialog("Save Exported Text As:");

     if(myFilePath != null){

          myDisplayDialog(myDocumentName, myFilePath);

     }

}

//--------------------------------------------------------------------------------------------------------------

function myDisplayDialog(myDocumentName, myFilePath){

     //Need to get export format, story separator.

     var myExportFormats = ["Text Only", "Tagged Text", "RTF"];

     var myDialog = app.dialogs.add({name:"ExportAllStories"});

     with(myDialog.dialogColumns.add()){

          with(dialogRows.add()){

               with(dialogColumns.add()){

                    var myExportFormatDropdown = dropdowns.add({stringList:myExportFormats, selectedIndex:2});

               }

          }

          with(dialogRows.add()){

               var myAddSeparatorCheckbox = checkboxControls.add({staticLabel:"Add separator line", checkedState:true});

          }

     }

     var myResult = myDialog.show();

     if(myResult == true){

          var myExportFormat = myExportFormats[myExportFormatDropdown.selectedIndex];

          var myAddSeparator = myAddSeparatorCheckbox.checkedState;

          myDialog.destroy();

          myExportAllText(myDocumentName, myFilePath, myExportFormat, myAddSeparator);

     }

     else{

          myDialog.destroy();

     }

}

//--------------------------------------------------------------------------------------------------------------

function myExportAllText(myDocumentName, myFilePath, myExportFormat, myAddSeparator){

     var myPage, myStory;

     var myExportedStories = [];

     var myTempFolder = Folder.temp;

     var myTempFile = File(myTempFolder + "/tempTextFile.txt");

     var myNewDocument = app.documents.add();

     var myDocument = app.documents.item(myDocumentName);

     var myTextFrame = myNewDocument.pages.item(0).textFrames.add({geometricBounds:myGetBounds(myNewDocument, myNewDocument.pages.item(0))});

     var myNewStory = myTextFrame.parentStory;

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

          myPage = myDocument.pages.item(i);

          for (var t = 0; t < myPage.textFrames.length; t++){

               myStory = myPage.textFrames.parentStory;

               if (!IsInArray(myStory.id, myExportedStories)) {

                    //Export the story as tagged text.

                    myStory.exportFile(ExportFormat.taggedText, myTempFile);

                    myExportedStories.push(myStory.id);

                    //Import (place) the file at the end of the temporary story.

                    myNewStory.insertionPoints.item(-1).place(myTempFile);

                    //If the imported text did not end with a return, enter a return

                    //to keep the stories from running together.

                    if(i != myDocument.stories.length -1){

                         if(myNewStory.characters.item(-1).contents != "\r"){

                              myNewStory.insertionPoints.item(-1).contents = "\r";

                         }

                         if(myAddSeparator == true){

                              myNewStory.insertionPoints.item(-1).contents = "----------------------------------------\r";

                         }

                    }

               } // if not exported

          } // for text frames

     } // for pages

     switch(myExportFormat){

          case "Text Only":

               myFormat = ExportFormat.textType;

               myExtension = ".txt"

               break;

          case "RTF":

               myFormat = ExportFormat.RTF;

               myExtension = ".rtf"

               break;

          case "Tagged Text":

               myFormat = ExportFormat.taggedText;

               myExtension = ".txt"

               break;

     }

     myNewStory.exportFile(myFormat, File(myFilePath));

     myNewDocument.close(SaveOptions.no);

     myTempFile.remove();

}

//--------------------------------------------------------------------------------------------------------------

function myGetBounds(myDocument, myPage){

     var myPageWidth = myDocument.documentPreferences.pageWidth;

     var myPageHeight = myDocument.documentPreferences.pageHeight

     if(myPage.side == PageSideOptions.leftHand){

          var myX2 = myPage.marginPreferences.left;

          var myX1 = myPage.marginPreferences.right;

     }

     else{

          var myX1 = myPage.marginPreferences.left;

          var myX2 = myPage.marginPreferences.right;

     }

     var myY1 = myPage.marginPreferences.top;

     var myX2 = myPageWidth - myX2;

     var myY2 = myPageHeight - myPage.marginPreferences.bottom;

     return [myY1, myX1, myY2, myX2];

}

//--------------------------------------------------------------------------------------------------------------

function IsInArray(myString, myArray) {

     for (x in myArray) {

          if (myString == myArray) {

               return true;

          }

     }

     return false;

}

TOPICS
Scripting

Views

423

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 ,
Dec 14, 2016 Dec 14, 2016

Copy link to clipboard

Copied

Hi,
I would ask the author of that script.

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
New Here ,
Dec 14, 2016 Dec 14, 2016

Copy link to clipboard

Copied

I certainly would, if I knew who wrote the original script 🙂

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 ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

Hi,

You can look at: https://forums.adobe.com/thread/733900

or change/use this part of code:

// by groups

var theGroups = myDocument.pages

.groups;

for(var g = 0, gln = theGroups.length; g < gln; g++){

    var grp = theGroups;

    for(var t = 0; t < grp.textFrames.length;t++){

        b = grp.textFrames;

        if (b instanceof TextFrame) {

            // code here...

        }

    }

}


Swo

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
New Here ,
Dec 16, 2016 Dec 16, 2016

Copy link to clipboard

Copied

Hi Wosven,

Thank you so much for trying to help me and excuse my ignorance, but where exactly would you place the code you suggest into the following script? I keep getting errors like “

is not defined”:

if(app.documents.length != 0){

if(app.documents.item(0).stories.length != 0){

myGetFileName(app.documents.item(0).name);

}

}

//========================= FUNCTIONS ===========================

function myGetFileName(myDocumentName){

var myFilePath = File.saveDialog("Save Exported Text As:");

if(myFilePath != null){

myDisplayDialog(myDocumentName, myFilePath);

}

}

//

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 ,
Dec 17, 2016 Dec 17, 2016

Copy link to clipboard

Copied

Hi,

"p" stand for the "page" in my original script, here, you need to replace it by "i".

Here an example with a group and a sub-level of group (a group in a group is exported too).

Text isn't exported depending on position in the page, so this script would need more tweaking, since you need to reorder exported texts.

if(app.documents.length != 0){

     if(app.documents.item(0).stories.length != 0){

            myGetFileName(app.documents.item(0).name);

     }

}

//========================= FUNCTIONS ===========================

function myGetFileName(myDocumentName){

     var myFilePath = File.saveDialog("Save Exported Text As:");

     if(myFilePath != null){

            myDisplayDialog(myDocumentName, myFilePath);

     }

}

//---------------------------------------------------------------------------------------- ----------------------

function myDisplayDialog(myDocumentName, myFilePath){

     //Need to get export format, story separator.

     var myExportFormats = ["Text Only", "Tagged Text", "RTF"];

     var myDialog = app.dialogs.add({name:"ExportAllStories"});

     with(myDialog.dialogColumns.add()){

            with(dialogRows.add()){

                 with(dialogColumns.add()){

                    var myExportFormatDropdown = dropdowns.add({stringList:myExportFormats, selectedIndex:2});

                 }

            }

            with(dialogRows.add()){

                 var myAddSeparatorCheckbox = checkboxControls.add({staticLabel:"Add separator line", checkedState:true});

            }

     }

     var myResult = myDialog.show();

     if(myResult == true){

            var myExportFormat = myExportFormats[myExportFormatDropdown.selectedIndex];

            var myAddSeparator = myAddSeparatorCheckbox.checkedState;

            myDialog.destroy();

            myExportAllText(myDocumentName, myFilePath, myExportFormat, myAddSeparator);

     }

     else{

            myDialog.destroy();

     }

}

//---------------------------------------------------------------------------------------- ----------------------

function myExportAllText(myDocumentName, myFilePath, myExportFormat, myAddSeparator){

     var myPage, myStory;

     var myExportedStories = [];

     var myTempFolder = Folder.temp;

     var myTempFile = File(myTempFolder + "/tempTextFile.txt");

     var myNewDocument = app.documents.add();

     var myDocument = app.documents.item(myDocumentName);

     var myTextFrame = myNewDocument.pages.item(0).textFrames.add({geometricBounds:myGetBounds(myNewDocument, myNewDocument.pages.item(0))});

     var myNewStory = myTextFrame.parentStory;

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

            myPage = myDocument.pages.item(i);

            for (var t = 0; t < myPage.textFrames.length; t++){

                 myStory = myPage.textFrames.parentStory;

                 if (!IsInArray(myStory.id, myExportedStories)) {

                    //Export the story as tagged text.

                    myStory.exportFile(ExportFormat.taggedText, myTempFile);

                    myExportedStories.push(myStory.id);

                    //Import (place) the file at the end of the temporary story.

                    myNewStory.insertionPoints.item(-1).place(myTempFile);

                    //If the imported text did not end with a return, enter a return

                    //to keep the stories from running together.

                    if(i != myDocument.stories.length -1){

                         if(myNewStory.characters.item(-1).contents != "\r"){

                                myNewStory.insertionPoints.item(-1).contents = "\r";

                         }

                         if(myAddSeparator == true){

                                myNewStory.insertionPoints.item(-1).contents = "----------------------------------------\r";

                         }

                    }

                 } // if not exported

            } // for text frames

        // by groups

        var theGroups = myDocument.pages.groups;

        for(var g = 0, gln = theGroups.length; g < gln; g++){

            var grp = theGroups;

            // try second level of groups (nested group)

            if (grp.groups.length > 0) {

                for(var g2 = 0, gln2 = grp.groups.length; g2 < gln2; g2++){

                    var grp2 = grp.groups[g2];

                    for(var t2 = 0; t2 < grp2.textFrames.length;t2++){

                        b2 = grp2.textFrames[t2];

                        if (b2 instanceof TextFrame) {

                            // code here...

                            myStory = b2.parentStory;

                            if (!IsInArray(myStory.id, myExportedStories)) {

                                //Export the story as tagged text.

                                myStory.exportFile(ExportFormat.taggedText, myTempFile);

                                myExportedStories.push(myStory.id);

                                //Import (place) the file at the end of the temporary story.

                                myNewStory.insertionPoints.item(-1).place(myTempFile);

                                //If the imported text did not end with a return, enter a return

                                //to keep the stories from running together.

                                if (i != myDocument.stories.length -1) {

                                    if (myNewStory.characters.item(-1).contents != "\r") {

                                        myNewStory.insertionPoints.item(-1).contents = "\r";

                                    }

                                    if (myAddSeparator == true) {

                                        myNewStory.insertionPoints.item(-1).contents = "----------------------------------------\r";

                                    }

                                }

                            } // if not exported

                        } // if TextFrame

                    } // for grp2

                } // for

            } // if grp2

            for(var t = 0; t < grp.textFrames.length;t++){

                b = grp.textFrames;

                if (b instanceof TextFrame) {

                    // code here...

                     myStory = b.parentStory;

                     if (!IsInArray(myStory.id, myExportedStories)) {

                        //Export the story as tagged text.

                        myStory.exportFile(ExportFormat.taggedText, myTempFile);

                        myExportedStories.push(myStory.id);

                        //Import (place) the file at the end of the temporary story.

                        myNewStory.insertionPoints.item(-1).place(myTempFile);

                        //If the imported text did not end with a return, enter a return

                        //to keep the stories from running together.

                        if (i != myDocument.stories.length -1) {

                            if (myNewStory.characters.item(-1).contents != "\r") {

                                myNewStory.insertionPoints.item(-1).contents = "\r";

                            }

                            if (myAddSeparator == true) {

                                myNewStory.insertionPoints.item(-1).contents = "----------------------------------------\r";

                            }

                        }

                     } // if not exported

                }

            } // for grp

        }

     } // for pages

     switch(myExportFormat){

            case "Text Only":

                 myFormat = ExportFormat.textType;

                 myExtension = ".txt"

                 break;

            case "RTF":

                 myFormat = ExportFormat.RTF;

                 myExtension = ".rtf"

                 break;

            case "Tagged Text":

                 myFormat = ExportFormat.taggedText;

                 myExtension = ".txt"

                 break;

     }

     myNewStory.exportFile(myFormat, File(myFilePath));

     myNewDocument.close(SaveOptions.no);

     myTempFile.remove();

}

//---------------------------------------------------------------------------------------- ----------------------

function myGetBounds(myDocument, myPage){

     var myPageWidth = myDocument.documentPreferences.pageWidth;

     var myPageHeight = myDocument.documentPreferences.pageHeight

     if(myPage.side == PageSideOptions.leftHand){

            var myX2 = myPage.marginPreferences.left;

            var myX1 = myPage.marginPreferences.right;

     }

     else{

            var myX1 = myPage.marginPreferences.left;

            var myX2 = myPage.marginPreferences.right;

     }

     var myY1 = myPage.marginPreferences.top;

     var myX2 = myPageWidth - myX2;

     var myY2 = myPageHeight - myPage.marginPreferences.bottom;

     return [myY1, myX1, myY2, myX2];

}

//---------------------------------------------------------------------------------------- ----------------------

function IsInArray(myString, myArray) {

     for (x in myArray) {

            if (myString == myArray) {

                 return true;

            }

     }

     return false;

}

Swo

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
New Here ,
Dec 18, 2016 Dec 18, 2016

Copy link to clipboard

Copied

LATEST

Thank you so much, Wosven! This works perfect.

The order is not important as long as you can search the full text, which you can now. 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