通过ExtendScript获取XMP文件没有构造函数错误

时间:2019-05-14 14:47:54

标签: debugging adobe adobe-indesign extendscript xmp

我在Mac OS上使用In Design CC 2019。当我尝试使用XMP获取.indd(InDesign文档)的ExtendScript数据时。

我目前收到这样的错误:

  

XMPFile Does not have a constructor

下面是我的脚本。

// 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')
 }

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

您的代码逻辑存在问题,您需要进行以下更改:

  1. !函数主体中为if语句指定的条件中添加Logical NOT operator(即loadXMPLibrary)。

    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;
    }
    

    您需要添加它,因为当前您的if语句检查条件是否为真,即它检查ExternalObject.AdobeXMPScript是否为true。在加载AdobeXMPScript库之前,它将一直保持false的状态,因此您实际上是从未加载该库的代码。

修订后的脚本:

为清楚起见,以下是完整的修订脚本:

// 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')
}