Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Extending Document Object in CS5

Explorer ,
Jan 31, 2011 Jan 31, 2011

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?

TOPICS
Scripting
842
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
LEGEND ,
Feb 01, 2011 Feb 01, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 01, 2011 Feb 01, 2011

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 01, 2011 Feb 01, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 01, 2011 Feb 01, 2011

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
      app.activeDocument.pages;
that would indicate you need to somehow make the "Pages" object available.  I tried adding

      app.activeDocument
to 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?


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 05, 2011 Feb 05, 2011
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 01, 2011 Feb 01, 2011

It's also clear...

app.myOwnMethod - is just a variable contents "function() {return "Test String proto"}", and

app.myOwnMethod(); - calls your function

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines