1 Reply Latest reply on Sep 8, 2012 6:08 PM by Marc Autret

    Appending Code

    sushdeeksh

      Hi all,

       

      While exporting indesign to html i created seaprate css also,

       

      var cssText1 = "";

       

      var separator1 = kCreateCompactCSS ? " " : "\n";

       

      var cssLine1 = frameReference.identifier +

      "{" + separator1 +

                

      (kCreateCompactCSS ?

      "" :

      (" /* Page " + frameReference.page + ", " + ((frameReference.frame instanceof TextFrame) ? "Text" : "Graphic") + " */ ")) + separator +

                 

      if(app.activeDocument.textFrames[i].itemLayer.name == "Layer 3")

      {

          "z-index:3;" + separator1 +

      }

      if(app.activeDocument.textFrames[i].itemLayer.name == "Layer 2")

      {

          "z-index:2;" + separator1 +

      }

      if(app.activeDocument.textFrames[i].itemLayer.name == "Layer 1")

      {

          "z-index:1;" + separator1 +

      }

       

      "}\n\n";            cssText1 += cssLine11;

       

      Using this code..

       

      But now i want insert if condition inside this,

       

      My logic is if textbox in layer 1 and z-index should be 1 and layer 2 z-index is 2.. how to do tht..

       

      i written some code like this :

      var cssText1 = "";

       

      var separator1 = kCreateCompactCSS ? " " : "\n";

       

      var cssLine1 = frameReference.identifier +

      "{" + separator1 +

                

      (kCreateCompactCSS ?

      "" :

      (" /* Page " + frameReference.page + ", " + ((frameReference.frame instanceof TextFrame) ? "Text" : "Graphic") + " */ ")) + separator +

                 

      if(app.activeDocument.textFrames[i].itemLayer.name == "Layer 3")

      {

          "z-index:3;" + separator1 +

      }

      if(app.activeDocument.textFrames[i].itemLayer.name == "Layer 2")

      {

          "z-index:2;" + separator1 +

      }

      if(app.activeDocument.textFrames[i].itemLayer.name == "Layer 1")

      {

          "z-index:1;" + separator1 +

      }

       

      "}\n\n";            cssText1 += cssLine11;

       

      but giving error plzzz help for me to get result...

        • 1. Re: Appending Code
          Marc Autret Level 4

          Hi,

           

          1) Assuming I understand your question, the general pattern of your problem can be described as follows: given a set of exclusive conditions cond1, cond2, cond3..., how to set an expression to value1 if cond1 is true, to value2 if cond2 is true, to value3 if cond3 is true, etc., otherwise to a default value. In other words, you want the expression to be set according to this pseudocode:


          myExpression = (function()
              {
              if( cond1 ) return value1;
              if( cond2 ) return value2;
              if( cond3 ) return value3;
              // etc.
              return defValue;
              })();
          

           

          In JavaScript, a usual and very compact way to get such expression is:

           

          myExpression = [defValue, value1, value2, value3][1*(cond1)||2*(cond2)||3*(cond3)]
          

           

          If we apply that pattern to your code with no factorization, we end up with something like:

           

          // ...
          var cssLine1 = "beginning of your string" +
          
              ["", "z-index:3;"+separator1, "z-index:2;"+separator1, "z-index:1;"+separator1]
              [
              1*(app.activeDocument.textFrames[i].itemLayer.name == "Layer 3") ||
              2*(app.activeDocument.textFrames[i].itemLayer.name == "Layer 2") ||
              3*(app.activeDocument.textFrames[i].itemLayer.name == "Layer 1")
              ] +
          
              "end of your string";
          // ...
          

           

          However, you can see there is still a lot of redundancy. So…

           

          2) Considering the specific structure of your three tests, it is possible to use regular expressions instead. Thanks to a few acrobatics, you can even avoid introducing any additional variable—although the resulting code is pretty hilarious:

           

          var cssLine1 = "beginning of your string" +
          
              (app.activeDocument.textFrames[i].itemLayer.name.match(/^Layer ([1-3])$/)||['',''])[1].
                  replace(/\d/,"z-index:$&;"+separator1) +
          
              "end of your string";
          

           

          Of course there is no reason to proceed as esoterically. Better is to parse the itemLayer's name and preset the CSS part into a string BEFORE you set cssLine1!

           

           

          Hope that helps anyway.

           

          @+

          Marc