2 Replies Latest reply: Dec 9, 2011 7:51 AM by Hoang_Huynh RSS

    How to save/retrive custom properties to a PageItem

    Hoang_Huynh

      Hello community, have any of you succeeded in saving custom properties/data to a PageItem? I try to save my own properties to a PageItem using its property named properties. Setting properties seems to work but getting doesn't. This is my code (ActionScript 3.0):

       

      var app:Application = InDesign.app;

      var page:Page = app.activeDocument.pages.item(0);

      var items:Array = page.allPageItems as Array;

       

      // my properties

      var myPropertyArray:Object = {fname:"John", lname:"Lennon"};

       

      // set properties to first item of page

      items[0].properties = myPropertyArray; // this works

       

      // now get

      var props:Object = items[0].properties; // Error: In wrapReturnedHostObject(), attempted invalid wrap: com.adobe.indesign::Enumerator returned as Object

       

      I read on this forum that this is a bug of CSAWLib. If so, is there any other way to save my custom properties to an InDesign document?

       

      Thanks!

      --Hoang

        • 1. Re: How to save/retrive custom properties to a PageItem
          Marc Autret Community Member

          Hi Hoang,

           

          I'm afraid your code:

           

          items[0].properties = customObject;

           

          only *seems* to work, but actually does not. The properties property is a special command that only deals with native, valid, properties of a DOM object. Used as a setter, it simply ignores non-native keys. Used as a getter, it simply returns available properties. More generally, given a page item—say myRectangle— any statement such as myRectangle.myCustomProperty = "foobar" will cause an error ("Invalid property or method", or whatever) because you cannot extend DOM object instances. They all are command wrappers (=specifiers) and don't work as regular JS objects.

           

          Anyway, you can extend the methods of DOM object prototypes, e.g.:

           

          Rectangle.prototype.myCustomMethod = function(){ return this.label;};

           

          When you do that, keep in mind that a Rectangle object instance can reflect several actual InDesign rectangles. For example, myDoc.rectangles.everyItem() is seen as a Rectangle (in terms of cast).

           

          IMHO, the right way to attach custom properties to DOM components is to use insertLabel() / extractLabel() with serialized objects.

           

          @+

          Marc

           

          EDIT: The CSAWLib bug you mention is not connected to the problem of setting custom properties.

          • 2. Re: How to save/retrive custom properties to a PageItem
            Hoang_Huynh Community Member

            Thank you so much Marc!

             

            Your answer explains clearly what I just need. Using insertLabel() / extractLabel() with serialized objects is a good way to store custom data in a PageItem.