Skip navigation
Currently Being Moderated

extending existing objects/classes

May 31, 2012 1:39 PM

is there a way to extend existing classes? I am trying to add methods to the symbolItems class, but it doesn't seem to work. says it is not defined:

 

symbolItems.prototype.isIn = function(parentItem, runThis) {
//do something
}
 
Replies
  • Currently Being Moderated
    May 31, 2012 2:11 PM   in reply to hilukasz

    Not as far as I know. AI scripting is pretty basic.

     
    |
    Mark as:
  • Currently Being Moderated
    May 31, 2012 2:22 PM   in reply to hilukasz

    You can extend the classes by prototyping it's something I have avoided so far but have seen other AI scripts where it has been used without issue though…

     
    |
    Mark as:
  • Currently Being Moderated
    May 31, 2012 3:34 PM   in reply to Muppet Mark

    You're right, Mark. I found one of Carlos's scripts with a prototype class extension.

    #target Illustrator
     
    //  script.name = preview_CS4_CS5.V1.0.jsx;
    //  script.description = previews the active artboard;
    //  script.required = an open document;
    //  script.parent = carlos canto // 8/30/11;
    //  script.elegant = false;
    //  script.credits = inspired by one of Mark Larsen's scripts where he used a snap shot of a selected object in the ScriptUI.
    //                             Script made possible by the use of Marc Autret's prototype function to resize an image to fit its container.
     
    //  notes: not the fastest script in town, it takes a couple of seconds to capture the screen. The bigger the artboard, the longer it takes.
     
    //  Mac users: ************************** Press Esc key to dismiss *************************************** , windows too in fact.
     
    if (app.documents.length > 0)
        {
            // thanks to Marc for writing this amazing function prototype
            Image.prototype.onDraw = function()
                { // written by Marc Autret
                    // "this" is the container; "this.image" is the graphic
                    if( !this.image ) return;
                    var WH = this.size,
                    wh = this.image.size,
                    k = Math.min(WH[0]/wh[0], WH[1]/wh[1]),
                    xy;
                    // Resize proportionally:
                    wh = [k*wh[0],k*wh[1]];
                    // Center:
                    xy = [ (WH[0]-wh[0])/2, (WH[1]-wh[1])/2 ];
                    this.graphics.drawImage(this.image,xy[0],xy[1],wh[0],wh[1]);
                    WH = wh = xy = null;
                }
     
            var idoc = app.activeDocument;
            var img = File('~/Desktop/tempCapture.png'); // image place holder
     
            var captureOpts = new ImageCaptureOptions; // declare capture options, needed to trun on anti-alias
            captureOpts.matte = true; // to give transparent areas some color. default is white.
            captureOpts.antiAliasing = true; // anti-alias the image
            idoc.imageCapture (img, idoc.artboards[idoc.artboards.getActiveArtboardIndex()].artboardRect,captureOpts); // capture the active artboard
     
            var w = new Window('dialog','Preview active Artboard - Press Esc key to close'); // create a dialog
     
            var scr = $.screens[0]; // array of screens, my laptop = screen[0]
            var width = scr.right-scr.left; // get max width
            var height = scr.bottom - scr.top; // get max height
            
            w.size = [width, height]; // size the window
     
            //var img = File.openDialog ('select file'); // debug test different images
            var imgFrame = w.add('image',undefined,img); // add image container
            imgFrame.helpTip = 'Coded by CarlosCanto';
            imgFrame.title = 'Press Esc key to close';
            imgFrame.titleLayout = { alignment: ['center', 'center'] };
            
            imgFrame.size = [width-100,height-80]; // size container, a tad smaller than the window
            
            w.show(); 
         }
     else 
        {
            alert ("there are no open documents to preview");
        }
    
     
    |
    Mark as:
  • Currently Being Moderated
    May 31, 2012 7:11 PM   in reply to hilukasz

    Should be Class not Collection. Try this:

     

    SymbolItem.prototype.isIn = function(parentItem, runThis) {
    //do something
    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points