13 Replies Latest reply on Oct 15, 2008 6:00 AM by c.pfaffenbichler

    Opening a document in photoshop and running a ps script

      I'm trying to write a script which will open a link in photoshop (which I have managed to do with Link.editOriginal) and also run a photoshop script after the file is opened. I have looked in online references and the PDF's found on this site, but I can't seem to find a function which will let me run a ps script from inside Indesign. Is there any method of doing this, or am I out of luck?

      Thanks for your time.
        • 1. Re: Opening a document in photoshop and running a ps script
          try67 MVP & Adobe Community Professional
          Which OS and which type of script is it (JS, VB, AScript)?
          • 2. Re: Opening a document in photoshop and running a ps script
            Level 1
            The OS is XP and I'm writing it in JS.

            Thanks.
            • 3. Re: Opening a document in photoshop and running a ps script
              Loic.Aigon Adobe Community Professional
              I tried this but didn't work so good :
              var myscript = File("/c/.../myscript.jx");
              myscript.execute();

              the script is :
              #target photoshop
              alert("hey !");

              in fact it seems that the file is launched but i am stuck with a confirm dialog and can't success passing over it so it's all automatic.
              • 4. Re: Opening a document in photoshop and running a ps script
                try67 MVP & Adobe Community Professional
                I was going to suggest using the File.execute function, but it will only work with VBS, I think.

                In your case you might be able to use a tool I developed. Contact me by email (click on my username for the address) for more info.
                • 5. Re: Opening a document in photoshop and running a ps script
                  Loic.Aigon Adobe Community Professional
                  i but it will only work with VBS, I think.

                  It runs very well in JS except...it's just that as it's ordered to run into photoshop, Windows try to warn against scripts that try to do things by their own.
                  It's like windows wanted to be certain it's not a virus and then wants you validate.

                  But that said, it breaks the automation process. That is what stucks me so far.

                  But I am certain someone here has the key.
                  Loic
                  • 6. Re: Opening a document in photoshop and running a ps script
                    Kasyan Servetsky Level 5
                    May be this will help you this function is called from an ID script and sends a PS script (which resizes an image) via BridgeTalk message to PS.

                    Kasyan
                    //----------------------------
                    function ResizeInPS(myPath, myHorScale, myVerScale, myResolution, myResampleMethod) {
                    
                       var bt = new BridgeTalk;
                       bt.target = "photoshop";
                       var myScript = 'try {\n';
                       myScript += 'var myPsDoc = app.open(File(\"' + myPath + '\"));\n';
                       myScript += 'app.preferences.rulerUnits = Units.PERCENT; \n' ;
                       myScript += 'myPsDoc.resizeImage(' + myHorScale +', ' + myVerScale + ', ' + myResolution + ', ' + myResampleMethod + '); \n' ;
                       myScript += 'myPsDoc.close(SaveOptions.SAVECHANGES);\n';
                       myScript += '}\n';
                       myScript += 'catch (myError) {\n';
                       myScript += 'myPsError += \"myError' + myPath + '\";\n';
                       myScript += '}';
                       bt.body = myScript;
                       bt.send();
                    }
                    • 7. Re: Opening a document in photoshop and running a ps script
                      c.pfaffenbichler Level 8
                      Kasyan, great Script, seems to be just what I was looking for.
                      • 8. Re: Opening a document in photoshop and running a ps script
                        Martin Fischer Level 2
                        Kasyan,

                        great function indeed.

                        I have tried to put it into a script to resize every link of a document.

                        But the script seems to be too quick!
                        The script below seems to update links that haven't been resized and while Photoshop is still working.

                        Is it possible to pause the script until the job in photoshop is completely done?


                        // resizeInPS.jsx
                        //DESCRIPTION: Resizes every link in PhotoShop
                        var theScale = 90; // scaling factor in %
                        var theResolution = 300;
                        var theResampleMethod =  'ResampleMethod.BICUBIC';

                        var myLinks = app.activeDocument.links;
                        for ( i = 0; i < myLinks.length; i++ )
                        {
                        ResizeInPS( myLinks[i].filePath, theScale, theScale, theResolution, theResampleMethod );
                        myLinks[i].update();
                        myLinks[i].parent.fit( FitOptions.FRAME_TO_CONTENT );
                        }

                        function ResizeInPS(myPath, myHorScale, myVerScale, myResolution, myResampleMethod) {
                           var bt = new BridgeTalk;
                           bt.target = "photoshop";
                           var myScript = 'try {\n';
                           myScript += 'var myPsDoc = app.open(File(\"' + myPath + '\"));\n';
                           myScript += 'app.preferences.rulerUnits = Units.PERCENT; \n' ;
                           myScript += 'myPsDoc.resizeImage(' + myHorScale +', ' + myVerScale + ', ' + myResolution + ', ' + myResampleMethod + '); \n' ;
                           myScript += 'myPsDoc.close(SaveOptions.SAVECHANGES);\n';
                           myScript += '}\n';
                           myScript += 'catch (myError) {\n';
                           myScript += 'myPsError += \"myError' + myPath + '\";\n';
                           myScript += '}';
                           bt.body = myScript;
                           bt.send();
                        }


                        Martin
                        • 9. Re: Opening a document in photoshop and running a ps script
                          Martin Fischer Level 2
                          Gerald Singelman told me to check the status of a link in a loop before executing the update()-command.

                          This is the updated version:

                          // resizeInPS04.jsx 
                          //DESCRIPTION: Resizes every link in PhotoShop 
                          var theScale = 150; // scaling factor in % 
                          var theResolution = 72; 
                          var theResampleMethod =  'ResampleMethod.BICUBIC'; 

                          var myChoice = confirm ('Warnung! \rBei Ausfühurng dieses Skriptes werden unwiderruflich alle verknüpften Bild-Dateien verändert. Machen Sie vorher Backups der Dateien.' ); 
                          if (myChoice == false) 
                            exit(); 

                          var myLinks = app.activeDocument.links; 
                          for ( var i = myLinks.length-1; i >= 0; i-- ) 
                          ResizeInPS( myLinks[i].filePath, theScale, theScale, theResolution, theResampleMethod ); 

                          while (myLinks[0].status == LinkStatus.NORMAL )
                          {
                          // think about the weather
                          }

                          myLinks.everyItem().update(); 

                          for ( var x = myLinks.length-1; x >= 0; x-- ) 
                          myLinks[x].parent.fit( FitOptions.FRAME_TO_CONTENT ); 

                          function ResizeInPS(myPath, myHorScale, myVerScale, myResolution, myResampleMethod) {  
                             var bt = new BridgeTalk;  
                             bt.target = "photoshop";  
                             var myScript = 'try {\n';  
                             myScript += 'var myPsDoc = app.open(File(\"' + myPath + '\"));\n';  
                             myScript += 'app.preferences.rulerUnits = Units.PERCENT; \n' ;  
                             myScript += 'myPsDoc.resizeImage(' + myHorScale +', ' + myVerScale + ', ' + myResolution + ', ' + myResampleMethod + '); \n' ;  
                             myScript += 'myPsDoc.close(SaveOptions.SAVECHANGES);\n';  
                             myScript += '}\n';  
                             myScript += 'catch (myError) {\n';  
                             myScript += 'myPsError += \"myError' + myPath + '\";\n';  
                             myScript += '}';  
                             bt.body = myScript;  
                             bt.send();  
                          }


                          Martin
                          • 10. Re: Opening a document in photoshop and running a ps script
                            Kasyan Servetsky Level 5
                            Hi Martin,

                            This is just a small snippet from my script that resizes raster images (currently PSD and TIF, because we dont use other raster formats) and sets them to exactly 100% not like InDesign does: something like 99,89690721649485 or 101,13871724045462 (I dont know why but this annoys me). Im translating it from AS to JS.
                            If you are interested, I can post/send you an unfinished, intermediate version (no need to reinvent the wheel for you). And when I finish it, I am sure to post it for everybody who wants it.

                            Kasyan
                            • 11. Re: Opening a document in photoshop and running a ps script
                              Martin Fischer Level 2
                              Hi Kasyan,

                              thank you for your offer to post me an unfinished version.

                              I am afraid that I would try to complete it by myself.
                              And there is less time to do this. ;-)

                              So I am looking forward to the finished version for every one.

                              I have published my short version in a german forum.
                              And I am sure, that many readers will be thankfull to get your code.

                              Thanks
                              Martin
                              • 13. Re: Opening a document in photoshop and running a ps script
                                Level 1
                                > Is it possible to pause the script until the job in photoshop is completely done?

                                Instead of
                                bt.send()

                                do
                                bt.send(60); // wait for (at most) 60 seconds

                                This forces the sender of the message to wait/block until the receiver has
                                completed processing. You should have the receiver send a response so that the
                                caller can verify that the message has been processed; there doesn't appear to
                                be any type of timeout mechanism or exception in place if the processing takes
                                long than indicated.

                                -X