ignoring hidden OSX files

Discussion of the xtools Toolkit

Moderators:xbytor, DavideBarranca, Paul MR

Post Reply
lukasz
ignoring hidden OSX files

Post by lukasz » Mon Dec 27, 2010 8:56 pm

is there a way to make the get file and folders functions to ignore hidden files like "._name.psd" on OSX it's ok, however when we work on PC it will read these files like it is an actual PSD and will mess up the open process by erroring out. I'm thinking adding a regular expression somewhere but was thinking there is an easier fix maybe?

I posted this in a difference section of the site with a different problem, but here is the code again just in case:

Code: Select all#target photoshop
app.bringToFront()   
// Helper functions for using Action Manager
cTID = function( s ) { return app.charIDToTypeID( s ); };// from Xbytor
sTID = function( s ) { return app.stringIDToTypeID( s ); };
Stdlib = function Stdlib() {};// from Xbytor's xtools: http://sourceforge.net/project/showfile ... _id=207620
Stdlib.convertFptr = function(fptr) {
  var f;
  if (fptr.constructor == String) {
    f = File(fptr);

  } else if (fptr instanceof File || fptr instanceof Folder) {
    f = fptr;

  } else {
    Error.runtimeError(19, "fptr");
  }
  return f;
};
Stdlib.getFolders = function(folder) {
  if (folder.alias) {
    folder = folder.resolve();
  }
  var folders = Stdlib.getFiles(folder,
                                function(f) { return f instanceof Folder; });
  return folders;
};
Stdlib.getFiles = function(folder, mask) {
   var files = [];

   folder = Stdlib.convertFptr(folder);

  if (folder.alias) {
    folder = folder.resolve();
  }
  var getF;
  if (Folder.prototype._getFiles) {
    getF = function(f, m) { return f._getFiles(m); }
  } else {
    getF = function(f, m) { return f.getFiles(m); }
  }

  if (mask instanceof RegExp) {
    var allFiles = getF(folder);
    for (var i = 0; i < allFiles.length; i = i + 1) {
      var f = allFiles;
      if (decodeURI(f.absoluteURI).match(mask)) {
        files.push(f);
      }
    }
  } else if (typeof mask == "function") {
    var allFiles = getF(folder);
    for (var i = 0; i < allFiles.length; i = i + 1) {
      var f = allFiles;
      if (mask(f)) {
        files.push(f);
      }
    }
  } else {
    files = getF(folder, mask);
  }

  return files;
};
Stdlib.findFiles = function(folder, mask) {
     var files = Stdlib.getFiles(folder, mask);
     var folders = Stdlib.getFolders(folder);

     for (var i = 0; i < folders.length; i++) {
       var f = folders;
       var ffs = Stdlib.findFiles(f, mask);
       // files.concat(ffs); This occasionally fails for some unknown reason (aka
       // interpreter Bug) so we do it manually instead
       while (ffs.length > 0) {
         files.push(ffs.shift());
         
         
     
           
         
         
         
       }
     }
     return files;
   };



Folder.prototype.findFiles = function(mask) {
     return Stdlib.findFiles(this, mask);

};
Stdlib.createFolder = function(fptr, interactive) {
  if (!fptr) {
    Error.runtimeError(19, "fptr");  // Bad Argument
  }

  if (fptr.constructor == String) {
    fptr = new Folder(fptr);
  }

  // XXX this needs testing
  if ((!fptr.exists || (fptr.parent && !fptr.parent.exists)) && interactive) {
    var f = (fptr instanceof File) ? fptr.parent : fptr;
    if (!confirm(f.toUIString() + " does not exist. Create?")) {
      return false;
    }
  }

  if (fptr instanceof File) {
    return Stdlib.createFolder(fptr.parent);
  }
  if (fptr.exists) {
    return true;
  }
  if (fptr.parent && !fptr.parent.exists) {
    if (!Stdlib.createFolder(fptr.parent)) {
      return false;
    }
  }
  return fptr.create();
};



