3 Replies Latest reply on Feb 11, 2016 4:20 AM by bartb84992024

    Peter Kahrel's GREP change case script

    Chris Z.

      Hi,

      I'm trying to alter a script to change case based on a GREP search. I know next to nothing about scripting in general, but was hoping I'd be able to figure it out on my own.

       

      The original script can change from uppercase to lowercase and vice versa. I want to add the ability to change to title case.

       

      The original script is below:

      #target indesign;

      #targetengine "session";

       

       

      var w = new Window ("palette", "Change case", undefined, {closeButton: false, resizeable: true});

          var options = [ChangecaseMode.lowercase, ChangecaseMode.uppercase];

          w.alignChildren = "fill";

          var rb = w.add ("panel");

              var upper_to_lower = rb.add ("radiobutton", undefined, " A > a\u00A0");

              var lower_to_upper = rb.add ("radiobutton", undefined, " a > A\u00A0");

          var b = w.add ("group");

              b.orientation = "column";

              b.alignChildren = "fill";

              var find_next = b.add ("button", undefined, "Find next");

              var change = b.add ("button", undefined, "Change");

              var change_all = b.add ("button", undefined, "Change all");

              var change_find = b.add ("button", undefined, "Change/find");

              var done = b.add ("button", undefined, "Done", {name: "cancel"});

       

       

          if (app.findGrepPreferences.findWhat.indexOf ("\\u") > -1)

              {

              upper_to_lower.value = true;

              changetype = ChangecaseMode.lowercase;

              }

          else

              {

              lower_to_upper.value = true;

              changetype = ChangecaseMode.uppercase;

              }

       

       

          upper_to_lower.onClick = function () {changetype = ChangecaseMode.lowercase};

          lower_to_upper.onClick = function () {changetype = ChangecaseMode.uppercase};

       

       

          var found = app.documents[0].findGrep();

          var found_counter = 0;

          if (found.length > 0)

              show_found (found[found_counter]);

          else

              alert ("No (more) matches found.");

       

       

          find_next.onClick = function ()

              {

              found_counter++;

              if (found_counter < found.length)

                  show_found (found[found_counter]);

              else

                  alert ("No (more) matches found.");

              }

         

          change.onClick = function () {found[found_counter].changecase(changetype);}

         

          change_find.onClick = function ()

              {

              if (found_counter < found.length)

                  {

                  found[found_counter].changecase(changetype);

                  found_counter++;

                  if (found_counter < found.length)

                      show_found (found[found_counter]);

                  else

                      alert ("No (more) matches found.");

                  }

              }

         

          change_all.onClick = function ()

              {

              for (var i = found_counter; i < found.length; i++)

                  found[i].changecase(changetype);

              }

       

       

          done.onClick = function () {w.close()};

       

       

      w.show ();

       

       

       

       

      function show_found (f)

          {

          f.select();

          app.activeWindow.activePage = find_page (f.parentTextFrames[0]);

          }

       

       

       

       

      function find_page (o)

          {

          try

              {

              if (o.hasOwnProperty ("parentPage"))

                  return o.parentPage;

              if (o.constructor.name == "Page")

                  return o;

              switch (o.parent.constructor.name)

                  {

                  case "Character": return find_page (o.parent);

                  case "Cell": return find_page (o.parent.texts[0].parentTextFrames[0]);

                  case "Table" : return find_page (o.parent);

                  case "TextFrame" : return find_page (o.parent);

                  case "Group" : return find_page (o.parent);

                  case "Story": return find_page (o.parentTextFrames[0]);

                  case "Footnote": return find_page (o.parent.storyOffset);

                  case "Page" : return o.parent;

                  }

              }

              catch (_) {return ""}

          }

       

      My, non-working, version is next. I'm getting an error on line 26 (in red, bold). I think it's just an issue with my "else if" syntax. As a matter of fact, I'm not even sure if that's the proper way to do what I want.

       

      #target indesign;

      #targetengine "session";

       

       

      var w = new Window ("palette", "Change case", undefined, {closeButton: false, resizeable: true});

          var options = [ChangecaseMode.lowercase, ChangecaseMode.uppercase, ChangecaseMode.titlecase];

          w.alignChildren = "fill";

          var rb = w.add ("panel");

              var upper_to_lower = rb.add ("radiobutton", undefined, " A > a\u00A0");

              var lower_to_upper = rb.add ("radiobutton", undefined, " a > A\u00A0");

              var any_to_title = rb.add ("radiobutton", undefined, " a > A\u00A0");

          var b = w.add ("group");

              b.orientation = "column";

              b.alignChildren = "fill";

              var find_next = b.add ("button", undefined, "Find next");

              var change = b.add ("button", undefined, "Change");

              var change_all = b.add ("button", undefined, "Change all");

              var change_find = b.add ("button", undefined, "Change/find");

              var done = b.add ("button", undefined, "Done", {name: "cancel"});

       

       

          if (app.findGrepPreferences.findWhat.indexOf ("\\u") > -1)

              {

              upper_to_lower.value = true;

              changetype = ChangecaseMode.lowercase;

              }

          else if

              {

              lower_to_upper.value = true;

              changetype = ChangecaseMode.uppercase;

              }

          else

              {

              any_to_title.value = true;

              changetype = ChangecaseMode.titlecase;

              }

       

       

          upper_to_lower.onClick = function () {changetype = ChangecaseMode.lowercase};

          lower_to_upper.onClick = function () {changetype = ChangecaseMode.uppercase};

          any_to_title.onClick = function () {changetype = ChangecaseMode.titlecase};

       

       

          var found = app.documents[0].findGrep();

          var found_counter = 0;

          if (found.length > 0)

              show_found (found[found_counter]);

          else

              alert ("No (more) matches found.");

       

       

          find_next.onClick = function ()

              {

              found_counter++;

              if (found_counter < found.length)

                  show_found (found[found_counter]);

              else

                  alert ("No (more) matches found.");

              }

         

          change.onClick = function () {found[found_counter].changecase(changetype);}

         

          change_find.onClick = function ()

              {

              if (found_counter < found.length)

                  {

                  found[found_counter].changecase(changetype);

                  found_counter++;

                  if (found_counter < found.length)

                      show_found (found[found_counter]);

                  else

                      alert ("No (more) matches found.");

                  }

              }

         

          change_all.onClick = function ()

              {

              for (var i = found_counter; i < found.length; i++)

                  found[i].changecase(changetype);

              }

       

       

          done.onClick = function () {w.close()};

       

       

      w.show ();

       

       

       

       

      function show_found (f)

          {

          f.select();

          app.activeWindow.activePage = find_page (f.parentTextFrames[0]);

          }

       

       

       

       

      function find_page (o)

          {

          try

              {

              if (o.hasOwnProperty ("parentPage"))

                  return o.parentPage;

              if (o.constructor.name == "Page")

                  return o;

              switch (o.parent.constructor.name)

                  {

                  case "Character": return find_page (o.parent);

                  case "Cell": return find_page (o.parent.texts[0].parentTextFrames[0]);

                  case "Table" : return find_page (o.parent);

                  case "TextFrame" : return find_page (o.parent);

                  case "Group" : return find_page (o.parent);

                  case "Story": return find_page (o.parentTextFrames[0]);

                  case "Footnote": return find_page (o.parent.storyOffset);

                  case "Page" : return o.parent;

                  }

              }

              catch (_) {return ""}

          }

       

      Any advice would be greatly appreciated.

       

      Thanks,

      Chris