13 Replies Latest reply on Oct 8, 2010 2:10 PM by Yuliaart

    Image re-saving in InDesign CS4 js

    Yuliaart Level 1

      Hi,

      I used apple script on Mac to open photoshop link from InDesign in Photoshop re-save it to a different format and replace it back into InDesign, but I can't use it on a pc. What would be good easy alternative to apple script for the pc. Could I use visual basic for it. Or is there a way to script it directly from InDesign. Directly from InDesign would be preferable, because it would work for both.

       

      Thank you for your help.

      Yulia

        • 1. Re: Image re-saving in InDesign CS4 js
          Marijan Tompa [tomaxxi] Level 4

          Hey!

           

          Similar thing was discussed few days ago.

          Thake a look here: http://forums.adobe.com/thread/726921

           

          Hope it helps.

           

          --

          tomaxxi

          http://indisnip.wordpress.com/

          • 2. Re: Image re-saving in InDesign CS4 js
            Yuliaart Level 1

            Thank you.

             

            I did find the code that is almost there in the link you mentioned. I got it adjusted to what I need somewhat, but I don't know much about BridgeTalk, and I am not sure how to make Ph to re-save my files into png files. Here what I have so far:

             

             

            #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 == "RGB") {
                                var myImagePath = myLink.filePath;
                                     var myNewPath = File(myFolder + "/" + myLink.name);
                                     if (myLink.name.indexOf (".png")==-1) { // re-save any ph file, but the png
                                          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();
                      }
                 }
            }
            

             

             

            I suspect that it would by following function that I would need to be changed where ph is doing the work:

             

             

            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);
            }

             

             

             

            Thank you so much for your help.

            Yulia

            • 3. Re: Image re-saving in InDesign CS4 js
              Kasyan Servetsky Level 5

              #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 == "RGB") {
                                  var myImagePath = myLink.filePath;
                                       var myImageFile = new File(myImagePath);
                                       var myNewPath =  myFolder.absoluteURI + "/" + GetFileNameOnly(myImageFile.name) + ".png";
                                       if (myLink.name.indexOf (".png")==-1) { // re-save any ph file, but the png
                                            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);

                   var myPNGSaveOptions = new PNGSaveOptions();
                   myPNGSaveOptions.interlaced = false; // or true
                   myPsDoc.saveAs(new File(myNewPath), myPNGSaveOptions, 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();
                        }
                   }
              }
              //--------------------------------------------------------------------------------------------------------------
              function GetFileNameOnly(myFileName) {
                   var myString = "";
                   var myResult = myFileName.lastIndexOf(".");
                   if (myResult == -1) {
                        myString = myFileName;
                   }
                   else {
                        myString = myFileName.substr(0, myResult);
                   }
                   return myString;
              }
              //--------------------------------------------------------------------------------------------------------------

               

              I don't think that this line makes sense:

              if (myLink.name.indexOf (".png")==-1) { // re-save any ph file, but the png

              since you already checked if the link is psd before

              if (myLink.linkType == "Photoshop"){

              I'd leave it as it used to be

              if (myHorScale != 100 || myVerScale != 100) {

              because you don't need to open images that are already at 100% in PS.

               

              Kasyan

               

              P.S. Written in CS3 for Windows, hope it works in CS4 too.

              • 4. Re: Image re-saving in InDesign CS4 js
                Yuliaart Level 1

                Hi, Kasyan.

                 

                Thank you very much for your help. I don't need to resize images, in fact it is very important to leave them at the size they are, but I need to convert them to pngs, because the program they will be put into could accept png files only. I think the script for most parts outlines what needs to be done. Although I didn't realize it was selecting only psd files. I will need it to go through all the images in InDesign that are not saved in png and re-save them into pngs. And that are the modifications I will need to figure out for the script.

                 

                Thank you for your help.

                Yulia

                • 5. Re: Image re-saving in InDesign CS4 js
                  Kasyan Servetsky Level 5

                  Hi Yulia,

                   

                  Here is a new version of the script:

                  #target indesign

                  var myDoc = app.activeDocument;
                  var myFolder = Folder.selectDialog ("Select the output folder for PNG 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 != "Portable Network Graphics (PNG)"){
                                 var myImage = myLink.parent;
                                 var myImagePath = myLink.filePath;
                                 var myImageFile = new File(myImagePath);
                                 var myNewPath =  myFolder.absoluteURI + "/" + GetFileNameOnly(myImageFile.name) + ".png";
                                 CreateBridgeTalkMessage(myImagePath, myNewPath);
                                 Relink(myLink, myNewPath);
                            }
                       }
                  }
                  //--------------------------------------------------------------------------------------------------------------
                  function CreateBridgeTalkMessage(myImagePath, myNewPath) {
                       var bt = new BridgeTalk();
                       bt.target = "photoshop";
                       var myScript = ResaveInPS.toString() + "\r";
                       myScript += "ResaveInPS(\"" + myImagePath + "\", \"" + myNewPath + "\");";
                       bt.body = myScript;
                       bt.onResult = function(resObj) {}
                       bt.send(100);
                  }
                  //--------------------------------------------------------------------------------------------------------------
                  function ResaveInPS(myImagePath, myNewPath) {
                       try {
                             var myPsDoc = app.open(new File(myImagePath));
                             var docName = myPsDoc.name;

                            var myPNGSaveOptions = new PNGSaveOptions();
                            myPNGSaveOptions.interlaced = false; // or true
                            myPsDoc.saveAs(new File(myNewPath), myPNGSaveOptions, true);
                            myPsDoc.close(SaveOptions.DONOTSAVECHANGES);     
                       }
                       catch (err) {
                            try {
                                 app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                            }
                            catch (err) {}
                       }
                  }
                  //--------------------------------------------------------------------------------------------------------------
                  function Relink(myLink, myNewPath) {
                       var newFile = new File (myNewPath);
                       if (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) {}
                       }
                  }
                  //--------------------------------------------------------------------------------------------------------------
                  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();
                            }
                       }
                  }
                  //--------------------------------------------------------------------------------------------------------------
                  function GetFileNameOnly(myFileName) {
                       var myString = "";
                       var myResult = myFileName.lastIndexOf(".");
                       if (myResult == -1) {
                            myString = myFileName;
                       }
                       else {
                            myString = myFileName.substr(0, myResult);
                       }
                       return myString;
                  }
                  //--------------------------------------------------------------------------------------------------------------

                  Kasyan

                  1 person found this helpful
                  • 6. Re: Image re-saving in InDesign CS4 js
                    Yuliaart Level 1

                    Oh, this is perfect, thank you very much.

                    • 7. Re: Image re-saving in InDesign CS4 js
                      Yuliaart Level 1

                      I have one more question. There are occasional images that it wouldn't re-save and they are not png(s). It looks like ph fails to open the file, so nothing happens. Do you have any ideas why it could be. I don't see anything in the script that might cause that.

                       

                      Thank you for your help.

                      Yulia

                      • 8. Re: Image re-saving in InDesign CS4 js
                        Yuliaart Level 1

                        Oh, I see why, because ph doesn't provide the option to save as png. Are there specific conditions for file to have a capabilities to be saved as png?

                         

                        Thank you for your help.

                        • 9. Re: Image re-saving in InDesign CS4 js
                          Kasyan Servetsky Level 5

                          Hi Yulia,

                           

                          PNG format supports RGB, Indexed Color, Grayscale, and Bitmap mode images without alpha channels. ... PNG supports 24‑bit images  ...*

                          So the script should check color mode of a link -- it should be one of the above. Or if it's CMYK, Lab, etc., convert it. It should also deal with 32-bit images -- e.g. convert them to 8/16/32 bit.

                           

                          Kasyan

                           

                           

                          * Photoshop CS3 help

                          • 10. Re: Image re-saving in InDesign CS4 js
                            Yuliaart Level 1

                            The images for our new program need to be RGB, but I might have sampled some CMYK images without realizing that that could be an issue. Let me check on it.

                             

                            Thank you very much.

                            • 11. Re: Image re-saving in InDesign CS4 js
                              Yuliaart Level 1

                              Kasyan, since I need to have all the files to be converted to RGB before re-saving them into png(s), how can I add this conversion and would it go somewhere in this portion of the script:

                               

                              function ResaveInPS(myImagePath, myNewPath) {
                                   try {
                                         var myPsDoc = app.open(new File(myImagePath));
                                         var docName = myPsDoc.name;

                                        var myPNGSaveOptions = new PNGSaveOptions();
                                        myPNGSaveOptions.interlaced = false; // or true
                                        myPsDoc.saveAs(new File(myNewPath), myPNGSaveOptions, true);
                                        myPsDoc.close(SaveOptions.DONOTSAVECHANGES);     
                                   }
                                   catch (err) {
                                        try {
                                             app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                                        }
                                        catch (err) {}
                                   }
                              }

                               

                               

                              Thank you so much.

                              Yulia

                              • 12. Re: Image re-saving in InDesign CS4 js
                                Kasyan Servetsky Level 5

                                Here is a new function:

                                function ResaveInPS(myImagePath, myNewPath) {
                                     try {
                                           var myPsDoc = app.open(new File(myImagePath));
                                             if (myPsDoc.mode == DocumentMode.CMYK) {
                                                    myPsDoc.changeMode(ChangeMode.RGB);
                                               }
                                           var docName = myPsDoc.name;

                                          var myPNGSaveOptions = new PNGSaveOptions();
                                          myPNGSaveOptions.interlaced = false; // or true
                                          myPsDoc.saveAs(new File(myNewPath), myPNGSaveOptions, true);
                                          myPsDoc.close(SaveOptions.DONOTSAVECHANGES);     
                                     }
                                     catch (err) {
                                          try {
                                               app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                                          }
                                          catch (err) {}
                                     }
                                }

                                 

                                Kasyan

                                • 13. Re: Image re-saving in InDesign CS4 js
                                  Yuliaart Level 1

                                  Got it, it works perfectly.

                                   

                                  Thank you very much.

                                  Yulia