2 Replies Latest reply on Jun 10, 2011 12:26 PM by Cris Ider

    Clean up spurious white space

    Cris Ider Level 1

      Can anyone help me change this script?

       

      I need to turn this script just works when I select text (by Selection Tool or Type Tool), if is there no text selected, do nothing.

      Cause I apply keyboard shortcuts on all scripts and sometimes I apply it accidentally and causes drastics modifications in my document.

      So my idea is turn this script to works just if  to selected text (by Selection Tool or Type Tool).

       

       

      Script:

       

      // Clean up spurious white space; optionally convert all caps to sentence case

      #target indesign
      clean_up (app.activeDocument, true);

      exit();

      try
      {
      var caps = ask ();
      if (app.selection.length == 0)
      clean_up (app.activeDocument, caps);
      else
      clean_up (app.selection[0], caps);
      }
      catch (e) {alert (e.message + " (line " + e.line + ")")};


      function clean_up (scope, caps)
      {
      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findChangeGrepOptions.includeFootnotes = true;
      app.findChangeGrepOptions.includeHiddenLayers = true;
      app.findChangeGrepOptions.includeLockedLayersForFind = true;
      app.findChangeGrepOptions.includeLockedStoriesForFind = true;
      app.findChangeGrepOptions.includeMasterPages = true;

      // series of tabs
      change_grep (scope, '\\t\\t+', '\\t');
      // series of returns
      change_grep (scope, '\\r\\r+', '\\r');
      // series of spaces
      change_grep (scope, '\\x{20}\\x{20}+', '\\x{20}');
      // trailing spaces and tabs
      change_grep (scope, '\\s+$', "");
      // leading spaces/tabs
      change_grep (scope, '^\\s+', "");
      // ALL UPPERCASE TO Sentence case
      // This fails on certain types of selection,
      // so you need to "try"
      if (caps == true)
      try {uc2sc (scope)} catch(_){}
      } // clean_up


      function change_grep (obj, f, r)
      {
      app.findGrepPreferences.findWhat = f;
      app.changeGrepPreferences.changeTo = r;
      obj.changeGrep()
      }


      function uc2sc (obj)
      {
      app.findGrepPreferences = null;
      app.findGrepPreferences.findWhat = '^[^\\l]+$'
      var f = obj.findGrep (true);
      for (var i = 0; i < f.length; i++)
      f[i].changecase (ChangecaseMode.sentencecase);
      }


      function ask ()
      {
      var w = new Window ("dialog", "Clean up");
      w.alignChildren = "right";
      var panel = w.add ("panel");
      var chk = panel.add ("checkbox", undefined, "\u00A0All caps to sentence case");
      var buttons = w.add ("group");
      buttons.add ("button", undefined, "OK", {name: "ok"});
      buttons.add ("button", undefined, "Cancel", {name: "cancel"});
      if (w.show ()== 1)
      return chk.value;
      else
      exit ();
      }

        • 1. Re: Clean up spurious white space
          Harbs. Level 6

          This will work on only the current selection:

           

          try{
              clean_up (app.selection[0], true);
          }catch(err){}
          
          
          
          function clean_up (scope, caps)
          {
              app.findGrepPreferences = app.changeGrepPreferences = null;
              app.findChangeGrepOptions.includeFootnotes = true;
              app.findChangeGrepOptions.includeHiddenLayers = true;
              app.findChangeGrepOptions.includeLockedLayersForFind = true;
              app.findChangeGrepOptions.includeLockedStoriesForFind = true;
              app.findChangeGrepOptions.includeMasterPages = true;
              
              // series of tabs
              change_grep (scope, '\\t\\t+', '\\t');
              // series of returns
              change_grep (scope, '\\r\\r+', '\\r');
              // series of spaces
              change_grep (scope, '\\x{20}\\x{20}+', '\\x{20}');
              // trailing spaces and tabs
              change_grep (scope, '\\s+$', "");
              // leading spaces/tabs
              change_grep (scope, '^\\s+', "");
              // ALL UPPERCASE TO Sentence case
              // This fails on certain types of selection,
              // so you need to "try"
              if (caps == true)
              try {uc2sc (scope)} catch(_){}
          } // clean_up
          
          
          function change_grep (obj, f, r)
          {
              app.findGrepPreferences.findWhat = f;
              app.changeGrepPreferences.changeTo = r;
              obj.changeGrep()
          }
          
          
          function uc2sc (obj)
          {
              app.findGrepPreferences = null;
              app.findGrepPreferences.findWhat = '^[^\\l]+$'
              var f = obj.findGrep (true);
              for (var i = 0; i < f.length; i++)
              f[i].changecase (ChangecaseMode.sentencecase);
          }
          
          • 2. Re: Clean up spurious white space
            Cris Ider Level 1

            Thank you!