ah_script_tidy

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Kukurykus, Tom, xbytor

Andrew

ah_script_tidy

Post by Andrew »

You know you're dealing with addictive behaviour when you start writing scripts like this. On the other hand I have learned a great deal in creating this, so for me it has been worth it.

This is a very basic utility script that scans a text file, and identifies and, if requested, removes unused orphan functions. It can also remove commenting. For safety's sake I recommend the 'save to new file' option but it has been tested on some pretty large scripts, some not written by me and it seems to work ok.

There are a couple of bits of the script that could be useful to others, first, the comment cleaning function (set 'rep' to '' and comments are removed, set 'rep' to ' ' and they are replaced by blanks):

Code: Select allfunction removeComments (jsStr,rep)
{
   var re = /\/\*(?:\*(?!\/)|[^\*])*\*\//g;// finds /* */
   var re1 = /^[ \t]*\/\/.*$/mg; // finds ^ //
   var re2 = /([;\}\)\{]) *\/\/.*$/mg; // finds { or } or ) or ; followed by // to EOL
   var noComments = jsStr.replace(re,function (x) {return x.replace(/\S/g,rep);});
   noComments = noComments.replace(re1,function (x) {return x.replace(/\S/g,rep);});
   noComments = noComments.replace(re2,function (x,y) {
      return y + x.substr(1).replace(/\S/g,rep);});
   return noComments;   
}

and second the dynamic UI window inside this function:

Code: Select allfunction fDialog (tJSClean,orphanAr, sName)
{
   var insert = f0String = fString = vBtnString = '', lt, lb;
   var fDAr = new Object;
   fDAr.del = new Array;fDAr.selAr = new Array();
   for (var i = 0;i < orphanAr.length; i++)
   {
      lt = 40+(i+1)*30;lb = 65+(i+1)*30;

      insert += 'vBtn' + i + ": Button { text:'View', bounds:[10," + lt + ",60," + lb + "]}, ";// dlg
      insert += 'f' + i + ": Checkbox { text:'" + orphanAr[0] + "', bounds:[65," + lt + ",400," + lb + "]}, ";// dlg

      f0String    += 'fDAr.del[' + i + ']=false;';// initialise
      fString    += 'st.f' + i + '.onClick = function () {fDAr.del[' + i + ']=st.f' + i + '.value;}';// initialise
      vBtnString += 'st.vBtn' + i + '.onClick = function () {alert(orphanString(orphanAr['+ i + '],tJSClean));}';// initialise
   }
   insert = insert.substring(0,insert.length -2);
   var dTPos = '50', dBPos = (orphanAr.length*25 + 155);
   var fDlg =
         "dialog { text: 'AH Script Tidy : " + sName + "', bounds:[100," + dTPos + ",470," + dBPos + "], \
         allBtn: Button { text:'Delete All', bounds:[10,10,100,30]}, \
         selBtn: Button { text:'Del Selected', bounds:[110,10,200,30]}, \
         canBtn: Button { text:'Cancel', bounds:[210,10,270,30]}, \
         bu: Checkbox { text:'New File', value:'true', bounds:[280, 10, 365, 30]}, \
         rComBtn: Button { text:'Just De-Comment', bounds:[10,35,200,55]}, \
         rComCB: Checkbox { text:'De-Comment and Del', bounds:[210, 35, 365, 55]}, "
         + insert
         + "\ } ";
   var st = new Window(fDlg);
   fDAr.bu = true;fDAr.rComDel = false;
   st.bu.onClick = function () {fDAr.bu = st.bu.value;}
   st.allBtn.onClick = function () {st.close();fDAr.all = true;}
   st.selBtn.onClick = function () {
      for (var j = 0; j < fDAr.del.length; j++) {
         if (fDAr.del[j]) {fDAr.selAr.push(orphanAr[j]);}
      }
      st.close();fDAr.sel = true;
   }
   st.rComBtn.onClick = function () {st.close();fDAr.rComOnly = true;}
   st.rComCB.onClick = function () {fDAr.rComDel = st.rComCB.value;}
   eval(f0String);
   eval (fString);
   eval(vBtnString);
   var doReview = st.show();
   if (doReview > 0) fDAr.quit = true;   
   return fDAr;   
}

Andrew