1 Reply Latest reply on Apr 6, 2009 1:48 AM by Kasyan Servetsky

    INCOPY SCRIPT -- What's wrong with this script??

    Level 1
      Could someone please tell me what's wrong with the below script? I need it to use the FindChangeList to do some basic formatting (replacing double-dashes with em-dashes), then apply the next style, then find my last two lines and format them to a paragraph style, THEN map styles to tags. It's all working EXCEPT it does not do the initial "find/replace" formatting. I suspect it's a conflict of the FindChangeList somehow?? Any and all help is appreciated.

      //FindChangeByList.jsx
      main();
      function main(){
      var myObject;
      var myCheckSelection = false;
      if(app.documents.length > 0){
      if(app.selection.length > 0){
      switch(app.selection[0].constructor.name){
      case "InsertionPoint":
      case "Character":
      case "Word":
      case "TextStyleRange":
      case "Line":
      case "Paragraph":
      case "TextColumn":
      case "Text":
      myObject = myDisplayDialog();
      myCheckSelection = myObject[1];
      myObject = myObject[0];
      if(myObject != "None"){
      myFindChangeByList(myObject, myCheckSelection);
      }
      break;
      default:
      myFindChangeByList(app.documents.item(0), false);
      }
      }
      else{
      //Nothing was selected, so simply search the document.
      myFindChangeByList(app.documents.item(0), false);
      }
      }
      else{
      alert("No documents are open. Please open a document and try again.");
      }
      }
      function myDisplayDialog(){
      var myObject;
      var myDialog = app.dialogs.add({name:"FindChangeByList"});
      with(myDialog.dialogColumns.add()){
      with(dialogRows.add()){
      with(dialogColumns.add()){
      staticTexts.add({staticLabel:"Search Range:"});
      }
      var myRangeButtons = radiobuttonGroups.add();
      with(myRangeButtons){
      radiobuttonControls.add({staticLabel:"Selection"});
      radiobuttonControls.add({staticLabel:"Selected Story"});
      radiobuttonControls.add({staticLabel:"Document", checkedState:true});
      }
      }
      }
      var myResult = myDialog.show();
      if(myResult == true){
      switch(myRangeButtons.selectedButton){
      case 0:
      myObject = app.selection[0];
      myCheckSelection = true;
      break;
      case 1:
      myObject = app.selection[0].parentStory;
      myCheckSelection = false;
      break;
      case 2:
      myObject = app.documents.item(0);
      myCheckSelection = false;
      break;
      }
      }
      else{
      myObject = "None";
      }
      myDialog.destroy();
      return [myObject, myCheckSelection];
      }
      function myFindChangeByList(myObject, myCheckSelection){
      var myScriptFileName, myFindChangeFile, myFindChangeFileName, myScriptFile, myResult;
      var myFindChangeArray, myFindPreferences, myChangePreferences, myFindLimit;
      var myStartCharacter, myEndCharacter;
      var myFindChangeFile = myFindFile("/FindChangeSupport/FindChangeList.txt")
      if(myFindChangeFile != null){
      // Because the selection will change as we add/remove characters,
      // we'll need to reset the selection after each pass if we are
      // checking the selection. We'll get the index of the first character
      // in the selection (relative to the start of its parent story) and
      // the index of the last character in the selection (relative to the
      // *end* of the story, and we'll use them later in the script to
      // keep the ends of the selection in place.
      if(myCheckSelection == true){
      var myStart = myObject.characters.item(0).index;
      var myEnd = myObject.characters.item(-1).index;
      var myStory = myObject.parentStory;
      var myStoryEnd = myStory.characters.item(-1).index;
      myEnd = (myStoryEnd - myEnd)+1;
      }
      myFindChangeFile = File(myFindChangeFile);
      var myResult = myFindChangeFile.open("r", undefined, undefined);
      if(myResult == true){
      //Loop through the find/change operations.
      do{
      myLine = myFindChangeFile.readln();
      //Ignore comment lines and blank lines.
      if((myLine.substring(0,4)!="text")||(myLine.substring(0,4)!="grep")||(myLine.substring(0, 5)!="glyph")){
      myFindChangeArray = myLine.split("\t");
      //