2 Replies Latest reply on Jun 24, 2008 10:47 PM by (Andy_N.)

    Generic access with properties-property

      Hi everyone,

      I need some help with a very special scripting propblem that I encountered recently.

      I want to have a generic access to some InDesign object properties, and with the concept of the .properties there is a fine way to do this.
      With the following script (just a simple example) I can alter the given attribute of the object.

      b var someFont = app.activeDocument.paragraphStyles.item("Heading").appliedFont;

      b var word = app.activeDocument.pages[0].textFrames[0].words[0];

      b word.properties = {"appliedFont": someFont};

      As a matter of fact I can leave the quotation marks in the last line (which I find quite strange, and which I think is the reason for my proplem; see below).

      b word.properties = {appliedFont: someFont};

      However, in the next step I wanted to parameterize this expression, and that doesn't work, because the property is not set.

      b var nameOfTheProperty = "appliedFont";

      b word.properties = {nameOfTheProperty: someFont};

      I *think* that I do understand why this won't work. It's because of the associative array underneath this syntax.

      And now my question: I always thought that there is another syntax to alter such associative array.

      b word.properties["appliedFont"] = someFont;

      But that doesn't work. Do I make a misstake or is it simply not possible to have this generic access I want to have.

      I would appreciate if someone can help me out with this.
      Thanks in advance
      Andy

      CS3/Windows
        • 1. Re: Generic access with properties-property
          Level 1
          I think the reason that last version doesn't work is akin to the reason that when you set an array property, you have to use the whole array. For example, geometricBounds:

          myPI.geometricBounds[2] = 45;

          executes just fine but does nothing useful. Why? Because when the interpreter sees the reference to the array member, it pulls a copy of the geometricBounds property into a temporary array and changes the value of item 2 of it. Thereby executing but doing nothing useful.

          When you say:

          word.properties["appliedFont"] = someFont;

          you're doing something very similar. The properties property of word is an associative array, so what I think happens is very similar to the geometricBounds case -- a copy of properties is moved into a temporary space and the appliedFont property is changed in that temporary copy, so the statement executes, but does nothing useful.

          Why are you using the properties property at all, here?

          word["appliedFont"] = someFont;

          will do what you want.

          Dave
          • 2. Re: Generic access with properties-property
            Level 1
            b word["appliedFont"] = someFont;

            Great tip Dave!

            I didn't know this would work. It perfectly fits my needs.

            And thanks for the background info on the properties issue.

            Andy