1 Reply Latest reply on Oct 8, 2014 7:45 AM by Silly-V

    More agile way to use BridgeTalk functions

    Sakari Niittyma Level 1

      I have used BridgeTalk now in my couple scripts. I found this way to more agile way to use functions with BridgeTalk. Just want to share this and get comments if there is more better way to do this.

       

      So, I just get function name and evaluate function itself to BT message body. In this way I just need to call BT message only with one line and make it more dynamic.

       

      // BridgeTalk Message
      function btMsg ( fun_name, parameters ) {
          
          var ref_parameters;
          
          // Test type of parameters
          if (parameters != undefined) {
              if (typeof parameters == 'string' || parameters instanceof String) {
                  ref_parameters = parameters != undefined ? '"' + parameters + '"' : '';
              } else {
                  ref_parameters = parameters;
              }
          } else {
              ref_parameters = '';
          }
          
          // Make BridgeTalk Message
          var bt = new BridgeTalk;
          bt.target = "illustrator";
          var msg = eval(fun_name) + '\r ' + fun_name + '(' + ref_parameters + ');';
          bt.body = msg;
          bt.send();
      }
      
      // GET: Function name. (Use target function name without brackets)
      function getFunctionName ( fun_name ) {
          var ret = fun_name.toString();
          ret = ret.substr('function '.length);
          ret = ret.substr(0, ret.indexOf('('));
          return ret;
      }
      
      // Your Function
      function myFunction( parameter ) {
          $.writeln(parameter);
      }
      
      
      // GUI Constructor
      function GUI() { this.windowRef = null; }  
      
      // GUI Window
      GUI.prototype.run = function()  
      {
          
          // WINDOW: Main Window
          var win = new Window( "palette", script_name, undefined );
          win.orientation = "column";
          win.alignChildren = ["fill", "fill"];
          
          // LISTBOX
          myListbox = win.add ("listbox", undefined, ['Text', 2],  
              {  
                  numberOfColumns: 1,  
                  showHeaders: true,
                  columnTitles: ['Values']
              });
        
          // BUTTON: Hello
          var myButton = win.add("button", undefined, "What Value?");
          myButton.onClick = function() {
              btMsg ( getFunctionName(myFunction), myListbox.selection.text );
          }
      
      
          // BUTTON: Close button
          win.btnQuit = win.add("button", undefined, "Close");
          win.btnQuit.onClick = function() { win.close(); }  
      
      
          // Window Lauch Properties
          win.center();
          win.show(); 
      }
      
      new GUI().run();
      
        • 1. Re: More agile way to use BridgeTalk functions
          Silly-V Adobe Community Professional

          The way AI scripting treats working with palettes makes me appreciate the silent mechanisms behind regular & other app palettes all around us which I take for granted all of the time.

          When I did some BT function work, I think I ended up making a 'main' function to send to via BT , and in that main function I would have replaceable strings which get dynamically replaced by my in-script choices in a string-replace action inside the function which sends the BT message.

          This is some sample code from my sample palette:

           

              function asSourceString(func){
                  return func.toSource().toString().replace("(function "+func.name+"(){","").replace(/}\)$/,"");
              }
              function sendBTmsg(func, updateObj, resultFunc, resultFuncArgs){
                  if(updateObj==undefined){updateObj={self: 'nothing'}};
                  var updateObjName=updateObj.self;
                  if(updateObjName!=undefined){
                      var bt=new BridgeTalk;
                      bt.target='illustrator';
                      var btMsg=asSourceString(func);
                      for(all in updateObj){
                          if(all!='self'){
                              var rx=new RegExp(updateObjName+'\\.'+all,'g');
                              btMsg=btMsg.replace(rx,updateObj[all]);
                          }
                      }
                      bt.body=btMsg;
                      //$.write(btMsg);
                      bt.onResult=function(result){
                          if(resultFunc!=undefined){
                              resultFunc(result.body, resultFuncArgs);
                          }
                      }
                      bt.send();
                  } else {
                      $.writeln("Error, function 'sendBTmsg(func, updateOb)': the update object should have a 'self' property, indentifying its' name.");
                  }
              }
          
              function returnRefresh(){
                  outcome={
                      width: width,
                      height: height,
                      unitsName: unitsName,
                  }.toSource()
              }
              function processRefresh(objString, args){
                  var uiStuff=args[0];
                  var obj=eval(objString);
                  uiStuff.wE.text=obj.width+" "+obj.unitsName;
                  uiStuff.hE.text=obj.height+" "+obj.unitsName;
              }
              var UNITS={
                  self: "UNITS",
                  _in: {ratio: 1/72, name: '"in"'},
                  _mm: {ratio: 0.352777778, name: '"mm"'},
                  _pt: {ratio: 1, name: '"pt"'}
              };
              var INFO={
                  self: "INFO",
                  unitsRatio: UNITS._in.ratio,
                  unitsName: UNITS._in.name,
                  procedure: asSourceString(doNothing),
                  decimals: 3,
                  colortype: '"CMYKColor"',
                  clr_1: 0,
                  clr_2: 100,
                  clr_3: 100,
                  clr_4: 0,
                  arrowType: 1,
              };
              // Main messenger function
          
              function paletteToAI(){
                  outcome=null;
                  if(app.documents.length>0){
                      var doc=app.activeDocument;
                      var sel=doc.selection;
                      if(sel.length>0){
                          var top,left,bottom,right;
                          if(sel.length>1){
                              alert("selection items : "+sel.length);
                          } else {
                              var item=sel[0];
                              left=item.visibleBounds[0];
                              top=item.visibleBounds[1];
                              right=item.visibleBounds[2];
                              bottom=item.visibleBounds[3];
                              var cv=1*INFO.unitsRatio;
                              var decimals=Math.pow(10,INFO.decimals);
                              var pointWidth=-1*(left-right);
                              var pointHeight=top-bottom;
                              var width=Math.round(-1*(left-right)*cv*decimals)/decimals;
                              var height=Math.round((top-bottom)*cv*decimals)/decimals;
                              var unitsName=INFO.unitsName;
                              var proportionFactor=function(){
                                  var num=(pointWidth<pointHeight) ? pointWidth:pointHeight;
                                  if(num>72){
                                      return 1;
                                  } else {
                                      return (num>10) ? num/72:10/72;
                                  }
                              }();
                              INFO.procedure;
                          }
                      } else {
                          alert("Please select an Art Object...");
                          outcome=null;
                      }
                  } else {
                      alert("There isnt an open document!");
                      outcome=null;
                  }
              }
          

           

          //=========================================== Skip down to where this is attached to palette controls... =================//
                  refreshBtn.addEventListener('mousedown',function(){ // REFRESH
                      INFO.unitsRatio=UNITS['_'+unitsDD.selection.text].ratio;
                      INFO.unitsName=UNITS['_'+unitsDD.selection.text].name;
                      INFO.procedure=asSourceString(returnRefresh);
                      INFO.decimals=decimalsDD.selection.text;
                      sendBTmsg(paletteToAI, INFO, processRefresh,[uiStuff]);
                  });
                  specsBtn.addEventListener('mousedown',function(){ // Real Specs
                      INFO.unitsRatio=UNITS['_'+unitsDD.selection.text].ratio;
                      INFO.unitsName=UNITS['_'+unitsDD.selection.text].name;
                      INFO.decimals=decimalsDD.selection.text;
                      INFO.procedure=asSourceString(putSpecs);
                      sendBTmsg(paletteToAI, INFO, processRefresh,[uiStuff]);
                  });
          

          This is the part where it all comes together inside the addEventListeners on particular UI controls.  The INFO object gets updated from all the UI inputs and the sendBTmsg function uses the paletteToAI (main message function) which also uses the INFO object for the INFO.procedure part, which becomes the specific function inside the main function.    The processRefresh and [uiStuff] are there to basically refresh the palette UI based on what happened in the BT procedure using the BT onresult call.