Need help with a Bridge script

General Discussion of Scripting for Adobe Bridge

Moderators: Tom, Kukurykus

Mike Hale

Need help with a Bridge script

Post by Mike Hale »

I don't do much Bridge scripting but I need to create a Bridge script that will allow a user to edit the html file created by the Bridge output module.

The user selects a folder in Bridge. That folder can either be a web gallery or a folder that contains multiple gallery folders. A dialog should popup to allow the user to choose one of 4 different names that will be used to edit the html files.

The script below works in Photoshop if I hard code the selected folder( and comment out the Bridge only commands ) but doesn't work in Bridge. Does anyone see anything wrong with what I have?

Code: Select all#target bridge

editWebGalleries = {};
editWebGalleries.repsArray = ['DuBose','Jane','Kirsten','Trisha'];
editWebGalleries.repsCodeFolder = new Folder('/f/code');
editWebGalleries.createDialog = function ( ) {
   var dlg = new Window( 'dialog', 'Choose a rep for the gallery(ies)' );
   //dlg.stFolder = dlg.add('statictext',undefined,this.folder);
   dlg.ddNames = dlg.add('dropdownlist',undefined,this.repsArray);
   dlg.ddNames.items[0].selected = true;
   dlg.ddNames.preferredSize.width = 80;
   dlg.btnPnl = dlg.add( 'panel', undefined );
   dlg.btnPnl.orientation = "row";
   dlg.btnPnl.alignment = "right";
   dlg.btnPnl.preferredSize [ 80, 80 ]
   dlg.btnPnl.okBtn = dlg.btnPnl.add( 'button', undefined, 'Ok', { name:'ok' });
   dlg.btnPnl.cancelBtn = dlg.btnPnl.add( 'button', undefined, 'Cancel', { name:'cancel' });
   return dlg;
};
editWebGalleries.initializeDialog = function( w ) {
   // Set up initial control states
   with ( w.btnPnl ) {
      // The Ok and Cancel buttons close this dialog
      okBtn.onClick = function ( ) { this.parent.parent.close( 1 ); };
      cancelBtn.onClick = function ( ) { this.parent.parent.close( 2 ); };
   }   
};
editWebGalleries.runDialog = function( w ) {
   return w.show( );
};
editWebGalleries.writeHTMLFile =  function( file, str ){
   file.remove();
   file.open('w');
   file.write(str);
   file.close();
};
editWebGalleries.readFile =  function( file ){
   file.open('r');
   var str = file.read();
   file.close();
   return str;
};
editWebGalleries.getWebFiles = function(folder){
   function getFolders(folder){
      var folders = folder.getFiles(function(file){return file instanceof Folder;});
      for(var f = 0;f<folders.length;f++){
         getWebFiles(folders[f]);
         var webFiles = folders[f].getFiles(/.html$/i);
         while (webFiles.length > 0) {
            arry.push(webFiles.shift());
         }
      }
   }
   if(undefined == arry) {
      var arry = []
   }
   getFolders(folder);
   var webFiles = folder.getFiles(/.html$/i);
   while (webFiles.length > 0) {
      arry.push(webFiles.shift());
   }
   return arry;
};
editWebGalleries.execute = function(){
   if(app.document.selections.length != 1 || !app.document.selections[0].container){
      alert('Please select a shoot or gallery folder\nand try again');
      return;
   }
   var sels = app.document.selections[0].spec;
   this.folder = new Folder(sels);
   var win = this.createDialog();
   this.initializeDialog(win);
   var results = this.runDialog(win);
   if(results==1){
      var webFiles = this.getWebFiles(this.folder);
      var repString = this.readFile( new File('/k/code/'+win.ddNames.selection.text+'.txt'));
      for(var f=0;f<webFiles.length;f++){
         var webString = this.readFile( webFiles[f]);
         webString = webString.replace('\<!-- saved from url=(0013)about:internet --\>\n\<html lang="en"\>',repString);
         this.writeHTMLFile(webFiles[f], webString);
      }
   }
}

// this script only works in bridge
if (BridgeTalk.appName == "bridge"){

  var menu = MenuElement.create( "command", "Edit Web Galleries", "at the end of Tools");
  menu.onSelect = editWebGalleries.execute;
}
Paul MR

Need help with a Bridge script

Post by Paul MR »

Hi Mike, on a quick look I would change:
menu.onSelect = editWebGalleries.execute; to menu.onSelect = editWebGalleries.execute();
It should then run.
Mike Hale

Need help with a Bridge script

Post by Mike Hale »

No joy. When I tried making that change it appears that Bridge tries to run the script at startup and it throws an undefined error for the line 'if(app.document.selections.length != 1 || !app.document.selections[0].container){'

I would have thought that menu.onSelect = editWebGalleries.execute assigns the execute function to onSelect. Where as menu.onSelect = editWebGalleries.execute() would assign the results of that function.

But I'm glad you don't see anything wrong with the basic approach.
Paul MR

Need help with a Bridge script

Post by Paul MR »

Sorry Mike I didn't test it! This should work now...
Code: Select all// this script only works in bridge
if (BridgeTalk.appName == "bridge"){
  var menu = MenuElement.create( "command", "Edit Web Galleries", "at the end of Tools");
  menu.onSelect = function () {
   editWebGalleries.execute();
}
}