Copy link to clipboard
Copied
I would like to extend the Document object (or any other, for that matter) as described in this thread:
http://forums.adobe.com/message/1103689#1103689
I tried extending Application with myOwnMethod as described, but even that doesn't work. When I "alert" the extended function, it prints the implementation code of the procedure! Here's the code:
Application.prototype.myOwnMethod = function() {
return "Test String proto"
}
alert(app.myOwnMethod)
When run, the alert box contains:
function() {
return "Test String proto"
}
What do I do to extend the Document or Application objects?
Copy link to clipboard
Copied
One word:
Don't!
I've never tried in Illustrator, but trying to extend native objects is more trouble than it's worth in InDesign. I imagine the same thing applies to Illustrator.
You are not invoking your method. To get your example to work the way you want you need to add the parenthesis:
alert(app.myOwnMethod())
Harbs
Copy link to clipboard
Copied
Can you point me somewhere that explains "Don't!" What kind of trouble? Why is this any worse than having to use javascript at all?
Copy link to clipboard
Copied
Search the InDesign scripting forum.
We've had quite a bit of discussion on prototyping objects in general, and InDesign objects in particular.
You should find plenty of food for thought there...
The absolute worst thing to do is to prototype "Object". Native objects can sometimes be prototyped and sometimes not. (For example InDesign collection objects can only be prototyped if they are previously invoked. I've never tried in Illy.) If there's any chance of scripts effecting other scripts, you're asking for trouble by prototyping any global objects.
Harbs
Copy link to clipboard
Copied
Assuming I'm willing to take the risk, how can I extend the Document object? It doesn't work like the Application object. I tried all different types of prototyping, but nothing seems to do the trick. This thread contains an explanation why you (Harbs) don't like to extend Object. It also contains this example
// make global"Pages" object available that would indicate you need to somehow make the "Pages" object available. I tried adding
app.activeDocument.pages;
app.activeDocumentto make Document available, but it doesn't work. It also doesn't work with Symbol. It does work with Application. What's the difference? How do I get it working in these other cases?
Copy link to clipboard
Copied
I was able to make these accessible with this code:
app.documents[0] // Make Document available
app.documents[0].symbols[0] // make Symbol object available
I don't know how to make PageItem available using this technique. If you try it, Extendscript seems to use the derivative object, not the PageItem. It is necessary to touch the PageItem, not its descendent.
This code has problems if the document doesn't have symbols or the document isn't open. The extended code needs protection for these cases too.
Copy link to clipboard
Copied
It's also clear...
app.myOwnMethod - is just a variable contents "function() {return "Test String proto"}", and
app.myOwnMethod(); - calls your function
Find more inspiration, events, and resources on the new Adobe Community
Explore Now