14 Replies Latest reply on Sep 27, 2010 9:11 AM by Kasyan Servetsky

    [JS][CS5] switching between CS5 apps

    Roy Marshall Level 1

      Hi

       

      My script starts out in InDesign, but I need to trigger Photoshop at one point in the script.  Is there a way to do this other than do script to make AS launch PS and pass back to JS for me to carry on using JS in PS?

       

      If not, I am trying this:

      app.doScript(File("~/Desktop/triggerPS.applescript"), ScriptLanguage.APPLESCRIPT_LANGUAGE);

      which seems to work ok, but my AS script:

           tell application "Adobe Photoshop CS5"

           set myFile to ("Macintosh HD:Users:atlasdemo:Desktop:photoshop.jsx")

           do script alias myFile language javascript

           end tell

      wont even compile, saying:

      Expected end of line, etc. but found “script”.

      Can anyone let me know what I am doing wrong, or is there a better way?

       

      Cheers

       

      Roy

        • 1. Re: [JS][CS5] switching between CS5 apps
          Kasyan Servetsky Level 5

          You can do this via BridgeTalk. Here is an example of such a script. I am working on a new version of this script which works in CS5 -- if you are interested, I can give you an intermediate version of it.

           

          Kasyan.

          • 2. Re: [JS][CS5] switching between CS5 apps
            Roy Marshall Level 1

            Hi Kasyan

             

            I dont have Bridge installed, will that matter?  This is the sort of script I was wanting to write, although possibly a little less advanced!!

             

            If I dont need Bridge, I would love a copy of the intermediate version if you dont mind

             

            Thanks alot,

             

            Roy

            • 3. Re: [JS][CS5] switching between CS5 apps
              Kasyan Servetsky Level 5

              Don't confuse Bridge application with BridgeTalk class. You don't need Bridge running on your system to use BridgeTalk.

              You can also find more simple but more versatile examples in Bridge CS3 SDK: SnpSendMessageToInDesign.jsx, SnpSendMessage.jsx, SnpSendMessage.jsx, SnpSendArray.jsx, SnpSendCustomObject.jsx, SnpSendDOMObject.jsx.

               

              Here I posted the script. (Warning! It's work in progress).

               

              Kasyan

               

              P.S. Here is a very simplified version of the script -- processes only TIFs in RGB mode:

              #target indesign
              myDocument = app.activeDocument;
              openFiles();

              function openFiles(){
                   for(var myCounter = 0; myCounter < myDocument.links.length; myCounter++){
                   var myLink = myDocument.links[myCounter];
                        if (myLink.linkType == "TIFF"){
                             var myImage = myLink.parent;
                             var myHorScale = myImage.horizontalScale;
                             var myVerScale = myImage.verticalScale;
                             if (myImage.space == "RGB") {
                                  myPath = myLink.filePath;
                                  ResizeInPS(myPath, myHorScale, myVerScale);
                             }
                        }
                   }
              }

              function ResizeInPS(myPath, myHorScale, myVerScale) {
                   var bt = new BridgeTalk;
                   bt.target = "photoshop";
                   var myScript = 'var myPsDoc = app.open(File(\"' + myPath + '\")); \n';
                   myScript += 'app.preferences.rulerUnits = Units.PERCENT; \n' ;
                   myScript += 'myPsDoc.resizeImage(' + myHorScale +', ' + myVerScale + ', myPsDoc.resolution, ResampleMethod.BICUBIC);' ;
                   bt.body = myScript;
                   bt.send();
              }

              • 4. Re: [JS][CS5] switching between CS5 apps
                Roy Marshall Level 1

                Yeah! Thats the sort of answer I was looking for.

                 

                I will play with this, and adapt as required. Many thanks for the help with this one.

                 

                Roy

                • 5. Re: [JS][CS5] switching between CS5 apps
                  Roy Marshall Level 1

                  One other question, this is the first I have heard of Bridge Talk.  Where can I find more info on how to use it, (classes, functions, etc...)?

                   

                  Thanks Again.

                   

                  Roy

                  • 6. Re: [JS][CS5] switching between CS5 apps
                    Roy Marshall Level 1

                    Dont worry, I found the chapter in the JavaScript_Tools_Guide_CS4.pdf

                     

                    If there is anything more up to date that would be good to know, but I will start here.

                     

                    Cheers

                     

                    Roy

                    • 7. Re: [JS][CS5] switching between CS5 apps
                      Roy Marshall Level 1

                      HI

                       

                      I now have had the time to work on this script.

                       

                      I am trying to save the PS file, into a folder that has been selected. This script works OK on its own, but as part of the BridgeTalk, the save fails.

                       

                      Any ideas why this is happening? I dont seem to be getting any errors, just the save not happening.

                       

                      Thanks again

                       

                      Roy

                           var myScript = 'var myPsDoc = app.open(File(\"' + myPath + '\")); \n';

                           myScript += 'app.preferences.rulerUnits = Units.PERCENT; \n' ;

                           myScript += 'myPsDoc.resizeImage(' + myHorScale +', ' + myVerScale + ', myPsDoc.resolution, ResampleMethod.BICUBIC);\n' ;

                           myScript += 'myPsDoc.saveAs (myFolder, undefined, false, undefined);\n' ;

                           myScript += 'myPsDoc.close();' ;

                           bt.body = myScript;

                           bt.send();

                      • 8. Re: [JS][CS5] switching between CS5 apps
                        Kasyan Servetsky Level 5
                        myScript += 'myPsDoc.saveAs (myFolder, undefined, false, undefined);\n' ;
                        

                        The 1st parameter of saveAs should be File, not Folder. myFolder variable is not defined -- neither in myScript string, nor passed as parameter.

                         

                        Below is a snippet from my script to show you example how to use saveAs method:

                         

                        // if it's TIF and theNewFile is undefined just save it

                        if (myLinkType == "TIFF" && theNewFile == "undefined") {
                             myPsDoc.save();
                             myPsDoc.close(SaveOptions.DONOTSAVECHANGES);
                        }
                        else { // otherwize save as TIF using new name
                             var newTifFilePath = (theOldFile == "undefined") ? theOldFile : theNewFile;
                             var myTiffSaveOptions = new TiffSaveOptions();
                             myTiffSaveOptions.imageCompression = TIFFEncoding.NONE;
                             myPsDoc.saveAs(new File(newTifFilePath), myTiffSaveOptions, true);
                             myPsDoc.close(SaveOptions.DONOTSAVECHANGES);
                        }

                         

                        It would be better if you'd post the whole script marking it with Java highlighting.

                         

                        Kasyan

                        • 9. Re: [JS][CS5] switching between CS5 apps
                          Roy Marshall Level 1

                          Hey.

                           

                          Here is the entire script in progress.

                          You will see that myFolder was defined, and passed to the function:

                           

                          #target indesign
                          myDocument = app.activeDocument;
                          var myFolder = Folder.selectDialog ("Select the Output Folder for the resized images");
                          openFiles();
                          function openFiles(){
                               for(var myCounter = 0; myCounter < myDocument.links.length; myCounter++){
                               var myLink = myDocument.links[myCounter];
                                    if (myLink.linkType == "Photoshop"){
                                         var myImage = myLink.parent;
                                         var myHorScale = myImage.horizontalScale;
                                         var myVerScale = myImage.verticalScale;
                                         if (myImage.space == "CMYK") {
                                              myPath = myLink.filePath;
                                              ResizeInPS(myPath, myHorScale, myVerScale,myFolder);
                                         }
                                    }
                               }
                          }
                          
                          function ResizeInPS(myPath, myHorScale, myVerScale,myFolder) {
                               var bt = new BridgeTalk;
                               bt.target = "photoshop";
                               var myScript = 'var myPsDoc = app.open(File(\"' + myPath + '\")); \n';
                               myScript += 'app.preferences.rulerUnits = Units.PERCENT; \n' ;
                               myScript += 'myPsDoc.resizeImage(' + myHorScale +', ' + myVerScale + ', myPsDoc.resolution, ResampleMethod.BICUBIC);\n' ;
                               myScript += 'myPsDoc.saveAs (myFolder, undefined, false, undefined);\n' ;
                               myScript += 'myPsDoc.close();' ;
                               bt.body = myScript;
                               bt.send();
                          }
                          

                           

                          I will look at your answer to see what I should do. However, there are parts that are beyond me, so may take a while!!

                               var newTifFilePath = (theOldFile == "undefined") ? theOldFile : theNewFile;
                          

                           

                          Thanks again for the quick reply,

                           

                          Roy

                          • 10. Re: [JS][CS5] switching between CS5 apps
                            Muppet Mark-QAl63s Level 4

                            Roy, myFolder is a object which if I recall correctly in bridgetalk is considered a complex type so you should use toSource() & eval() with this otherwise pass myFolder as a string and have the folder constructed on the other side like the myPath File example by Kasyan…

                             

                            Also give Photoshop some closing options… As Kasyan's example… Else Photoshop may be error and you are not catching that…

                            • 11. Re: [JS][CS5] switching between CS5 apps
                              Kasyan Servetsky Level 5

                              Roy, I have to leave right now -- will answer you later in a few hours.

                               

                              Kasyan

                              • 12. Re: [JS][CS5] switching between CS5 apps
                                Roy Marshall Level 1

                                Hi

                                 

                                On an un-related matter(ish), why in some cases are file paths shown as "xxx/xxxx/xxxx.xxx", and others as "xxxx:xxxx:xxxx.xxx"

                                When I asked for a folder to be selected, the ":" was used as the delimiter, but the file path is shown with the delimiter "/"

                                 

                                What are the roles around this?

                                 

                                Cheers

                                 

                                Roy

                                • 13. Re: [JS][CS5] switching between CS5 apps
                                  Muppet Mark-QAl63s Level 4

                                  Roy, my Photoshop choked on your resize line so the image opened but then nothing was happening… Anyway I changed and this was fine.

                                  Went for unit values so you don't need to change the user's prefs…

                                   

                                   

                                  #target indesign
                                  
                                  myDocument = app.activeDocument;
                                  
                                  myFolder = Folder.selectDialog ("Select the Output Folder for the resized images");
                                  
                                  openFiles();
                                  
                                  function openFiles(){
                                       for(var myCounter = 0; myCounter < myDocument.links.length; myCounter++){
                                            var myLink = myDocument.links[myCounter];
                                            //$.writeln(myLink.linkType);
                                            if (myLink.linkType == "Photoshop"){
                                                 var myImage = myLink.parent;
                                                 var myHorScale = myImage.horizontalScale;
                                                 var myVerScale = myImage.verticalScale;
                                                 //$.writeln(myImage.space);
                                                 if (myImage.space == "CMYK") {
                                                      myPath = myLink.filePath;
                                                      //$.writeln(myPath);
                                                      ResizeInPS(myPath, myHorScale, myVerScale, myFolder);
                                                 }
                                            }
                                       }
                                  }
                                   
                                  function ResizeInPS(myPath, myHorScale, myVerScale,myFolder) {
                                       var bt = new BridgeTalk;
                                       bt.target = "photoshop";
                                       var myScript = 'var myPsDoc = app.open(File("'+myPath+'"));\n';
                                       myScript += 'myPsDoc.resizeImage(UnitValue('+myHorScale+',"%"), UnitValue('+myVerScale+',"%")'+', myPsDoc.resolution, ResampleMethod.BICUBIC);\n';
                                       myScript += 'var newPath = File("'+myFolder+'/"'+'+myPsDoc.name);\n';
                                       myScript += 'myPsDoc.saveAs(newPath, undefined, false, undefined);\n' ;
                                       myScript += 'myPsDoc.close(SaveOptions.DONOTSAVECHANGES);';
                                       bt.body = myScript;
                                       //$.writeln(myScript);
                                       bt.send();
                                  }
                                  

                                   

                                  Removed a redundant line… doh!

                                  • 14. Re: [JS][CS5] switching between CS5 apps
                                    Kasyan Servetsky Level 5

                                    Here is my version of the script -- tried to make it as simple as possible (make sure to read comments):

                                    #target indesign

                                    var myDoc = app.activeDocument;
                                    var myFolder = Folder.selectDialog ("Select the Output Folder for the resized images");

                                    SetDisplayDialogs("NO");
                                    OpenFiles();
                                    SetDisplayDialogs("ALL");
                                    UpdateAllOutdatedLinks();
                                    alert("Done");

                                    function OpenFiles(){
                                         for (var i = myDoc.links.length-1; i >= 0; i--) { // process links collection backwards since it's changing!
                                              var myLink = myDoc.links[i];
                                               
                                              if (myLink.linkType == "Photoshop"){
                                                   var myImage = myLink.parent;

                                                           var myHorScale = Math.abs(myImage.absoluteHorizontalScale);
                                                        var myVerScale = Math.abs(myImage.absoluteVerticalScale);
                                                        
                                                   if (myImage.space == "CMYK") {
                                                        var myImagePath = myLink.filePath;
                                                             var myNewPath = File(myFolder + "/" + myLink.name);
                                                             if (myHorScale != 100 || myVerScale != 100) { // resize only images with scaling not set to 100%
                                                                  CreateBridgeTalkMessage(myImagePath, myNewPath, myHorScale, myVerScale, 300);
                                                                  SetLink100percent(myLink, myNewPath);
                                                             }
                                                   }
                                              }
                                         }
                                    }
                                    //--------------------------------------------------------------------------------------------------------------
                                    function CreateBridgeTalkMessage(myImagePath, myNewPath, myHorScale, myVerScale, myResolution) {
                                         var bt = new BridgeTalk();
                                         bt.target = "photoshop";
                                         var myScript = ResizeInPS.toString() + "\r";
                                         myScript += "ResizeInPS(\"" + myImagePath + "\", \"" + myNewPath + "\", \"" +  myHorScale + "\", \"" + myVerScale + "\", \"" + myResolution + "\");";
                                         bt.body = myScript;
                                         // if something doesn't work, uncomment the following two lines -- the PS script will be written into console, copy it and dubbug it as a stand alone script in PS
                                    //~      $.write(myScript);
                                    //~      exit();
                                         // the following two lines are very important -- they force ID to wait until PS finishes resizing, saving and closing the image (this may take hours)
                                         bt.onResult = function(resObj) {}
                                         bt.send(100);
                                    }
                                    //--------------------------------------------------------------------------------------------------------------
                                    function ResizeInPS(myImagePath, myNewPath, myHorScale, myVerScale, myResolution) {
                                         var myPsDoc = app.open(new File(myImagePath));
                                         var docName = myPsDoc.name;
                                         
                                         try {
                                              if (myHorScale == "undefined") {
                                                   var myHorScale = undefined;
                                              }
                                              else {
                                                   var myHorScale = eval(new UnitValue(myHorScale, "%"));
                                              }
                                         
                                              if (myVerScale == "undefined") {
                                                   var myVerScale = undefined;
                                              }
                                              else {
                                                   var myVerScale = eval(new UnitValue(myVerScale, "%"));
                                              }

                                         myPsDoc.resizeImage(myHorScale, myVerScale, eval(myResolution), ResampleMethod.BICUBIC);
                                         
                                         myPsDoc.saveAs(new File(myNewPath), undefined, true);
                                         myPsDoc.close(SaveOptions.DONOTSAVECHANGES);
                                         }
                                         catch (err) {
                                              try {
                                                   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                                              }
                                              catch (err) {}
                                         }
                                    }
                                    //--------------------------------------------------------------------------------------------------------------
                                    function SetLink100percent(myLink, myNewPath) { // relink to new file and set exactly to 100%
                                         var newFile = new File (myNewPath);
                                         if (myNewPath != undefined && newFile.exists) {
                                              var originalLinkFile = new File(myLink.filePath);
                                              myLink.relink(newFile);
                                              
                                              try { // for versions prior to 6.0.4
                                                   var myLink = myLink.update();
                                              }
                                              catch(err) {}
                                              
                                              var myImage = myLink.parent;
                                              // set to exactly 100% if the image is not flipped
                                              if (myImage.absoluteHorizontalScale > 0 && myImage.absoluteVerticalScale > 0) {
                                                   myImage.absoluteHorizontalScale = 100;
                                                   myImage.absoluteVerticalScale = 100;
                                              }
                                         }
                                    }
                                    //--------------------------------------------------------------------------------------------------------------
                                    function SetDisplayDialogs(Mode) { // turn on-off DialogModes in PS -- don't want to see the open file dialog while the script is running
                                         var bt = new BridgeTalk;
                                         bt.target = "photoshop";
                                         var myScript = "app.displayDialogs = DialogModes." + Mode + ";";
                                         bt.body = myScript;
                                         bt.send();
                                    }
                                    //--------------------------------------------------------------------------------------------------------------
                                    function UpdateAllOutdatedLinks() {
                                         for (var myCounter = myDoc.links.length-1; myCounter >= 0; myCounter--) {
                                              var myLink = myDoc.links[myCounter];
                                              if (myLink.status == LinkStatus.linkOutOfDate) {
                                                   myLink.update();
                                              }
                                         }
                                    }

                                     

                                    Notice that I write ResizeInPS as an ordinary function and convert it to string:

                                    var myScript = ResizeInPS.toString() + "\r";

                                     

                                    It's much more convenient than this myScript += "blah-blah-blah...\n"; stuff (btw, this tip was given by Harbs and Bob Stucky, thanks them for it).

                                     

                                    The script was written on CS3 for Windows -- can't test it on CS5 so far.

                                    There is an important setting for the script to work correctly: Preferences > File Handling > Preserve Image Dimensions When Relinking should be OFF in CS5. But Relink Preserves Dimensions should be ON in CS3

                                     

                                    On an un-related matter(ish), why in some cases are file paths shown as "xxx/xxxx/xxxx.xxx", and others as "xxxx:xxxx:xxxx.xxx"

                                    When I asked for a folder to be selected, the ":" was used as the delimiter, but the file path is shown with the delimiter "/"

                                     

                                    What are the roles around this?

                                    "xxxx:xxxx:xxxx.xxx" is Mac OS 9 path name

                                    "xxx/xxxx/xxxx.xxx" can be either Mac OS X  or URI path name

                                    You can read about it in the File System Access chapter of JavaScript Tools Guide.

                                     

                                    Hope this helps.

                                     

                                    Kasyan