1 Reply Latest reply on Jan 18, 2016 3:40 PM by Jump_Over

    problem with picking story in find/change script

    Henry Sulek

      Hi, everyone!

      I'm trying to create my own version of find/change script that will gather couple of features. I want to equip it with dropdownlist with possibility of choosing story, selected text, document etc. Everything's working great until "story" is picked up - after running script in this mode InDesign is slowing down, it takes couple of second to recover. Where is my mistake?

      Probably this kind of script was done by most of the users, I know that, but I thought it would be nice way to start learning.

      Thanks in advance for any advice.

       

      Here's my code:

      #targetengine 'cleaning'

       

      var mainPanel = {};

      createDialog();

      mainPanel.win.show();

       

      // makes certain that user interaction (display of dialogs, etc.) is turned on.

      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;

       

      // creates main dialog window

      function createDialog(){

          mainPanel.win = new Window('palette', 'find&change');

          mainPanel.win.pnlSearch = mainPanel.win.add('panel');

          var arrSearch = ['This document', 'Selected text', 'Story', 'All open documents'];

          mainPanel.win.pnlSearch.ddl = mainPanel.win.pnlSearch.add('dropdownlist', undefined, arrSearch);

       

          // detects search area chosen by user

          if(app.documents.length > 0) {

              if(app.selection.length > 0) {

                  switch(app.selection[0].constructor.name) {

                           // any kind of text selection was made

                      case 'Character':

                      case 'Word':

                      case 'TextStyleRange':

                      case 'Line':

                      case 'Paragraph':

                      case 'TextColumn':

                      case 'Text':

                      case 'Cell':

                      case 'Column':

                      case 'Row':

                      case 'Table':

                          mainPanel.win.pnlSearch.ddl.selection = 1;

                          break;

                      // something was selected, but it wasn't a text object,  or cursor was placed in text frame, so search the document

                      case 'InsertionPoint':               

                      default:

                          mainPanel.win.pnlSearch.ddl.selection = 0;

                  }

              }

              else {

                  // nothing was selected, so simply search the document

                  mainPanel.win.pnlSearch.ddl.selection = 0;

              }

          }

          else{

              alert('No documents are open. Please open a document and try again.');

              exit();

          }

       

          // search group

          mainPanel.win.pnlOps = mainPanel.win.add('panel', undefined, 'Operations');

          mainPanel.win.pnlOps.alignChildren = 'left';

       

          // search questions

          mainPanel.win.pnlOps.search0 = mainPanel.win.pnlOps.add('checkbox', undefined, 'multipled white spaces');

              mainPanel.win.pnlOps.search0.helpTip = 'removes all multipled white spaces';

          mainPanel.win.pnlOps.search1 = mainPanel.win.pnlOps.add('checkbox', undefined, 'initial/closing spaces');

              mainPanel.win.pnlOps.search1.helpTip = 'removes all white spaces from beginning and end of paragraph';

          mainPanel.win.pnlOps.search2 = mainPanel.win.pnlOps.add('checkbox', undefined, 'spaces in front of ?!,.:;)');

              mainPanel.win.pnlOps.search2.helpTip = 'xxx';

          mainPanel.win.pnlOps.search3 = mainPanel.win.pnlOps.add('checkbox', undefined, '?!… …?!');

              mainPanel.win.pnlOps.search3.helpTip = 'xxx';

          mainPanel.win.pnlOps.search4 = mainPanel.win.pnlOps.add('checkbox', undefined, '2 hyphens --> em dash.');

              mainPanel.win.pnlOps.search4.helpTip = 'xxx';

          // activating checkboxes - by default

          for(i = 0; i < mainPanel.win.pnlOps.children.length; i ++) {

              mainPanel.win.pnlOps.children[i].value = true;

          }

       

          // buttons groups

          mainPanel.win.grpActivate = mainPanel.win.add('group');

          mainPanel.win.grpNavi = mainPanel.win.add('group');

       

          // buttons size

          var btnWidth = 100;

          var btnHeight = 20;

       

          // Select All and Deselect All buttons

          mainPanel.win.grpActivate.actCheckboxes = mainPanel.win.grpActivate.add('button', undefined, 'Select All');

          mainPanel.win.grpActivate.actCheckboxes.minimumSize = [btnWidth, btnHeight];

          mainPanel.win.grpActivate.actCheckboxes.onClick = activateCheckboxes;

          mainPanel.win.grpActivate.deactCheckboxes = mainPanel.win.grpActivate.add('button', undefined, 'Deselect All');

          mainPanel.win.grpActivate.deactCheckboxes.minimumSize = [btnWidth, btnHeight];

          mainPanel.win.grpActivate.deactCheckboxes.onClick = deactivateCheckboxes;

       

          // Clean Up button

          mainPanel.win.grpNavi.cleanUp = mainPanel.win.grpNavi.add('button', undefined, 'Clean Up');

          mainPanel.win.grpNavi.cleanUp.onClick = cleanUpText;

          mainPanel.win.grpNavi.cleanUp.minimumSize = [btnWidth, btnHeight];

       

          // Done button

          mainPanel.win.grpNavi.done = mainPanel.win.grpNavi.add('button', undefined, 'Done');

          mainPanel.win.grpNavi.done.minimumSize = [btnWidth, btnHeight];

          mainPanel.win.grpNavi.done.onClick = doneWindow;

       

       

          // activate checkboxes function

          function activateCheckboxes() {

              for(i = 0; i < mainPanel.win.pnlOps.children.length; i++) {

                  mainPanel.win.pnlOps.children[i].value = true;

              }

          }

          // deactivate checkboxes function

          function deactivateCheckboxes() {

              for(i = 0; i < mainPanel.win.pnlOps.children.length; i++) {

                  mainPanel.win.pnlOps.children[i].value = false;

              }

          }

          //  done function

          function doneWindow() {

              mainPanel.win.close();

          }

       

      }

       

      function cleanUpText() {

          var arrDocs =[];

         

          // applying search area chosen by user

          switch(mainPanel.win.pnlSearch.ddl.selection.text) {

              case 'Selected text':

                  arrDocs[0] = app.selection[0];

                  break;

              case 'This document':

                  arrDocs[0] = app.activeDocument;

                  break;

              case 'All open documents':

                  arrDocs = app.documents;

                  break;

              case 'Story':

                  arrDocs = app.selection[0].parentStory;

                  break;

       

          }

       

          // perform operations chosen by user

          if(mainPanel.win.pnlOps.search0.value == true) {

              doFindChange(arrDocs, '[~m~>~f~|~S~s~<~/~.~3~4~% ]{2,}', ' ');

          }

          if(mainPanel.win.pnlOps.search1.value == true) {

              doFindChange(arrDocs, '^\\s+|\\s+$', '');

          }

          if(mainPanel.win.pnlOps.search2.value == true) {

              doFindChange(arrDocs, '\r\r', '\r');

          }

          if(mainPanel.win.pnlOps.search3.value == true) {

              doFindChange(arrDocs, ' \'', ' “');

          }

          if(mainPanel.win.pnlOps.search4.value == true) {

              doFindChange(arrDocs, '--', '—');

          }

      }

       

      function doFindChange(arrDocs, strFindWhat, strChangeTo) {

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

              app.findGrepPreferences = NothingEnum.nothing;

              app.changeGrepPreferences = NothingEnum.nothing;

              app.findGrepPreferences.findWhat = strFindWhat;

              app.changeGrepPreferences.changeTo = strChangeTo;

             

              if(mainPanel.win.pnlSearch.ddl.selection.text == 'Selected text') {

                  app.selection[0].changeGrep();

              } else if(mainPanel.win.pnlSearch.ddl.selection.text == 'Story') {

                  app.selection[0].parent.changeGrep();

              }

              else {

                  arrDocs[i].changeGrep();

              }

       

              app.findGrepPreferences = NothingEnum.nothing;

              app.changeGrepPreferences = NothingEnum.nothing;

          }

      }

        • 1. Re: problem with picking story in find/change script
          Jump_Over Level 5

          Hi,

           

          I did not check your createDialog() function assuming it works as you wish.

          But checking what's inside other two funcions:

           

          Basically you should notice that user can fire script with various kind of selection (text, table, frames).

          Code is reading this - but uses it only to set initial choice of dropdownlist in Dialog.

          app.selection[0] (various kind of object) stay a base for further procedures in your code in many places.

           

          Additionally notice:

          1. cleanUpText()

          //...
          
          switch(mainPanel.win.pnlSearch.ddl.selection.text) {
                  case 'Selected text':
                      arrDocs[0] = app.selection[0];
                      break;
                  case 'This document':
                      arrDocs[0] = app.activeDocument;
                      break;
                  case 'All open documents':
                      arrDocs = app.documents;
                      break;
                  case 'Story':
                      arrDocs = app.selection[0].parentStory;
                      break;
          //...
          
          
          
          
          
          

           

          this part defines variable arrDoc as:

          • 1-element array with text or object
          • 1-element array with doc
          • collections of docs
          • Story

          I am not sure if it is a good idea to create this kind of mash...

           

          2. doFindChange(arrDocs, strFindWhat, strChangeTo)


          This function - regardless of passing variable "arrDocs" - uses app.selection[o] to proccess some code...


          This line:

           

          app.selection[0].parent.changeGrep();

           


          - depends on app.selection[0].constructor.name - can process various objects (text, frame, doc) but should proccess a Story

           

          So your code need some cleaning to ensure proccessing objects you really assume to be proccessed.


          Jarek