Getting Bridge Selections for Photoshop (also CS equivalent)

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

Moderators: Kukurykus, Tom, xbytor

Andrew

Getting Bridge Selections for Photoshop (also CS equivalent)

Post by Andrew »

This is one way of grabbing Bridge selections from Photoshop - this sample code can be run as a script in Photoshop. Depening on the 'levels' setting it will also scan folders and subfolders in Bridge.

Code: Select allmain ();

function main(){
   var started = new Date();
   var count = 0;
   myData = new Object();
   myData.datFolderSD = new Folder(app.path + '/Presets/ScriptsData');
   myData.datFolder = new Folder(app.path + '/Presets/ScriptsData/AndrewHallScriptData');
   myData.bridgeDat = new File (myData.datFolder + '/ah_bridge_selections.dat');

   var testDat = myData.bridgeDat.modified
   
   if (app.version[0] == '9' && BridgeTalk.isRunning( "bridge")) {
      // second parameter determines depth of folder scan, 2 is unlimited
      ah_collectDataFromBridge (myData.bridgeDat,1);
      while (!(myData.bridgeDat.modified > testDat)) {
         $.sleep(2000);
         if (count > 5 && !confirm("Collecting Bridge Selection Data\n To Continue click Yes")) break;
         count++
      }   
      alertAr(getBridgeSelections(myData));
   }
}

function ah_collectDataFromBridge (datFile,levels) {
   // levels controls scanning of folders selected in Bridge
   // 0: no scan; 1: scan but do not go deeper; 2: no limit on scan depth
   var targetApp,running,bt,sStr;
   targetApp = BridgeTalk.getSpecifier( "bridge", "1");
   running = BridgeTalk.isRunning( "bridge");
   if( targetApp && running) {
      bt = new BridgeTalk;
      bt.target = "bridge";
      sStr = bridgeString(datFile,levels);
      bt.body = sStr;
      bt.send();
   }
}

function ah_getScriptString  (fStr) {
      f = new File (fStr);
      if (f.open('r')) {
         var lsString = f.read();
         return lsString;
      }
      else throw('failed to open script'+f.fsName);   
}

function bridgeString(datFile, levels) {
   return "   ah_CollectBridgeData( '" + datFile + "',"+levels+");\
      function ah_writeData (tFile,tString) {\
         if (tFile.open('w')) {\
            if (tFile.write(tString)) tFile.close();\
            else throw ('Error saving file list ' + tFile);\
         }\
         else throw ('Error, Data File Not Opened ' + tFile); \
      }\
      function ah_CollectBridgeData (fileString, levels) {\
         var tFile = new File (fileString);app.document.refresh;\
         var filestr = ah_fileThumbnailsToFilestr(app.document.selections,levels);\
         ah_writeData(tFile,filestr);\
      }\
      function ah_fileThumbnailsToFilestr ( thumbnails, levels ) {\
         var fileAr = new Array();\
         for ( var i = 0; i < thumbnails.length; i++ ) {\
            if(thumbnails.spec instanceof File) fileAr.push(thumbnails[ i ].spec);\
            else if(thumbnails.spec instanceof Folder && levels > 0) {fileAr.push(scanSubFolders(thumbnails[ i ].spec,levels));}\
         }\
         return fileAr.toString();\
      }\
      function scanSubFolders(tFolder,levels)\
      {\
         var sFolders = new Array();\
         var allFiles = new Array();\
         sFolders[0] = tFolder;\
         for (var j = 0; j < sFolders.length; j++) \
         {\
              var procFiles = sFolders[j].getFiles();\
              for (var i=0;i<procFiles.length;i++) \
              {\
                  if (procFiles instanceof File) allFiles.push(procFiles);\
                  else if (levels > 1 && procFiles instanceof Folder) sFolders.push(procFiles);\
               }\
               if (levels == 1) levels = 0;\
         }\
         return allFiles;\
      }"
}