var inputFolder = Folder.selectDialog("Select a folder of documents to process");
var files = inputFolder.findFiles(/.psd$/i);// get all jpg and psd file in this folder and subfolders

    ////////////////////////
    // if more than one file
    ////////////////////////


if (files.length > 1) {
     
     /*
      for ( var i = 0; i < files.length; i++){
         var thefile = files;
         var theDir = thefile.replace(/\/.*\.psd/,'');
      }
      var csv = new File(inputFolder+"/serial.txt");
      // open text again
      csv.open("w");
      // write your new serial to it
      csv.writeln(files.absoluteURI);
      // close the file
      csv.close();
     
      */
 }   
   
for( var d = 0; d < files.length; d++ ){

   app.open( files[d] );

   var sourceDoc = app.activeDocument;
   var format = getFormat();
   //app.activeDocument = documents[0]; // close up doc
   var closedoc = app.activeDocument;
         doAction("view", "KKS");
 
  //////////////
  // Dialog box
  //////////////
  function goforit() {
         try{
               
                function ftn1() {
                    var desc766 = new ActionDescriptor();
                        var ref499 = new ActionReference();
                        ref499.putEnumerated( charIDToTypeID('Path'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
                    desc766.putReference( charIDToTypeID('null'), ref499 );
                    desc766.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
                        var desc767 = new ActionDescriptor();
                        desc767.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Rlt'), 0 );
                        desc767.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Rlt'), 0 );
                    desc766.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc767 );
                    desc766.putUnitDouble( charIDToTypeID('Wdth'), charIDToTypeID('#Prc'), 100 );
                    desc766.putUnitDouble( charIDToTypeID('Hght'), charIDToTypeID('#Prc'), 100 );
                    desc766.putBoolean( charIDToTypeID('Lnkd'), true );
                    executeAction( charIDToTypeID('Trnf'), desc766, DialogModes.ALL );
                }; ftn1();               
               
               
                }
         catch(e){return;} 
 
  } goforit();

  function menu() {
     
    var maindialog = new Window( 'dialog', 'Concept Genius' );
    var choices = ['Good','Invert'];

            var panel = maindialog.add('panel');
                panel.text = '          Lets get this bad boy rollin     ';
                panel.margins = 30;

        var dropdown = panel.add("dropdownlist", undefined, choices);
        dropdown.items[0].selected = true;

        var buttons = panel.add('group');
            buttons.orientation = 'row';
            buttons.ok = buttons.add('button',undefined,'ok');

    maindialog.show();
    //setting up cases for the choices above
    switch(Number(dropdown.selection)){
        case 0 : good(); break;
        case 1 : invert(); break;
        default: break;
    }
    //defining actions for each case
    function good() {
            closedoc.close(SaveOptions.DONOTSAVECHANGES);
    }
    function invert() {
       doAction("invert", "KKS");
          closedoc.close(SaveOptions.SAVECHANGES);
    }
  }
  menu();



}//end for?////////////////////////////////////
//  get format
////////////////////////////////////   
function getFormat(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
   if(executeActionGet(ref).hasKey(stringIDToTypeID("format"))){
      return  executeActionGet(ref).getString(stringIDToTypeID("format"));
   }
}

lukasz

ignoring hidden OSX files

Post by lukasz » Tue Dec 28, 2010 4:27 pm

did some reading on regular expressions and think I came up with solution in case someone else runs in to this problem

Code: Select allvar files = inputFolder.findFiles(/^([^.]+)\.psd$/i);/



curious if you see any potential problems

xbytor

ignoring hidden OSX files

Post by xbytor » Tue Dec 28, 2010 11:27 pm

The only problem I see is that while it does prevent leading '.' characters, it cannot handle '.' elsewhere in the filename except for just before the extension.
In other words "this.is.a.file.psd" would fail.

This would cover that case
Code: Select all/^[^.].*\.psd$/i

-X

Post Reply