getting XMP File does not have a constructor error through ExtendScript

不羁岁月 提交于 2019-12-11 09:05:08

问题


I am using In Design CC 2019, on my Mac OS. When I am trying to get XMP data for my .indd (InDesign document) using ExtendScript.

I am currently getting the error like this:

XMPFile Does not have a constructor.

Below is my script.

// load XMP Library
function loadXMPLibrary(){
    if ( ExternalObject.AdobeXMPScript){
        try{ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');}
        catch (e){alert('Unable to load the AdobeXMPScript library!'); return false;}
    }
    return true;
}



var myFile= app.activeDocument.fullName;

// check library and file
if(loadXMPLibrary() && myFile != null){
   xmpFile = new XMPFile(myFile.fsName, XMPConst.FILE_INDESIGN, XMPConst.OPEN_FOR_UPDATE);
   var myXmp = xmpFile.getXMP();
}

if(myXmp){
    $.writeln ('sucess')
 }


回答1:


There's an issue with your codes logic, you need to make the following change:

  1. Add the Logical NOT operator (i.e. !) to the condition specified for your if statement in the body of your loadXMPLibrary function.

    function loadXMPLibrary(){
        if (!ExternalObject.AdobeXMPScript) { // <--- Change to this
        //  ^
          try {ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');}
          catch (e){alert('Unable to load the AdobeXMPScript library!'); return false;}
        }
        return true;
    }
    

    You need to add this because currently your if statement checks whether the condition is truthy, i.e. it checks whether ExternalObject.AdobeXMPScript is true. This will always remain false, until the AdobeXMPScript library has been loaded, therefore you're code that actually loads the library never gets executed.

Revised script:

For clarity here is the complete revised script:

// load XMP Library
function loadXMPLibrary() {
    if (!ExternalObject.AdobeXMPScript) {
        try{ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');}
        catch (e){alert('Unable to load the AdobeXMPScript library!'); return false;}
    }
    return true;
}

var myFile= app.activeDocument.fullName;

// check library and file
if (loadXMPLibrary() && myFile !== null) {
    xmpFile = new XMPFile(myFile.fsName, XMPConst.FILE_INDESIGN, XMPConst.OPEN_FOR_UPDATE);
    var myXmp = xmpFile.getXMP();
}

if (myXmp){
    $.writeln ('success')
}


来源:https://stackoverflow.com/questions/56133209/getting-xmp-file-does-not-have-a-constructor-error-through-extendscript

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