1 Reply Latest reply on Oct 20, 2010 12:50 PM by [Jongware]

    saving files as jpg from InDesign CS4 js

    Yuliaart Level 1

      Hi, Kasyan.

       

      You originally gavy me this script to re-save files as pngs, and now I need it to re-save them as jpgs instead. I made some changes, but I must have missed something, because it's not working. Could you take a look at it:

       

       

      #target indesign
       
      var myDoc = app.activeDocument;
      var myFolder = Folder.selectDialog ("Select the output folder for JPG 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 != "Joint Photographic Experts Group (JPEG)"){
                     var myImage = myLink.parent;
                     var myImagePath = myLink.filePath;
                     var myImageFile = new File(myImagePath);
                     var myNewPath =  myFolder.absoluteURI + "/" + GetFileNameOnly(myImageFile.name) + ".jpg";
                     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));
                   if (myPsDoc.mode == DocumentMode.CMYK) {
                          myPsDoc.changeMode(ChangeMode.RGB);
                     }
                 var docName = myPsDoc.name;
      
                var myJPGSaveOptions = new JPGSaveOptions();
                myJPGSaveOptions.interlaced = false; // or true
                myPsDoc.saveAs(new File(myNewPath), myJPGSaveOptions, 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;
      }
      

       

       

       

      Thank you.

      Yulia