4 Replies Latest reply on Apr 11, 2016 8:11 AM by durgapasupuleti

    Creating an object style with a script

    Miller42 Level 1

      I'm slowly teaching myself scripting, but my first script has a bug I can't seem to fix. Part of it creates an object style called "plain text" that has certain attributes. One of those attributes is that the first baseline offset should be "Fixed" at a certain leading. When I run the script in InDesign CS3, sometimes it works, and the first baseline offset is "Fixed", but sometimes it doesn't work, and the first baseline offset remains at the default value of "Ascent." I need it to work in InDesign CS4, too, but it doesn't work there at all. I've looked through the DOM, but it looks like everything is coded properly.

       

      Below is a portion of the script. What am I missing?

       

      //Creating the "plain text" object style.
      var myPlainTextObjectStyle = myDocument.objectStyles.add();
      with(myPlainTextObjectStyle){
      name = "plain text";
      enableFill = true;
      enableStroke = true;
      enableStrokeAndCornerOptions = false;
      enableTextFrameBaselineOptions = true;
      enableTextFrameGeneralOptions = true;
      fillColor = myDocument.swatches.item(0);
      strokeColor = myDocument.swatches.item(0);
      textFramePreferences.firstBaselineOffset.firstBaseline = FirstBaseline.FIXED_HEIGHT;
      textFramePreferences.minimumFirstBaselineOffset = myLeading;
      textFramePreferences.textColumnCount = 1;
      textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
      }

        • 1. Re: Creating an object style with a script
          Dave Saunders Level 4

          Your problem is that you have an extra "firstBasline" in the line that sets the firstBaselineOffset. Your script should read:

           

          //Creating the "plain text" object style.
          var myPlainTextObjectStyle = myDocument.objectStyles.add();
          with(myPlainTextObjectStyle){
          name = "plain text";
          enableFill = true;
          enableStroke = true;
          enableStrokeAndCornerOptions = false;
          enableTextFrameBaselineOptions = true;
          enableTextFrameGeneralOptions = true;
          fillColor = myDocument.swatches.item(0);
          strokeColor = myDocument.swatches.item(0);
          textFramePreferences.firstBaselineOffset = FirstBaseline.FIXED_HEIGHT;
          textFramePreferences.minimumFirstBaselineOffset = myLeading;
          textFramePreferences.textColumnCount = 1;
          textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
          }
          

           

          Although, there's a better syntax you could be using:

           

          //Creating the "plain text" object style.
          var myPlainTextObjectStyle = myDocument.objectStyles.add();
          myPlainTextObjectStyle.properties = {
               name : "plain text",
               enableFill : true,
               enableStroke : true,
               enableStrokeAndCornerOptions : false,
               enableTextFrameBaselineOptions : true,
               enableTextFrameGeneralOptions : true,
               fillColor : myDocument.swatches.item(0),
               strokeColor : myDocument.swatches.item(0),
               textFramePreferences : { 
                    firstBaselineOffset : FirstBaseline.FIXED_HEIGHT,
                    minimumFirstBaselineOffset : myLeading,
                    textColumnCount : 1,
                    verticalJustification : VerticalJustification.TOP_ALIGN 
               }
          }
          

           

          What makes this better is that there is just the one interaction with the object model rather than one for every property of the object style.

           

          Dave

          • 2. Re: Creating an object style with a script
            Steven.. Level 3

            or maybe with only 1 interaction

             

            var props = {
                 name : "plain text",
                 enableFill : true,
                 enableStroke : true,
                 enableStrokeAndCornerOptions : false,
                 enableTextFrameBaselineOptions : true,
                 enableTextFrameGeneralOptions : true,
                 fillColor : myDocument.swatches.item(0),
                 strokeColor : myDocument.swatches.item(0),
                 textFramePreferences : { 
                      firstBaselineOffset : FirstBaseline.FIXED_HEIGHT,
                      minimumFirstBaselineOffset : myLeading,
                      textColumnCount : 1,
                      verticalJustification : VerticalJustification.TOP_ALIGN 
                 }
            }
            var myPlainTextObjectStyle = myDocument.objectStyles.add(props);
            

            1 person found this helpful
            • 3. Re: Creating an object style with a script
              Miller42 Level 1

              Thanks for your help! That solved it. I'll try to keep those streamlining methods in mind.

              • 4. Re: Creating an object style with a script
                durgapasupuleti

                Hello Dave,

                 

                This is very nice to read. But I am facing a problem with the script. My requirement is to color the logo text.  Logo files are either .eps or .tiff files and have a text on them. I need to colorize the text which is on Image file before exporting to pdf using Indesign server. Could you please help me out. Below is the script we are using,

                 

                function imageColors(myDoc, iName, iColor, curTemplate) {
                  var isColorized = false;
                    try {
                       // logToCon("in imageColors ::::::: "+iName);
                        changeColors(myDoc, iName, iColor, curTemplate);
                
                        var myObjectStyle = myDoc.objectStyles.item(iName);
                  //logToCon("in myObjectStyle ::::::: "+myObjectStyle);
                   
                        try {
                            
                            var myName = myObjectStyle.name;
                  //logToCon("in myObjectStyle name Before catch statement::::::: "+myName);
                        }
                        catch (e) {
                            //The object style did not exist, so create it.
                            myObjectStyle = myDoc.objectStyles.add({ name: iName, fillColor: iName, strokeColor :iName });
                        }
                  //for(var i = 0; i < myDoc.objectStyles.length; i++){  
                  // logToCon("myDocument.objectStyles["+i+"].name  "+myDoc.objectStyles[i].name);
                   
                    //}
                  for(var i = 0; i < myDoc.allPageItems.length; i++){  
                
                
                
                      if ( myDoc.allPageItems[i].label == iName) {
                
                                myDoc.allPageItems[i].applyObjectStyle(myObjectStyle, true, true);
                  //myDoc.allPageItems[i].fillColor = iName;
                  isColorized = true;
                            }
                  }
                     
                
                
                    } catch (e) {
                        updateResponse("code4", "Error while processing template " + curTemplate + " for imageColors for Name " + iName + " for color " + iColor + ". Error Message: " + e.message, -1);
                
                
                    }
                    return isColorized;
                }@@@@