2 Replies Latest reply on Apr 11, 2013 6:28 AM by JeffLambert

    [JS CS6] modifying Peter's RunScript script

    JeffLambert Level 1

      Hi,

       

      With Peter K. permission. I want to modify his script Runscript.jsx to fit my needs, which are, to be able to "see" all scripts within subfolders of the main script folder and show applescripts AND javascripts.

       

      First, I'm just starting out in JS and scripting for me as only been in Applescript so far, so bare with me and thanks for your patience.

       

      I've changed some parts of the scripts with bits of code Peter was gracious enough to send me, but all I get is the list of either applescripts, or js, depending  on the file extension I choose in the var scripts. I also get a full path name instead of just the name and when I choose a script to execute, it says it can't find it. In AS, I would say it doesn't resolve to the file in question and just has the path, not the actual file.

       

      Anyway, any help would be appriciated. Here's the script so far in it's entirety. Sorry if it's too long.

       

      // DESCRIPTION: Launch a script by typing its name or picking it from a recent-history list

      // Peter Kahrel 2006-2011 -- www.kahrel.plus.com

       

       

      #target indesign;

       

       

      try {app.doScript (get_script ())}

          catch (e) {alert (e.message + "\r(line " + e.line + ")")};

       

       

      function get_script ()

          {      

          var script_dir = find_script_dir();

          var fstring = script_dir + "/runscript-last.txt";

          var history = get_history (fstring);

          if (history.length > 0)

              {

              if (history[0].indexOf("true") > -1 || history[0].indexOf("false") > -1)

                  {

                  var temp = history[0].split("£");

                  var filter_check_history = eval(temp[0]);

                  try {keep_filter_history = eval(temp[1]);} catch(_){keep_filter_history = false;}

                  try {filter = temp[2];} catch(_){filter = "";}

                  history.shift();

                  }

              }

       

       

          var scripts = get_scripts (script_dir, [], ".applescript"); //changed to the fonction that gets every file in subfolders, can't get it to work on ALL files though

          //var scripts = get_scripts (script_dir);

          if (history.length === 0)

              var droplist = scripts;

          else

              var droplist = history.concat ("------------------------------------------------------------------").concat (scripts);

          var ListLength = droplist.length;

       

       

          var w = new Window ("dialog", "Script launcher", undefined, {closeButton: false});

              var filter, i;

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

                  main.orientation = "column";

                  var entry = main.add ("edittext", undefined, droplist[0]);

                  entry.minimumSize.width = 300;

                  var list = main.add ("listbox", undefined, droplist);

                      list.preferredSize = [300, 280];

                      list.selection = 0;

                     

                  var options = main.add ("group");

                      options.alignment = "left";

                      var filter_check = options.add ("checkbox", undefined, "\u00A0Filter list\u00A0");

                          try {filter_check.value = filter_check_history;} catch(_){filter_check.value = false;}

                      var keep_filter = options.add ("checkbox", undefined, "\u00A0Apply filter on next run\u00A0");

                          try {keep_filter.value = keep_filter_history;} catch(_){keep_filter.value = false;}

       

       

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

                  buttons.alignment = "right";

                  ok_button = buttons.add ("button", undefined, "OK", {name: "OK"});

                  buttons.add ("button", undefined, "Cancel", {name: "cancel"});

                 

              entry.onChanging = FilterList;

             

              function FilterList ()

                  {

                  filter = entry.text;

                  if (filter_check.value)

                      {

                      filter = entry.text;

                      list.removeAll();

                      for (i = 0; i < droplist.length; i++)

                          {

                          if (droplist[i].toLowerCase().indexOf (filter.toLowerCase()) > -1 || droplist[i].slice (0,3) === "---")

                              {

                              list.add ("item", droplist[i]);

                              }

                          }

                      list.items[0].text.slice(0,3)==="---" ? list.selection = 1 : list.selection = 0;

                      }

                  else

                      {

                      i = 0;

                      // Look for the first match

                      while (i < ListLength && list.items[i].text.toLowerCase().indexOf (filter.toLowerCase()) < 0) {++i;}

                      if (list.items[i].text.toLowerCase().indexOf (filter.toLowerCase()) > -1)

                          {

                          // select it

                          list.selection = i;

                          // then centre it in the listbox

                          if (i < ListLength-7) {list.revealItem(i+7);}

                          else {list.revealItem(ListLength-1);}

                          }

                      }

                  } // entry.onChanging

       

       

       

       

              list.addEventListener("click", function (event)

                  {

                  if (event.detail == 2 /*if double-click*/)

                    w.close (1);

                  }

              );

       

       

       

       

              w.onShow = function ()

                  {

                  w.layout.layout();

                  entry.active = true;

                  if (filter !== "" && filter_check.value)

                      {

                      entry.text = filter;

                      FilterList();

                      }

                  }

             

              if (w.show () == 2)

                  {

                  w.close ();

                  exit ();

                  }

              else

                  {

                  var script = list.selection.text;

                  if (!keep_filter.value) filter = "";

                  store_history (fstring, script, history, filter_check.value, keep_filter.value, filter);

                  return File (script_dir + "/" + script);

                  }

          } // function drop_typeahead

       

       

       

       

      //function get_scripts (script_dir)

         // {

          //var array = [];

          //var f = Folder (script_dir).getFiles ("*.js*");

          //for (var i = 0; i < f.length; i++)

           //   array.push (f[i].name);

          //return array

         // }

       

       

       

       

      function get_history (fstring)

          {

          var f = File (fstring);

          var h = [];

          if (f.exists)

              {

              f.open ("r");

              var temp = f.read ();

              f.close();

              var h = temp.split ("\n");

              }

          return h

          }

       

       

       

       

      function store_history (fstring, new_item, history, filter_check, keep_filter, filter)

          {

          history = insert_item (new_item, history);

          var f = File (fstring);

          f.open ("w");

          f.write (filter_check+"£"+keep_filter+"£"+filter + "\n" + history);

          f.close ();

          }

       

       

      // Update the history list

       

       

      function insert_item (new_item, history)

          {

          // Add \n at beginning and end

          var s = "\n" + history.join ("\n") + "\n";

          // Delete the script's name from the history list (if it's there)

          s = s.replace ("\n"+new_item+"\n", "\n");

          // Add the script's name at the beginning

          s = new_item+s;

          // If there are more than 15 items, chop off the last one

          if (s.match (/\n/g).length > 15)

              s = s.replace (/\n[^\n]+\n$/, "\n");

          // Delete trailing returns

          s = s.replace (/[\n]+$/, "");

          return s

          }

       

       

       

       

      function find_script_dir()

          {

          try {return File (app.activeScript).path}

          catch(e) {return File (e.fileName).path}

          }

       

       

      function get_scripts (dir, array, mask){ //changed name of fonction to match script

                var f = Folder(dir).getFiles('*.*');

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

                          if (f[i] instanceof Folder){

                                    get_scripts (f[i], array, mask); //changed name of fonction to match script

                  //find_files (f[i], array, mask);

                          } else if (f[i].name.substr(-mask.length).toUpperCase() == mask.toUpperCase()){

                                    array.push (f[i]);

                          }

                }

                return array;

      }

        • 1. Re: [JS CS6] modifying Peter's RunScript script
          Peter Kahrel Adobe Community Professional & MVP

          Jeff,

           

          To look for multiple file types in the current folder and its subfolders, you need something like this:

           

          var scripts = find_files ('/d/scripts/', ['.jsx', '.applescript']);
          
          function find_files (dir, mask_array){
              var arr = [];
              for (var i = 0; i < mask_array.length; i++){
                  arr = arr.concat (find_files_sub (dir, [], mask_array[i].toUpperCase()));
              }
              return arr;
          }
          
          function find_files_sub (dir, array, mask){
              var f = Folder (dir).getFiles ( '*.*' );
              for (var i = 0; i < f.length; i++){
                  if (f[i] instanceof Folder){
                      find_files_sub (f[i], array, mask);
                  } else if (f[i].name.substr (-mask.length).toUpperCase() == mask){
                      array.push (f[i]);
                  }
              }
              return array;
          }
          

           

          Peter

          • 2. Re: [JS CS6] modifying Peter's RunScript script
            JeffLambert Level 1

            Thanks Peter,

             

            Now I can see the scripts I'm interested in, but still have the problem of seeing the whole path in the dialog, and it's pretty long, and the scripts don't actually work once selected and clicking OK. I guess I was a bit too ambisious to try to modify this script. Oh well, I'll go back to more basic stuff until I understand Javascript better. Back to Peter's book!

             

            Jeff