6 Replies Latest reply on Dec 4, 2008 8:06 PM by (charles_darwin)

    [CS2][JS]Access indesign files using a script

    Level 1
      Scripters help.

      Can javascript access files then look for a character style then write its content to a text file as well as its page number where it was located?
        • 1. Re: [CS2][JS]Access indesign files using a script
          Level 1
          what you want to do next with this file ?
          you can convert [en-space] to TAB and save content of Story to text file - or just Copy&Paste to Notepad

          and good advice - when you ask - tell us more about your problem - what you have and what you want to get - because there could be simpler and faster way to solve your problem ;)

          robin

          --
          www.adobescripts.com
          • 2. Re: [CS2][JS]Access indesign files using a script
            Level 1
            I am working on this book with multiple chapters and i need to extract all the authors from multiple indesign files. Since the authors have a characterStyle so I have to find the CharStyle in those indd files and get its content then save it to a text file and save the txt file somewhere let's say on c:
            thanks for the advice robin i really appreciate.
            • 3. Re: [CS2][JS]Access indesign files using a script
              Roy McCoy Level 1
              > i need to extract all the authors from multiple indesign files.

              Sounds like fun, but it also sounds like work. I suppose you'd either
              put all the files in a folder and loop through them, or open and
              search them all at the same time. But if you're only doing this once,
              I suspect it will be easier for you to just do it - i.e. search on the
              character style and copy the found names out to your text file - than
              to work out a script for the procedure. If you want to do it as a
              learning exercise, however, one can understand that.
              • 4. Re: [CS2][JS]Access indesign files using a script
                Level 1
                ok

                so you need to extract all Authors with page numbers, sort it alphabetically and colorize page ranges ?

                I don't know JS - I can help you only with VB - on PC

                robin

                --
                www.adobescripts.com
                • 5. Re: [CS2][JS]Access indesign files using a script
                  Level 1
                  Hi charles darwin,

                  Try something like this:

                  //ExtractByCharacterStyle.jsx
                  
                  //An InDesign CS3/CS4 JavaScript
                  //
                  //Extracts all of the text in all open documents
                  //by a specified character style name, then writes
                  //the text and the page number to a tab/return delimited
                  //text file.
                  main();
                  function main(){
                  if(app.documents.length != 0){
                    var myDocumentArray = new Array;
                    //Make a list of open document names; we'll use this later to refer to the documents.
                    for(var myCounter = 0; myCounter < app.documents.length; myCounter ++){
                     //If the document doesn't contain any text, don't add it to the list.
                     if(app.documents.item(myCounter).stories.length != 0){
                      myDocumentArray.push(app.documents.item(myCounter).name);
                     }
                    }
                    if(myDocumentArray.length != 0){
                     myDisplayDialog(myDocumentArray);
                    }
                  }
                  }
                  function myDisplayDialog(myDocumentArray){
                  var myDialog = app.dialogs.add({name:"Extract Text"});
                  var myCharacterStyleNames = app.documents.item(0).characterStyles.everyItem().name;
                  with(myDialog.dialogColumns.add()){
                    with(dialogRows.add()){
                     with(dialogColumns.add()){
                      staticTexts.add({staticLabel:"Character Style:"});
                     }
                     with(dialogColumns.add()){
                      var myCharacterStyleDropdown = dropdowns.add({stringList:myCharacterStyleNames, selectedIndex:0});
                     }
                    }
                  }
                  var myResult = myDialog.show();
                  if(myResult == true){
                    var myCharacterStyleName = myCharacterStyleNames[myCharacterStyleDropdown.selectedIndex];
                    myDialog.destroy();
                    myProcessDocuments(myDocumentArray, myCharacterStyleName);
                  }
                  else{
                    myDialog.destroy();
                  }
                  }
                  function myProcessDocuments(myDocumentArray, myCharacterStyleName){
                  var myDocument;
                  var myArray = new Array;
                  for(var myCounter = 0; myCounter < myDocumentArray.length; myCounter++){
                    myDocument = app.documents.item(myDocumentArray[myCounter]);
                    try{
                     myDocument.characterStyles.item("myCharacterStyle").name;
                     myExtractText(myDocument, myCharacterStyleName, myTextFile, myArray);
                    }
                    catch (myError){
                     alert("Something bad happened.");
                    }
                  }
                  //Now sort the entire text file by page number.
                  var myArray = myArray.sort(mySort);
                  var myTextFile = File.saveDialog("Save text file as");
                  //If the user clicked the Cancel button, the result is null; do nothing.
                  if(myTextFile != null){
                    //Open the file with write access.
                    myTextFile.open("w");
                    for(var myCounter = 0; myCounter < myArray.length; myCounter++){
                     myTextFile.writeln(myArray[myCounter][0] + "\t" + myArray[myCounter][1]);
                    }
                    myTextFile.close();
                  }
                  }
                  //This sort feature sorts text references by their page.
                  function mySort(a, b){
                  if(a[1] > b[1]){
                    return 1;
                  }
                  else{
                    if(a[1] <  b[1]){
                     return -1
                    }
                    else{
                     //If the two entries are on the same page, sort alphabetically.
                     if(a[0] >  b[0]){
                      return 1
                     }
                     else{
                      return -1;
                     }
                    }
                  }
                  }
                  function myExtractText(myDocument, myCharacterStyleName, myTextFile, myArray){
                  var myFoundItem, myTempArray;
                  var myCharacterStyle = myDocument.characterStyles.item(myCharacterStyleName);
                  app.findTextPreferences = NothingEnum.nothing;
                  app.changeTextPreferences = NothingEnum.nothing;
                  app.findTextPreferences.appliedCharacterStyle = myCharacterStyle;
                  var myFoundItems = myDocument.findText();
                  if(myFoundItems.length != 0){
                    for(var myCounter = 0; myCounter < myFoundItems.length; myCounter++){
                     myTempArray = new Array;
                     myString = myFoundItems[myCounter].contents;
                     myPage = myFindParentPage(myFoundItems[myCounter].parentTextFrames[0]);
                     myTempArray.push(myString);
                     myTempArray.push(myPage);
                     myArray.push(myTempArray);
                    }
                  }
                  }
                  function myFindParentPage(myParent){
                  switch(myParent.constructor.name){
                    case "Page":
                     myParentName = myParent.name;
                     break;
                    case "Spread":
                     myParentName = "Pasteboard";
                     break;
                    case "Application":
                     myParentName = "";
                     break;
                    case "Character":
                     myFindParentPage(myParent.parentTextFrames[0]);
                     break;
                    default:
                     myFindParentPage(myParent.parent);
                     break;
                  }
                  return myParentName;
                  }


                  Thanks,

                  Ole
                  • 6. Re: [CS2][JS]Access indesign files using a script
                    Level 1
                    Cool. Thanks to all of you guys.