// reads the dat file which contains the bridge selection info - after the data has been collected
function getBridgeSelections(myData) {
   var bridgeFileString,bridgeFileStringAr,i,procFiles=new Array();
   if (BridgeTalk.isRunning( "bridge")) {
      if (myData.bridgeDat.open('r')) bridgeFileString = myData.bridgeDat.read();
      else throw('failed to open bridge data file '+myData.bridgeDat.fsName);
      if (bridgeFileString.length > 0) bridgeFileStringAr = bridgeFileString.split(',');
      else {alert('There appear to be no selections in Bridge'); return 2;}
      for (i = 0; i < bridgeFileStringAr.length; i++) {
         procFiles = new File (bridgeFileStringAr);
      }      
      return procFiles;
   }
   else {alert('Use Selected Files - Bridge not running');return 2;}         
}

function alertAr (array1) {
   var arrayStr = '';
   for (var i = 0; i < array1.length; i++) {arrayStr += array1 + '\n';}
   alert(arrayStr);   
}

Andrew

Andrew

Getting Bridge Selections for Photoshop (also CS equivalent)

Post by Andrew »

This version works in CS (File Browser selections) and CS2 (Bridge selections):

Code: Select allmain ();

function main(){
   var started = new Date();
   var count = 0;
   myData = new Object();
   myData.datFolderSD = new Folder(app.path + '/Presets/ScriptsData');
   myData.datFolder = new Folder(app.path + '/Presets/ScriptsData/AndrewHallScriptData');
   myData.bridgeDat = new File (myData.datFolder + '/ah_bridge_selections.dat');

   test1 = myData.bridgeDat.modified
   
   if (app.version[0] == '9') {
      if (BridgeTalk.isRunning( "bridge")) {
         // second parameter determines depth of folder scan, 2 is unlimited
         ah_collectDataFromBridge (myData.bridgeDat,1);
         while (!(myData.bridgeDat.modified > test1)) {
            $.sleep(2000);
            if (count > 5 && !confirm("Collecting Bridge Selection Data\n To Continue click Yes")) break;
            count++
         }   
         alertAr(getBridgeSelections(myData));
      }
   }
   else if (app.version[0] == '8') {alertAr(fbSelect(1));}   // fbSelect parameter determines depth of folder scan
}

function fbSelect(levels) {
   // levels controls scanning of folders selected in the File Browser
   // 0: no scan; 1: scan but do not go deeper; 2: no limit on scan depth
   var j,actnR,fbIdentifier,files,exActG,filesL,tDsc,tSelect,tFilesL,tPth,nFiles,i,fof;   
   j = 0;
   actnR = new ActionReference();
   fbIdentifier = stringIDToTypeID( "fileBrowser" );
   actnR.putProperty( charIDToTypeID( 'Prpr' ), fbIdentifier );
   files = new Array();
   actnR.putEnumerated( charIDToTypeID( 'capp' ), charIDToTypeID( 'Ordn' ), charIDToTypeID( 'Trgt' ) );
   exActG = executeActionGet( actnR );
   filesL = charIDToTypeID( 'flst'  );
   tDsc = exActG.getObjectValue( fbIdentifier );
   tSelect = charIDToTypeID( 'fsel' );
   tFilesL = tDsc.getList( filesL );
   tPth = charIDToTypeID( 'Path' );
   nFiles = 0;
   for (i=0; i < tFilesL.count; i++)
   {
      if (tFilesL.getObjectValue(i).getBoolean( tSelect )) {
         fof = tFilesL.getObjectValue(i).getPath( tPth );
         if ( fof instanceof File ) {
            files[nFiles++] = fof;
         }
         else if(fof instanceof  Folder && levels > 0) {files = files.concat(scanSubFolders(fof,levels));}
      }      
   }
   return files;
}

