2 Replies Latest reply on Jul 15, 2011 9:20 AM by OliverJohn

    Object equality test requires use of id value

    OliverJohn Level 2

      I was writing code that needed to determine whether two TextFrame objects appeared on the same page, and noticed this bit of strangeness:

       

      If I use the following code:

       

      if (thisTextFrame.FrameParent == thatTextFrame.FrameParent) {...}

       

      the expression always evaluates to false. However, if instead of comparing the objects themselves, I compare their id properties:

       

      if (thisTextFrame.FrameParent.id == thatTextFrame.FrameParent.id) {...}

       

      the expression produces accurate results. After some testing, it seems the same behavior occurs when any FM object referenced by two variables is compared to itself.

       

      Everywhere else in ExtendScript, the use of F_ObjHandleT values has been replaced by direct object references. I have no idea if this exception is a bug or by design, but since I did not see it documented anywhere else, it seemed a good idea to mention it here. 

        • 1. Re: Object equality test requires use of id value
          frameexpert Level 4

          Hi Oliver,

           

          I am not exactly sure about this, but I think JavaScript objects are different animals than "objects" in the FDK, or FrameScript for that matter. For example, you can test if an object exists in FrameScript like this:

           

          If thisTextFrame

            // Do something.

          EndIf

           

          In the FDK, it would be

           

          if (thisTextFrame) {

            // Do something.

          }

           

          But with ExtendScript, you have to do this:

           

          if (thisTextFrame.ObjectValid()) {

            // Do something.

          }

           

          So there is something different going on in ExtendScript.

           

          It may be that, while thisTextFrame and thatTextFrame refer to the same TextFrame, they are represented by two separate JavaScript objects. This may be why they are not considered equal. But when you compare ids, you get a test of true equality since ids are unique within a document.

           

          This is only a bit of speculation on my part, but maybe it will prompt someone more knowledgeable to shed light on the issue.

           

          Rick

          • 2. Re: Object equality test requires use of id value
            OliverJohn Level 2

            Rick--

             

            One thing to keep in mind is the difference between an object existing and an object being valid. For example, if you reference the first paragraph of an empty text frame, FM returns an empty Pgf object with an id of 0. The Pgf object exists, but is not valid. But if I try to go one step further and reference the next paragraph in the flow (or any other property of the invalid object), FM returns nothing, and the script throws an error. The Pgf object does not exist. So:

             

            x = myEmptyFrame.FirstPgf.ObjectValid();  // x == false, script keeps running

            y = myEmptyFrame.FirstPgf.NextPgfInFlow.ObjectValid(); // y == nothing, script throws error

             

            I can't speak to how the FrameScript engine works, but it sounds like it encapsulates both conditions into a single test.

             

            As to the equality test behavior I described being general to JavaScript, I can confidently state that is not the case. In fact, I've confirmed that it is not even general to ExtendScript. If I write an ES script that targets InDesign, I can reliability compare two variables that reference the same object without looking at properties of that object.