11 Replies Latest reply on Apr 4, 2011 5:54 AM by stoereee

    [JS IDCS5] Read Attribute values to specify Image options

    stoereee Level 1

      Hello,

       

      I want to read the attribute values of imported XML to specify image fit options:

       

      # include "glue code.jsx"
      
      main();
      
      function main(){
      
      if (app.documents.length != 0){
      var myDoc = app.activeDocument;
      var myRuleSet = new Array (
      new findObjAttribute("//*")
      );
      
      with(myDoc){
      var elements = xmlElements;
      __processRuleSet(elements.item(0), myRuleSet);
      }
      }
      else{
      alert("You have no document open!");
      exit();
      }
      
      
      function findObjAttribute(XPATH){
      this.name = "findObjAttribute";
      this.xpath = XPATH;
      this.apply = function(myElement, myRuleProcessor)
      {
      with(myElement){
      try {
      var Name=myElement.xmlAttributes.itemByName("fit").value;
               myGraphics = app.documents[0].allGraphics;   
               if (Name == "frameToContent") {  
               for (i = myGraphics.length-1; i > -1; i--)
               myGraphics[i].fit(FitOptions.frameToContent);
               } 
               if (Name == "proportionally") {
               for (i = myGraphics.length-1; i > -1; i--)
               myGraphics[i].fit(FitOptions.proportionally);
               }
               if (Name == "centerContent") {
               for (i = myGraphics.length-1; i > -1; i--)
               myGraphics[i].fit(FitOptions.centerContent);
               }
           } 
      catch(e){};
      }
      return true;
      }
      }
      }
      

       

      This script is looping al xml attributes named "fit" and get the values of it. Then it applies the fitOptions of the last attribute value on all the images within the document.

      Now I want the loops of both the attributes and images are going to loop simultaneously, so that each image get it's own fitOptions!?

       

      Regards, Sjoerd

        • 2. Re: [JS IDCS5] Read Attribute values to specify Image options
          John Hawkinson Level 5
          Now I want the loops of both the attributes and images are going to loop simultaneously, so that each image get it's own fitOptions!?

           

          This sentence is not grammatical. It would be a lot easier to understand what you were asking if you rewrote it.

           

          Also, please supply a SHORT example of the XML this is operating on.

          • 3. Re: [JS IDCS5] Read Attribute values to specify Image options
            stoereee Level 1

            Excuse me for my bad English.

             

            I want that both loops of the XML (attributes) and images are loop simultaneously, so that the first attribute value is assigned to the first image, the second attribute value to the second image and so on.

             

            Example of the XML:

            <root>
            <anchor>
            <image fit="frameToContent" href="image1.jpg"></image>
            </anchor>
            <anchor>
            <image fit="proportionally" href="image2.jpg"></image>
            </anchor>
            <anchor>
            <image fit="centerContent" href="image3.jpg"></image>
            </anchor>
            </root>
            
            • 4. Re: [JS IDCS5] Read Attribute values to specify Image options
              csm_phil Level 4

              Hi Srjod,

               

              I thnk i finished your requirement.


              I have modified your above JS Code as per your requirement and also i checked its working fine,

              Can your please check and let me know this want you expect or not?


              Here my JS Code:

               

              i have checked in Indesign CS3 if you want change CS4/CS5 below line change into CS4 path:

              #target "Indesign-5.0"

               

              // this below line for WINDOWS
              #include "C:\\Program Files\\Adobe\\Adobe InDesign CS3\\Scripts\\XML Rules\\glue code.jsx"

               

              /*
              Are you using MAC SYSTEM
              //# include "glue code.jsx"
              */

               

              if (app.documents.length != 0){
                  var myDocument = app.documents.item(0);
                  var myRuleSet = new Array (new anchor);
                  with(myDocument){
                      var elements = xmlElements;
                      __processRuleSet(elements.item(0), myRuleSet);
                      }
                  }
              else{
                  alert("No open document");
                  }

               

              function anchor(){
                  this.name = "image";
                  this.xpath = "//image";

                  this.apply = function(myElement, myRuleProcessor){
                      with(myElement){
                          var myName=myElement.xmlAttributes.itemByName("fit").value;
                          app.select(myElement);
                          try {
                              app.menuActions.item("Go to Item").invoke();
                              }catch(e){};

                          if (myName == "frameToContent"){
                              app.selection[0].fit(FitOptions.frameToContent);
                              }
                          else if (myName == "proportionally"){
                              app.selection[0].fit(FitOptions.proportionally);
                              }
                          else if(myName == "centerContent"){
                              app.selection[0].fit(FitOptions.centerContent);
                              }
                          app.selection = null;
                          }
                      return true;
                      }
                  }
              alert("Done");

               

               

              thx,

              csm_phil

               

              Message was edited by: csm_phil

               

              Message was edited by: csm_phil

               

              Message was edited by: csm_phil

              1 person found this helpful
              • 5. Re: [JS IDCS5] Read Attribute values to specify Image options
                John Hawkinson Level 5

                Hopefully csm_phil's technique works for you.

                Do you understand why your original program is very wrong?

                It does things that don't make sense:

                 

                new findObjAttribute("//*")
                ...
                         myGraphics = app.documents[0].allGraphics;   
                ...
                         for (i = myGraphics.length-1; i > -1; i--)
                           myGraphics[i].fit(FitOptions.frameToContent);
                

                 

                That is, you look at every item in the XML document tree, and for each node, you refit all the graphics in the document according to it. If you had 10 nodes in the XML tree and 1,000 images in your document, you would do 10,000 fits, not five!

                 

                [You could probably also write that faster and more efficiently with .everyItem()]

                 

                Also, this:

                 

                if (Name == "frameToContent") {  
                for (i = myGraphics.length-1; i > -1; i--)
                myGraphics[i].fit(FitOptions.frameToContent);
                } 
                if (Name == "proportionally") {
                for (i = myGraphics.length-1; i > -1; i--)
                myGraphics[i].fit(FitOptions.proportionally);
                }
                if (Name == "centerContent") {
                for (i = myGraphics.length-1; i > -1; i--)
                myGraphics[i].fit(FitOptions.centerContent);
                }
                

                 

                is almost certainly better written as:

                 

                var fit;
                switch (Name) {
                case "frameToConent":
                  fit = FitOptions.frameToContent;
                  break;
                case "proportionally":
                  fit = FitOptions.proportionally;
                  break;
                case "centerContent":
                  fit = FitOptions.centerContent;
                  break;
                }
                for (i = myGraphics.length-1; i > -1; i--)
                  myGraphics[i].fit(fit);
                }
                
                • 6. Re: [JS IDCS5] Read Attribute values to specify Image options
                  Mayhem SWE Level 2

                  John Hawkinson wrote:

                   

                  is almost certainly better written as:

                   

                  var fit;
                  switch (Name) {
                  case "frameToConent":
                    fit = FitOptions.frameToContent;
                    break;
                  case "proportionally":
                    fit = FitOptions.proportionally;
                    break;
                  case "centerContent":
                    fit = FitOptions.centerContent;
                    break;
                  }
                  for (i = myGraphics.length-1; i > -1; i--)
                    myGraphics[i].fit(fit);
                  }
                  

                   

                  Heh, still seems overly complicated to me. Considering the Name string already matches FitOptions property keys it can be shortened to just two lines:

                  for (i = myGraphics.length-1; i > -1; i--)
                    myGraphics[i].fit(FitOptions[Name]);
                  1 person found this helpful
                  • 7. Re: [JS IDCS5] Read Attribute values to specify Image options
                    stoereee Level 1

                    Thank you all, I will test this at the office monday.

                    • 8. Re: [JS IDCS5] Read Attribute values to specify Image options
                      stoereee Level 1

                      csm_phil wrote:


                      I have modified your above JS Code as per your requirement and also i checked its working fine,

                      Can your please check and let me know this want you expect or not?

                       

                      Hi csm_phil,

                       

                      The script is generating the following error messege:

                       

                      error.JPG

                      • 9. Re: [JS IDCS5] Read Attribute values to specify Image options
                        csm_phil Level 4

                        Hi Storeee,

                         

                        i have checked in my js code its working fine, i am using ID CS4 In Mac version.

                        if you want using PC can you change the  myFilePath string text for corresponding "glude code jsx"

                        please check and let me know.

                         

                        #target "Indesign-6.0"
                        myFilePath="/Applications/Adobe InDesign CS4/Scripts/Xml Rules/glue code.jsx"
                        app.scriptPreferences.userInteractionLevel=UserInteractionLevels.interactWithAll;
                        var myFile = File(myFilePath);
                        app.doScript(myFile);           

                         

                        if (app.documents.length != 0){
                            var myDocument = app.documents.item(0);
                            var myRuleSet = new Array (new anchor);
                            with(myDocument){
                                var elements = xmlElements;
                                __processRuleSet(elements.item(0), myRuleSet);
                                }
                            }
                        else{
                            alert("No open document");
                            }

                         

                         

                         

                        function anchor(){
                            this.name = "image";
                            this.xpath = "//image";
                            this.apply = function(myElement, myRuleProcessor){
                                with(myElement){
                                    var myName=myElement.xmlAttributes.itemByName("fit").value;
                                    app.select(myElement);
                                    try {
                                        app.menuActions.item("Go to Item").invoke();
                                        }catch(e){};
                                    if (myName == "frameToContent"){
                                        app.selection[0].fit(FitOptions.frameToContent);
                                        }
                                    else if (myName == "proportionally"){
                                        app.selection[0].fit(FitOptions.proportionally);
                                        }
                                    else if(myName == "centerContent"){
                                        app.selection[0].fit(FitOptions.centerContent);
                                        }
                                    app.selection = null;
                                    }
                                return true;
                                }
                            }
                        alert("Done");

                         

                        thx,

                        csm_phil

                        • 10. Re: [JS IDCS5] Read Attribute values to specify Image options
                          stoereee Level 1

                          csm_phil,

                           

                          myFilePath="C:\\Program Files\\Adobe\\Adobe InDesign CS5\\Scripts\\XML Rules\\glue code.jsx"
                          

                           

                          This is the path to glue code.jsx and it is working fine.

                           

                          When I put #target "Indesign-6.0" on top of the script, the script is not working at all. When remove this line I get the same error as before!?

                          • 11. Re: [JS IDCS5] Read Attribute values to specify Image options
                            stoereee Level 1

                            This script is working, thank you all!

                             

                            #include "glue code.jsx";
                             
                            main();
                             
                            function main(){
                             
                            if (app.documents.length != 0){
                            var myDoc = app.activeDocument;
                            var myRuleSet = new Array (
                            new findObjAttribute("//image")
                            );
                             
                            with(myDoc){
                            var elements = xmlElements;
                            __processRuleSet(elements.item(0), myRuleSet);
                            }
                            }
                            else{
                            alert("You have no document open!");
                            exit();
                            }
                             
                             
                            function findObjAttribute(XPATH){
                            this.name = "image";
                            this.xpath = XPATH;
                            this.apply = function(myElement, myRuleProcessor)
                            {
                            with(myElement){
                            try {
                            var myName=myElement.xmlAttributes.itemByName("fit").value;
                                      app.select(myElement);
                                        try {
                                            app.menuActions.item("Go to Item").invoke();
                                            }catch(e){};
                                        if (myName == "frameToContent"){
                                            app.selection[0].fit(FitOptions.frameToContent);
                                            }
                                        else if (myName == "proportionally"){
                                            app.selection[0].fit(FitOptions.proportionally);
                                            }
                                        else if(myName == "centerContent"){
                                            app.selection[0].fit(FitOptions.centerContent);
                                            }
                                        app.selection = null;
                                 } 
                            catch(e){};
                            }
                            return true;
                            }
                            }
                            }