function ah_collectDataFromBridge (datFile,levels) {
   // levels controls scanning of folders selected in Bridge
   // 0: no scan; 1: scan but do not go deeper; 2: no limit on scan depth
   var targetApp,running,bt,sStr;
   targetApp = BridgeTalk.getSpecifier( "bridge", "1");
   running = BridgeTalk.isRunning( "bridge");
   if( targetApp && running) {
      bt = new BridgeTalk;
      bt.target = "bridge";
      sStr = bridgeString(datFile,levels);
      bt.body = sStr;
      bt.send();
   }
}

function ah_getScriptString  (fStr) {
      f = new File (fStr);
      if (f.open('r')) {
         var lsString = f.read();
         return lsString;
      }
      else throw('failed to open script'+f.fsName);   
}

function bridgeString(datFile, levels) {
   return "   ah_CollectBridgeData( '" + datFile + "',"+levels+");\
      function ah_writeData (tFile,tString) {\
         if (tFile.open('w')) {\
            if (tFile.write(tString)) tFile.close();\
            else throw ('Error saving file list ' + tFile);\
         }\
         else throw ('Error, Data File Not Opened ' + tFile); \
      }\
      function ah_CollectBridgeData (fileString, levels) {\
         var tFile = new File (fileString);app.document.refresh;\
         var filestr = ah_fileThumbnailsToFilestr(app.document.selections,levels);\
         ah_writeData(tFile,filestr);\
      }\
      function ah_fileThumbnailsToFilestr ( thumbnails, levels ) {\
         var fileAr = new Array();\
         for ( var i = 0; i < thumbnails.length; i++ ) {\
            if(thumbnails.spec instanceof File) fileAr.push(thumbnails[ i ].spec);\
            else if(thumbnails.spec instanceof Folder && levels > 0) {fileAr = fileAr.concat(scanSubFolders(thumbnails[ i ].spec,levels));}\
         }\
         return fileAr.toString();\
      }\
      function scanSubFolders(tFolder,levels)\
      {\
         var sFolders = new Array();\
         var allFiles = new Array();\
         sFolders[0] = tFolder;\
         for (var j = 0; j < sFolders.length; j++) \
         {\
              var procFiles = sFolders[j].getFiles();\
              for (var i=0;i<procFiles.length;i++) \
              {\
                  if (procFiles instanceof File) allFiles.push(procFiles);\
                  else if (levels > 1 && procFiles instanceof Folder) sFolders.push(procFiles);\
               }\
               if (levels == 1) levels = 0;\
         }\
         return allFiles;\
      }"
}

// reads the dat file which contains the bridge selection info - after the data has been collected
function getBridgeSelections(myData) {
   var bridgeFileString,bridgeFileStringAr,i,procFiles=new Array();
   if (BridgeTalk.isRunning( "bridge")) {
      if (myData.bridgeDat.open('r')) bridgeFileString = myData.bridgeDat.read();
      else throw('failed to open bridge data file '+myData.bridgeDat.fsName);
      if (bridgeFileString.length > 0) bridgeFileStringAr = bridgeFileString.split(',');
      else {alert('There appear to be no selections in Bridge'); return 2;}
      for (i = 0; i < bridgeFileStringAr.length; i++) {
         procFiles = new File (bridgeFileStringAr);
      }      
      return procFiles;
   }
   else {alert('Use Selected Files - Bridge not running');return 2;}         
}

function scanSubFolders(tFolder,levels) {
   var sFolders = new Array();
   var allFiles = new Array();
   sFolders[0] = tFolder;
   for (var j = 0; j < sFolders.length; j++)
   {
        var procFiles = sFolders[j].getFiles();
        for (var i=0;i<procFiles.length;i++)
        {
            if (procFiles instanceof File) allFiles.push(procFiles);
            else if (levels > 1 && procFiles[i] instanceof Folder) sFolders.push(procFiles[i]);
         }
         if (levels == 1) levels = 0;
   }
   return allFiles;
}

function alertAr (array1) {
   var arrayStr = '';
   for (var i = 0; i < array1.length; i++) {arrayStr += array1[i] + '\n';}
   alert(arrayStr);   
}


Andrew