4 Replies Latest reply on Dec 7, 2012 7:06 AM by SimonLinden

    Adding properties with ParagraphStyles.add()

    SimonLinden Level 1

      Hi!
      I must be misunderstanding how the property "properties" of classes ParagraphStyle and CharacterStyle works. I have a function which looks for text with provided attributes and assigns an existing paragraph style to it. If the requested paragraph style doesn't exist a new style should be created and that's where I'm having trouble. I'd thought I could just write...

      app.activeDocument.paragraphStyles.add({
                  name: styleName,
                  properties: myProperties
      });
      

       

      ...and that the style would be created with all those properties but it doesn't work that way. However if I write

       

      app.activeDocument.paragraphStyles.add({
                  name: styleName,
                  fontStyle: myProperties.fontStyle
      });
      

       

      then the right fontStyle gets applied to the new paragraph style, so the values are in the variable myProperties for sure. Any ideas on how to get this workning? See the full function with caller below if you need the context.

       

      applyParagraphStyle("s-heading", {pointSize: 12, fontStyle: "Bold"});
      
      function applyParagraphStyle(styleName, myProperties) {
          app.findGrepPreferences = NothingEnum.nothing;
          app.findGrepPreferences.properties = myProperties;
          
          var styles = app.activeDocument.paragraphStyles;
          var style;
          var found = app.activeDocument.findGrep();
          
          try {
              style = styles.item(styleName);
              
              //try to assign style.name to value to see if an error is thrown
              var n = style.name;
          }
          catch (myError) {
              alert("Couldn't find paragraph style '" + style.name + "'. New style created.");
              style = styles.add({
                  name: styleName,
                  properties: myProperties
              });
          }
          for (i = 0; i < found.length; i++){
              found[i].applyParagraphStyle(style, false);
          }
      }