Skip to main content
Known Participant
December 5, 2006
Question

How to add custom data type to scripting DOM

  • December 5, 2006
  • 4 replies
  • 778 views
Hi All,

I want to add a new data type (eg. MyData) to InDesign scripting DOM, so that a user can create a variable of that type and use it as a parameter in the events.

The custom data type will be used as given below,
---------------------------------------------------------
MyData data;
data["Key1"] = value1;
data["Key2"] = value2;
app.myEvent(data);
---------------------------------------------------------

Regards,
Pete
This topic has been closed for replies.

4 replies

Inspiring
December 6, 2006
Let me clarify the reason for requiring these predeclared names: Internally they are translated into the script ID which is a 4 character code, see the AppleEvent and AERecord structures for details. Those script IDs have to be predeclared in the ScriptInfo resource.

Sometimes I wished myself that Adobe had added a way to dynamically allocate arbitrary names, but you can't have it all.

Dirk
Inspiring
December 6, 2006
It all depends.

If you want a native class with native properties that invoke methods on access, have their native functions and so forth, then you have to follow the patterns for such objects.

You determine an appropriate parent class in the object hierarchy, such as app or Document.
You add an element collection in that class
You use the typical syntax with the add() on the collection, instead of JS operator new.
myData = myParent.myDatas.add();

The SDK has plenty info how to make your plugin scriptable, including dedicated pdf guide. If you search the fr sources for "RepresentObject" you will find working samples. Do not follow the preferences pattern as they are constructed as singleton, but CandleChart and CustomDatalink should be ok.

If you want a pure JS object you have to declare its constructor function before use. There are plenty callback methods in the ScriptProvider that give you an execution context, where you can execute own JavaScript, for example you make a script provider that adds the initMyData() method to the application script object.

You then would have to follow that "record" approach I suggested previously, for the record argument it is irrelevant whether the given object is plain JS Object or anything JS derived. It just cares whether the properties match by name to those of the expected native class.

You can't use arbitrary names (key3) here, if you want that you have to add glue code that iterates and feeds the associative JS array into actual native methods.

Dirk
Known Participant
December 6, 2006
Thanks Dirk. Yes, I understand this but I want to do something like,

var data = new MyData();
data.key1 = value1;
data.key2 = value2;

Now, here MyData is a data type defined by my plugin. How can I do this?

Regards,
Pete
Inspiring
December 5, 2006
I assume this is JavaScript.
You should then use either of these:

var data = {};
data["Key1"] = value1;

var data = new Object();
data.Key1 = value1;

var data = {Key1: value1};

You can only achieve this if you have all Key1 names registered as script names and make them properties for a class. The class name must also be registered. Then you use the record type for your data parameter, specifying it in the ScriptInfo resource works the same way as for object type parameters.

The same mechanism is used when the add() method takes an additional "with properties" parameter, or when you directly assign to the "properties" property.

Dirk