7 Replies Latest reply on May 11, 2009 12:25 PM by Harbs.

    [JS, CS4] TextFrame.extractLabel() cannot work with instances of this class

    Andreas Jansson Level 2

      I have script, which has been working fine in CS3. In CS4 ( app.version = 6.0.1.532 ) however, I get an error using extractLabel, after reading out a couple of other properties from a textframe:

       

      frameObject.extractLabel('name')
      Error: TextFrame.extractLabel() cannot work with instances of this class

       

      Up until reading one of the "normal" properties, such as (frameObject.)contents I can call the frameObject.extractLabel('name') without errors, but after "looking at" (by assigning to a variable in code, or by getting the value in the javascript console), the contents propery (or as it seems any normal property) the extractLabel method results in the error above.

       

      It seems to work to move all of the frameObject.extractLabel calls to the beginning of the function, but I don't think I should need to do that.

      It might very well be the case that the label read by extractLabel has no contents, and has never been assigned. Is there a change in behaviour from CS3 in that sentence? If so, and if thats the reason for the error, is there then a way to determine whether the label has ever been assigned?

       

      Is this error familiar to anyone else?

       

      Best regards,

      Andreas

        • 1. Re: [JS, CS4] TextFrame.extractLabel() cannot work with instances of this class
          Olav Martin Kvern Level 3

          Hi Andreas,

           

          I'm not sure what's going on--the following script works fine for me. Does it work for you?

           

          var myDocument = app.documents.add();
          myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
          myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
          var myTextFrame = myDocument.pages.item(0).textFrames.add({geometricBounds:[72, 72, 144, 288], contents:TextFrameContents.placeholderText});
          myTextFrame.insertLabel("myLabel", "Some label text.");
          alert(myTextFrame.contents);
          alert(myTextFrame.extractLabel("myLabel"));

           

          Thanks,

           

          Ole

          1 person found this helpful
          • 2. Re: [JS, CS4] TextFrame.extractLabel() cannot work with instances of this class
            Andreas Jansson Level 2

            Hi Ole,

             

            Your code works. It seems to be different depending on how I referr to the text frame. If I set the variable to the selected textbox I get no error. I need to use the itemByID to get the objects, in my code. Below I have adapted your code to result in the error.

             

            var myDocument = app.documents.add();
            myDocument = app.activeDocument;
            myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
            myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
            var myTextFrame = myDocument.pages.item(0).textFrames.add({geometricBounds:[72, 72, 144, 288], contents:TextFrameContents.placeholderText});
            var myId = myTextFrame.id;
            
            var pgItm = app.activeDocument.pageItems.itemByID(parseInt(myId));
            //var pgItm = app.selection[0]; // using app.selection does not result in the error.
            var myTextObj = new TextFrameInfo (pgItm, false);
            myTextObj;
            
            function TextFrameInfo(frameObject, withXMLContents){
                 this.Name = frameObject.extractLabel('name');
                 this.Contents = frameObject.contents;
                 var x = frameObject.extractLabel('mandatory'); // Here comes the error, just by calling extractLabel.
                 //this.Mandatory = StringToBool(frameObject.extractLabel('mandatory'));
            }
            
            
            
            • 3. Re: [JS, CS4] TextFrame.extractLabel() cannot work with instances of this class
              Dave Saunders Level 4

              Does it work if you use:

               

              var pgItm = app.activeDocument.textFrames.itemByID(parseInt(myId));

               

              or, this might work:

               

              var pgItm = app.activeDocument.pageItems.itemByID(parseInt(myId)).getElements()[0];

               

              Although, usually, itemByID gives you the item no matter what it is as long as it exists, so I'm kind of puzzled that either of these would be necessary.

               

              Dave

              • 4. Re: [JS, CS4] TextFrame.extractLabel() cannot work with instances of this class
                Andreas Jansson Level 2

                Yes, it's puzzeling, and since it works in CS3 I'd say it's a bug in CS4.

                 

                Both of your suggenstions work, Dave. I'll have to use the second one though.Thank you for helping out!

                 

                Andreas

                • 5. Re: [JS, CS4] TextFrame.extractLabel() cannot work with instances of this class
                  Harbs. Level 6

                  Andreas,

                   

                  Not knowing exactly how you are referencing your object I can't say what might be wrong, but chances are you are somehow creating a custom class which is throwing things off track.

                   

                  I have never seen accessing a property causing the results that you're seeing (and I access labels quite often...).

                   

                  Harbs

                  • 6. Re: [JS, CS4] TextFrame.extractLabel() cannot work with instances of this class
                    Andreas Jansson Level 2

                    Harbs,

                     

                    Here is the test code, stripped to a minimum, without the custom class, still creating the same error - don't you get the same error running it?

                     

                     

                    var myDocument = app.documents.add();
                    myDocument = app.activeDocument;
                    myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
                    myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
                    var myTextFrame = myDocument.pages.item(0).textFrames.add({geometricBounds:[72, 72, 144, 288], contents:TextFrameContents.placeholderText});
                    var myId = myTextFrame.id;
                     
                    var pgItm = app.activeDocument.pageItems.itemByID(parseInt(myId));
                    var thisName = pgItm.extractLabel('name');
                    var thisContents = pgItm.contents;
                    var x = pgItm.extractLabel('mandatory'); // error here
                    

                     

                    -- Andreas

                    • 7. Re: [JS, CS4] TextFrame.extractLabel() cannot work with instances of this class
                      Harbs. Level 6

                      Hi Andreas,

                       

                      Interesting problem!

                       

                      Your problems are caused by some peculiarities of itemByID. itemByID doesn't cast the object type properly. Dirk wrote about some aspects of this some time ago. If the scripting engine was strongly typed, this issue would probably be impossible but that would make scripting a lot more of an elitist activity!

                       

                      Your problem is that the object type is PageItem and you are accessing a TextFrame property (content). This seems to throw the object type for a loop.

                       

                      Using getElements() causes the scripting engine to rebuild the reference to the object correctly.

                       

                      Take a look at this code, which illustrates the issue quite well...

                       

                      var myDocument = app.documents.add();
                      myDocument = app.activeDocument;
                      myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
                      myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
                      var myTextFrame = myDocument.pages.item(0).textFrames.add({geometricBounds:[72, 72, 144, 288], contents:"test"});
                      var myId = myTextFrame.id;
                      
                      var pgItm = myDocument.pageItems.itemByID(myId);
                      alert(pgItm instanceof PageItem);
                      alert(pgItm instanceof TextFrame);
                      
                      var thisBounds = pgItm.geometricBounds;
                      var x = pgItm.extractLabel('mandatory');
                      
                      var pgItm = myDocument.textFrames.itemByID(myId);
                      alert(pgItm instanceof PageItem);
                      alert(pgItm instanceof TextFrame);
                      var thisName = pgItm.extractLabel('name');
                      var thisContents = pgItm.contents;
                      var x = pgItm.extractLabel('mandatory');
                      

                      Harbs

                      1 person found this helpful