Debug JavaScript injection in ActionScript (Flash Builder)

心不动则不痛 提交于 2019-12-25 02:49:09

问题


I'm working on a InDesign CS6 Extension in Adobe Flash Builder 4.6. For some reason my code (which worked fine for a long time) now throws the error null is not an object. The error is located in a javascript injection (last line):

public class Script {
    private static var _instance:Script;

    [ Embed (source="script.jsx", mimeType="application/octet-stream") ]
    private var ScriptClass:Class;
    private var jsxInterface:HostObject;

    public function Script() {
        if (Script._instance) {
            throw new Error("only single instance allowed");
        }
        Script._instance = this;
        this.init();
    }

    public static function getInstance():Script {
        return _instance;
    }

    private function init():void {
        Log.log("HostObject.mainExtension: "+HostObject.mainExtension);
        for each (var s:String in HostObject.extensions) {
            Log.log("Extension: "+s);
        }

        this.jsxInterface = HostObject.getRoot(HostObject.mainExtension);
        this.jsxInterface.eval(new ScriptClass().toString());
    }

    public function getScript(name:String):Object {
        return this.jsxInterface[name];
    }

    public function exec(name:String, args:Array = null):Object {
        return InDesign.app.doScript(
            this.jsxInterface[name], ScriptLanguage.javascript, args, UndoModes.AUTO_UNDO); // <-- this is where the error appears
    }

I'v checked the arguments of InDesign.app.doScript for null, but everything is ok. This is the function (inside script.jsx) that is being called:

function prepareForImageExport(params) {
    var pageItem = params[0];
    var prefix = params[1];
    var bounds = params[2];
    var ax = params[3];
    var ay = params[4];

    pageItem.visible = true;

    // create tmp container
    var container = app.activeDocument.rectangles.add(app.activeDocument.activeLayer);

    container.name = prefix+container.id;
    container.geometricBounds = bounds;
    container.strokeWeight = 0;
    container.strokeColor = app.activeDocument.swatches.item("None");
    container.fillColor = app.activeDocument.swatches.item("None");
    container.visible = true;
    container.transparencySettings.blendingSettings.opacity = 100;

    // create a duplicate of the pageItem in the tmp container
    var copyItem = pageItem.duplicate(app.activeDocument.activeLayer);
    copyItem.transparencySettings.blendingSettings.opacity = 100;
    copyItem.locked = false;
    container.geometricBounds = bounds;
    container.move([ax,ay]);
    copyItem.visible = true;

    app.select(copyItem);
    app.cut();
    app.select(container);
    app.pasteInto();

    app.scriptArgs.setValue("container", container.name);
}

At this place I'm stuck. I don't know in which line of the javascript the error appears.

I'm very new to ActionScript and I can't seem to find a documetation about how to debug Javascript injections in ActionScript. Also I don't really know, which variables (like app) are accessible inside the javascript code and which ones (like console - I can't write console.log) are not.

Any help is greatly appreciated!


回答1:


You should first try to check that the arguments/params sent to javascript are valid(not null or undefined there) after that try to log every line in the JS code to see where it crashes, I am not sure how the JS code is executed in your case(I did not made extensions) but if it uses the HtmlLoader then you can access the window object of the loader and add to it a log function or a console object that has a log function.




回答2:


Check first this.jsxInterface if it's a valid object returned. You can use the debug as mode and set a breakpoint there. Then look at this.jsxInterface[name].

I havn't made any extensions since a couple of years but jsxInterface should be reachable without "this." as you set it as a variable above.

For what it's worth, I would not try to debug the js code within Flex Builder. What you can do is use a log function (http://debrouwere.github.io/Extendables/docs/packages/logging/doc/readme.html) then look at what you receive from Flex. Beware that unsurprisingly when you pass Flex Objects, you actually receive Flex objects in the ExtendScript context.

But to be brief, your error signifies that you are trying to use an object that doesn't exist (this.jsxInterface in my humble opinion). Maybe you js code is just fine, maybe the parameters too but the problem should be located in the inbetweens.

And for flex logging you can use "trace()" but you need to be in a debug mode.



来源:https://stackoverflow.com/questions/30209254/debug-javascript-injection-in-actionscript-flash-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!