1 Reply Latest reply on Aug 7, 2012 1:26 AM by michael.shen

    Photoshop BridgeTalk with Includes

    Gregory Todd Smith

      I've been feeding off of discussions on these forums for years, but finally have something worth contributing. I know it's filled with evals, but BridgeTalk is so frustrating to use with Photoshop and you can forget including external libraries... well, I've figured out a work around.

       

      Basically, you use the createBridgeTalkMessage function as it is. You place the entire contents of your script into a container function. Then you call the createBridgeTalkMessage function using the set of variables listed in comments to include as many files with relative paths and the names of the objects (or classes) that you want to use. Unfortunately, you will have to list every class and object that you want to use as a string.

       

      In my case, I use an external config to determine how a palette is used and I need that same external config for a set of custom classes that build templates and proofs. The palette has to be sent as a BridgeTalk message and both need to operate on both Mac and PC.

       

      createBridgeTalkMessage('tester', [{library: 'test2.jsx', objects: ['blah','foo']}], 'photoshop');
      function createBridgeTalkMessage(containerName, externalLibraryArray, adobePlatform){
                // containerName is a string of the name of the function that contains all information for the BridgeTalk.
                // externalLibraryArray is an array of objects of the names of the libraries and objects to be included in the BridgeTalk (i.e.- a Universal Config). 
                // The format of the object is {library: 'name', objects: ['stringName','stringName']}.
                // adobePlatform is the platform to which the BridgeTalk should be sent (i.e.- photoshop or illustrator).
                if(arguments.length == 3){
                          // Has includes.
                          var externalObjects = '';
                          for(x=0; x<externalLibraryArray.length; x++){
                                    eval('// @include ' + externalLibraryArray[x].library);
                                    for(y=0; y<externalLibraryArray[x].objects.length; y++){
                                              externalObjects += 'var ' + externalLibraryArray[x].objects[y] + ' = ' + eval(externalLibraryArray[x].objects[y] + '.toSource()') + '\n';
                                    }
                          }
                          containerName = externalObjects + '\n' + eval(containerName + '.toString()') + '\n' + containerName + '();';
                }else if(arguments.length == 2){
                          // Has no includes.
                          containerName = eval(containerName + '.toString()') + '\n' + containerName + '();';
                }else{alert('Invalid number of arguments are being passed to createBridgeTalkMessage function.')}
                // This replaces all carriage returns, line breaks, escape characters, and single and double quotes
                // with escape codes, so if you have a dialog, palette, or alerts, these characters will load correctly.
                containerName = encodeURIComponent(containerName).replace(/\r/g, "%0d").replace(/\n/g, "%0a").replace(/\\/g, "%5c").replace(/'/g, "%27").replace(/"/g, "%22");
        
                var encodedScript = "var encodedScript = '" + containerName + "';\n";
                encodedScript += "actualScript = decodeURIComponent(encodedScript);\n";
                encodedScript += "eval(actualScript);";
        
                var bt = new BridgeTalk();
                bt.target = arguments[arguments.length - 1];
                bt.body = encodedScript;
                bt.send();
      }
      function tester(){
                for(x in blah){alert(blah[x])}
                for(x in foo){alert(foo[x])}
      }
      
      

       

       

      I hope this helps at least one other person out there.

        • 1. Re: Photoshop BridgeTalk with Includes
          michael.shen

          thank very much!

          I occured the issue as you mentioned above.But for some reason, i can't use your script directly. Can you help me to deal with the issue?

          please see my demo code:

          the problem is the text content contains ' or ".

          this code can't work correctly

          script2 = 'var docWidth = 1650;';

          script2 +='var docHeight = 1650;';

          script2 +="var docName ='Front';";

          script2 +='var docRef = app.documents.add(docWidth,docHeight, 300,docName);';

          script2 +="var myLayer1 = docRef.artLayers.add();";

          script2 +="myLayer1.kind = LayerKind.TEXT;";

          script2 +="myLayer1.textItem.kind = TextType.POINTTEXT;";

          script2 +="myLayer1.textItem.font='News702 BT';";

          script2 +="myLayer1.textItem.size=13;";

          script2 +="var textColor1 = new SolidColor;";

          script2 +="textColor1.rgb.hexValue = 'F8F3E9';";

          script2 +="myLayer1.textItem.color = textColor1;";

          script2 +="myLayer1.textItem.contents = 'abcd\'abd\'ed\"afa\"';";      

          var bt = new BridgeTalk();

          bt.target = "photoshop";

          bt.body = script2;

          bt.send();