18 Replies Latest reply on Apr 10, 2013 12:50 PM by Gustavo Del Vechio

    Continue a script in Photoshop

    Gustavo Del Vechio Level 3

      Hi

       

      I need to write a script that export artboards as PSD then open these files in Photoshop in order to continue the processing.

       

      So here´s basically the concept:

       

      #target illustrator

       

      ...body of the code to be executed in Illustrator, concluding with the document.exportFile (...) method; //it´s perfect up here. It generates the PSD properly.

       

      if (app.activeDocument.artboards.length == 1){ //if the Ai File had only one artboard then I know only 1 PSD file was exported. So I need to open this document and continue in Photoshop

       

      ...execute this first code in Photoshop.

       

      };

      else

      if (app.activeDocument.artboards.length > 1){

       

      ...execute this second code inn Photoshop

       

      };

       

      The problem is. How it could change the target application in the middle of the script? (this case, inside an IF loop)?

       

      I tried to read about BridgeTalk but perhaps I did not understand it right. Should I send the code portion as a "message" to Photoshop?? I´m having no luck doing this

       

      Best Regards

      Gustavo.

       


        • 1. Re: Continue a script in Photoshop
          Muppet Mark Level 5

          If you need Photoshop as part of your script then yes… the only way is to pass BridgeTalk messages back and forth… Why do you want to process in Photoshop inside the loop…? Can't you just pass a single file object or Array of… to PS when done in AI…? If you put the BT inside the loop you will need to use the BT call backs in order to proceed…

          • 2. Re: Continue a script in Photoshop
            CarlosCanto Adobe Community Professional & MVP

            I second that, just finish the illustrator script, then start with photoshop with 1 or more files

            • 3. Re: Continue a script in Photoshop
              Gustavo Del Vechio Level 3

              Hi Muppet and Carlos

               

              I never used BridgeTalk so  I ha no idea the best way to proceed...

               

              After concluding the Ai process and generating an exported PSD...I´d like to open this PSD and run a set of commands (I made a specific function for what Photoshop needs to run)..

               

              For example...Photoshop should continue:

               

              > Opening the file

              > Convert the ColorMode to RGB then use the Working sRGB profile

              > Apply some filters

              > Save as JPG.

               

              --

              How could I write a message to Photoshop execute my function (let´s say called function psActions(){...};

               

              Thank you a lot

              Best Regards

              Gustavo.

              • 4. Re: Continue a script in Photoshop
                Muppet Mark Level 5

                When I do this I usually include the whole PS script as a single function in my main script… I use function.toString() and concat the function call… If you want to pass complex objects such as File Object or Array then you need to use toSource() when sending… and eval( ) in the receiving app. Here is a sample that I posted only the other week in the Bridge forum… You only need change the target and the Bridge Function to your AI script and it should get you started…

                 

                 

                #target Bridge
                
                checkAppStatus();
                
                function checkAppStatus() {
                
                    var stat = BridgeTalk.getStatus( 'photoshop' );
                
                    switch( stat ) {
                
                        case 'ISNOTINSTALLED' : alert( 'You don\'t have the app…?' ); break;
                
                        case 'BUSY' : alert( 'The app is NOT responding…?' ); break;
                
                        default : processInPS(); // All other results are most likely good to go…
                
                    };
                
                };
                
                // My Bridge function
                function processInPS() {
                
                     var i, count, doc, file, res, script;
                
                     doc = app.document;
                
                     count = doc.visibleThumbnailsLength;
                
                     for ( i = 0; i < count; i++ ) {
                
                          file = doc.visibleThumbnails[i].spec.toSource(); // Here is my complex File Object passed as function argument
                
                          res = sendBT( 'photoshop', psAction, 3, file );
                
                          //alert( res);
                
                     };
                
                };
                
                // My Generic Bridgetalk function
                function sendBT( tarapp, script, delay, file ) {
                
                     BridgeTalk.bringToFront( tarapp );
                
                     var result = null, bt = new BridgeTalk();
                
                     bt.body = script + '\r' + script.name + '( ' + file + ' );';
                
                     bt.target = tarapp;
                
                     bt.timeout = delay;
                
                     bt.onError = function( btErr ) { result = btErr.body };
                
                     bt.onResult = function( btRes ) { result = btRes.body  };
                
                     bt.onTimeout = function( btOut ) { result = btOut.body  };
                
                     bt.send( delay );
                
                     return result;
                
                };
                
                // My Phoptoshop script in function wrapper
                function psAction( file ) {
                
                     app.displayDialogs = DialogModes.NO;
                
                     var doc = app.open( eval( file ) ); // Here we eval the complex object…
                
                     app.doAction( 'Elmo', 'Muppets');
                
                     doc.close( SaveOptions.SAVECHANGES );
                
                     app.displayDialogs = DialogModes.ALL;
                
                     return true;
                
                };
                
                • 5. Re: Continue a script in Photoshop
                  Gustavo Del Vechio Level 3

                  Hi Muppet

                   

                  I´m trying to read the documentation and chapter about BridgeTalk but it´s

                  unclear for me. Could you help me a little more?

                   

                  Here´s an example of function that should be sent as message to Photoshop

                  execute:

                   

                  function psExecute (arg) { //the argument will be a File stored in a variable called "f".

                     

                      photoshop.open (arg);       

                      var doc = app.activeDocument;

                      doc.flatten();

                      doc.convertProfile ("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC , true, true);

                      doc.resizeImage (UnitValue(largura, "px"), UnitValue(altura, "px"), 300, ResampleMethod.BICUBIC);

                         

                      var opJPG = new JPEGSaveOptions();

                      opJPG.embedColorProfile = true;

                      opJPG.formatOptions = FormatOptions.STANDARDBASELINE;

                      opJPG.matte = MatteType.WHITE;

                      opJPG.quality = 12;

                         

                      var outputPS = new File ("~/Desktop/File.jpg");       

                         

                      doc.saveAs (outputPS, opJPG, true, Extension.LOWERCASE);       

                     

                  };

                   

                   

                  Now here´s what I´ve already done with BridgeTalk:

                   

                  var f = new File ("~/Desktop/F1.psd");

                   

                  var messenger = new BridgeTalk();

                  messenger.target = "photoshop-60.064";

                  //messenger.body ?????? Here´s the problem. How could I use the toSource method passing the psExecute (f)??

                  messenger.send();

                   

                   

                   

                  Gustavo.

                   

                  Message was edited by: Gustavo Del Vechio

                   

                  Message was edited by: Gustavo Del Vechio

                  • 6. Re: Continue a script in Photoshop
                    CarlosCanto Adobe Community Professional & MVP

                    Hi Gustavo, here is a simple example on how to use a script in a function with arguments

                     

                    #target Illustrator
                    
                    alert('Hello from ' + app.name);
                    
                    var bt = new BridgeTalk;
                    bt.target = "photoshop";    
                    var args = {width:300, height:200};
                    var msg = psExecute + '\rpsExecute(' + args.toSource() + ');';
                    
                    bt.body = msg;
                    bt.send();
                    
                    function psExecute (args) {
                        var obj = eval(args);
                        app.documents.add(obj.width, obj.height);
                    };
                    
                    1 person found this helpful
                    • 7. Re: Continue a script in Photoshop
                      Muppet Mark Level 5

                      Firstly remove your typo… photoshop.open (arg); should be app.open( yourfileObject )… The BridgeTalk class can only pass strings so there are several things you need to be careful with… Such as script comments can become un-terminated after being passed also PC file paths as string if you use the backslash \ all this if you use it will require the proper escaping… Personally I prefer to pass what bridgetalk calls complex… ( I think its less work and probably more reliable )…

                       

                      If you look at my 2 comments you will see…

                       

                      file = doc.visibleThumbnails[i].spec.toSource(); // Here I pack the variable reference to JS source code

                       

                      and my open has an extra method inside it…

                       

                      var doc = app.open( eval( file ) ); // And here I use an extra method to evaluate/unwrap the JS source code

                       

                      Yup it's APITFA that AI script can't export at given res…

                      • 8. Re: Continue a script in Photoshop
                        Gustavo Del Vechio Level 3

                        Hi Carlos and Muppet

                         

                        Thank you a lot for your help.

                         

                        Carlos, I tried your suggestion (well...I assume I have no ideia the meaning of '\rpsExecute(' + args.toSource() + ');'

                         

                        Alhought the script did not generate any error...Photoshop was launched and only the first line of the function - app.open(arg) was executed. Photoshop does not continue along the function.

                         

                        Am I missing anything?

                         

                        Best Regards

                        Gustavo.

                        • 9. Re: Continue a script in Photoshop
                          Muppet Mark Level 5

                          Made a quick couple of edits to the first post it should give you something to work up… Works fine here in CS5… My action just greyscales the image…

                           

                           

                          #target illustrator
                          
                          checkAppStatus();
                          
                          function checkAppStatus() {
                          
                              var stat = BridgeTalk.getStatus( 'photoshop' );
                          
                              switch( stat ) {
                          
                                  case 'ISNOTINSTALLED' : alert( 'You don\'t have the app…?' ); break;
                          
                                  case 'BUSY' : alert( 'The app is NOT responding…?' ); break;
                          
                                  default : processInPS(); // All other results are most likely good to go…
                          
                              };
                          
                          };
                          
                          // My Illustrator function
                          function processInPS() {
                            
                               app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                          
                               var i, artBds, dF, doc, psdFile, psdOpts, res, script;
                          
                               doc = app.activeDocument;
                                               
                               dF = Folder( Folder.desktop + "/AI Artboard PSD's" );
                          
                          
                               if ( !dF.exists ) dF.create();
                            
                               psdOpts = new ExportOptionsPhotoshop();
                            
                               psdOpts.embedICCProfile = true;
                               psdOpts.imageColorSpace = ImageColorSpace.CMYK;
                               psdOpts.resolution = 300;
                                               
                               artBds = doc.artboards;
                          
                               for ( i = 0; i < artBds.length; i++ ) {
                                                         
                                    doc.artboards.setActiveArtboardIndex( i );
                          
                                    psdFile = File( dF.fsName + '/' + doc.artboards[i].name + '.psd' );
                            
                                    doc.exportFile( psdFile, ExportType.PHOTOSHOP ,psdOpts );
                          
                                    res = sendBT( 'photoshop', psAction, 3, psdFile.toSource() );
                          
                                    alert( res); // What comes back from the BT call backs… Here you decide if to continue or break
                          
                               };
                                     
                               app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
                            
                               alert( 'All done…' );
                          
                          };
                          
                          // My Generic Bridgetalk function
                          function sendBT( tarapp, script, delay, file ) {
                          
                               BridgeTalk.bringToFront( tarapp );
                          
                               var result = null, bt = new BridgeTalk();
                          
                               bt.body = script + '\r' + script.name + '( ' + file + ' );';
                          
                               bt.target = tarapp;
                          
                               bt.timeout = delay;
                          
                               bt.onError = function( btErr ) { result = btErr.body };
                          
                               bt.onResult = function( btRes ) { result = btRes.body  };
                          
                               bt.onTimeout = function( btOut ) { result = btOut.body  };
                          
                               bt.send( delay );
                          
                               return result;
                          
                          };
                          
                          // My Phoptoshop script in function wrapper
                          function psAction( file ) {
                          
                               app.displayDialogs = DialogModes.NO;
                          
                               var doc = app.open( eval( file ) );
                          
                               app.doAction( 'Elmo', 'Muppets');
                          
                               doc.close( SaveOptions.SAVECHANGES );
                          
                               app.displayDialogs = DialogModes.ALL;
                          
                               return true;
                          
                          };
                          
                          1 person found this helpful
                          • 10. Re: Continue a script in Photoshop
                            Gustavo Del Vechio Level 3

                            Thank you a lot Muppet and Carlos

                             

                            I´ll try your suggestions. Hope to be able to fit in my code.

                             

                            Best Regards

                            Gustavo.

                            • 11. Re: Continue a script in Photoshop
                              CarlosCanto Adobe Community Professional & MVP

                              post your code, there might be a typo, I used (args), you (arg)?

                              • 12. Re: Continue a script in Photoshop
                                Gustavo Del Vechio Level 3

                                Hi Carlos

                                 

                                Here´s the entire script: http://dl.dropbox.com/u/35273119/Exportar%20para%20PSD%20v1.jsx

                                 

                                Thank you for your support (as ever)!

                                 

                                Best Regards

                                Gustavo.

                                • 13. Re: Continue a script in Photoshop
                                  CarlosCanto Adobe Community Professional & MVP

                                  Carlos, I tried your suggestion (well...I assume I have no ideia the meaning of '\rpsExecute(' + args.toSource() + ');'

                                  this is key to understand, objects and scripts need to be serialized (as string) in order to be able to be sent via BridgeTalk

                                   

                                  **********************

                                   

                                  in your photoshop function:

                                   


                                  documentoAtivo.resizeImage (UnitValue(largura, "px"), UnitValue(altura, "px"), 300, ResampleMethod.BICUBIC);

                                   

                                  largura and altura don't exist, so your code stops here.

                                   

                                   

                                  var saidaPS = new File (caminho + "/" + nomeDocumento + ".jpg");  
                                  

                                   

                                  caminho and nomeDocument don't exist either, but you don't get to this line, the code stopped executing in the line I pointed out above.

                                  • 14. Re: Continue a script in Photoshop
                                    Gustavo Del Vechio Level 3

                                    Hi Carlos

                                     

                                    Thank you for your inputs.

                                     

                                    Haha, largura and altura really does not exist. I copied directlly this line to another script I wrote and forgot to change this.

                                     

                                    It should be: documentoAtivo.resizeImage (UnitValue(documentoAtivo.width, "px"), UnitValue(documentoAtivo.height, "px"), 300, ResampleMethod.BICUBIC);

                                     

                                    But changing this did not solve the problem. Photoshop function continues to not execute.

                                     

                                    --

                                     

                                    And caminho and nomeDocumento, this exists. But it was declarated outside the function (see lines 47 and 48 of the script). These variables was strings that used early in the portion of the code executed by Illustrator. Then I´d continue these string inside Photoshop portion. Should I declarate again inside the function?

                                     

                                     

                                     

                                    Best Regards

                                    Gustavo.

                                    • 15. Re: Continue a script in Photoshop
                                      CarlosCanto Adobe Community Professional & MVP

                                      yes you have to declare them inside your PS function, remember, that function is going to be serialized into a string and be sent to photoshop separately to execute...check and practice with the sample I posted above.

                                      • 16. Re: Continue a script in Photoshop
                                        Gustavo Del Vechio Level 3

                                        Perfect Carlos

                                         

                                        After declaring the variables again and correting two more mistakes, it worked perfectlly!

                                         

                                        Here´s the final script version: http://dl.dropbox.com/u/35273119/Exportar%20para%20PSD%20v1.jsx

                                         

                                        Just for curiosity: What means this:

                                         

                                        psExecute + '\rpsExecute(' + args.toSource() + ');';

                                         

                                         

                                        Why the + "\r"? Looking for a carriage return????

                                         

                                        Best Regards

                                        Gustavo

                                        • 17. Re: Continue a script in Photoshop
                                          CarlosCanto Adobe Community Professional & MVP

                                          great, yes,

                                           

                                          psExecute returns your function as string

                                           

                                          "psExecute (args) {......}"

                                           

                                          then we add carriage return

                                           

                                          "\r"

                                           

                                          and

                                           

                                          the line to call the function

                                           

                                          "psExecute (args.toSource());"

                                           

                                          so the whole string is something like this

                                           

                                          "psExecute (args) {

                                          ......

                                          }

                                          psExecute (args.toSource());"

                                          • 18. Re: Continue a script in Photoshop
                                            Gustavo Del Vechio Level 3

                                            Very good!

                                             

                                            Thank you Carlos