Copy link to clipboard
Copied
I am trying to implement a CustomDoc class that inherits from the document class. For example, something like the following:
var CustomDoc = function(doc){
/* Code to inherit properties and methods of Document class and
set the properties to those in doc */
this.exportPng = function(){
/* Method that exports the current document */
};
};
var currentDoc = new CustomDoc(app.activeDocument);
currentDoc.pathItems.removeAll(); //pathItems as example inherited property
currentDoc.exportPng(); //Custom function defined in CustomDoc
I'm not sure exactly how to do this. For one thing, the Document class does not have a constructor, so I can't do something like this:
var CustomDoc = function(doc){ Document.call() };
Nor can I do something like this:
CustomDoc.prototype = Document.create();
What I can do is add methods to an existing Document object, such as activeDocument:
activeDocument.customMethod = function(){
/* Body */
};
This might work, but it's not really how I'd prefer to design things. For example, if I create the variable newDoc in the example below, it won't have the properties or methods of activeDocument:
var newDoc = app.documents.add();
Rather, I would like to be able to do the following:
var newDoc = new CustomDoc(app.documents.add());
Does anyone have recommendations on how to go about this? Many thanks in advance.
Copy link to clipboard
Copied
I'd recommend basic wrapper over an instance of a PS doc for example (not bug-proofed)
var MyDoc = {
exportPng: function() {...},
create: function(ps_doc} {
return {
_doc: ps_doc,
exportPng: MyDoc.exportPng
}
}
}
Then you'd
var my_doc = MyDoc.create(app.documents.add())
mydoc.exportPng()
Copy link to clipboard
Copied
This works, though it does not allow the use of the 'this' keyword when writing functions, and also requires you to do something like my_doc._doc.name, rather than my_doc.name, which is clumsy. Can you think of a way that would behave more like a subclass of Document?
Copy link to clipboard
Copied
var MyDoc = function(doc){
doc.printName = function(){
$.writeln(this.name);
};
return doc;
};
var doc = new MyDoc(activeDocument);
doc.printName();
$.writeln(doc.name);
The above writes your document name twice. It allows use of the this keyword while writing functions, and access to Document properties and methods in the same namespace as your custom properties and methods. Many thanks for your help, I would not have figured this out otherwise.