XMP 对象需要 setProperty 语法

need setProperty syntax for XMP object

我正在随机生成 DocumentIDInstanceID,但在将 属性 DocumentIDInstanceID 设置为 xmp 对象时遇到问题。

如何将生成的 DocumentIDInstanceID 设置为我的 allXMP

var xmpFile = new XMPFile(linkFilepath, XMPConst.FILE_INDESIGN, XMPConst.OPEN_FOR_UPDATE);
var allXMP = xmpFile.getXMP();

// Retrieve values from external links XMP.
var documentID = allXMP.getProperty(XMPConst.NS_XMP_MM, 'DocumentID', XMPConst.STRING);
var instanceID = allXMP.getProperty(XMPConst.NS_XMP_MM, 'InstanceID', XMPConst.STRING);

documentID =  randomString(32);
instanceID = randomString(32);

// ???? Here I need to set DocumentID and InstanceID to allXMP

if (xmpFile.canPutXMP(allXMP)) {     
    xmpFile.putXMP(allXMP);    
    xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);     
} 

您可以利用 AdobeXMPScript 库中的 setProperty() 方法来创建和设置 DocumentIDInstanceID[=32 的值=]

下面是一些用于添加 DocumentIDInstanceID 的辅助函数。

// Note: This function works on macOS only
function generateUUID() {
  var cmd = 'do shell script "uuidgen | tr -d " & quoted form of "-"';
  return app.doScript(cmd, ScriptLanguage.applescriptLanguage);
}

// Add an XMP property and Value.
function addXmpPropertyAndValue(filePath, xmpProperty, xmpValue) {
  var xmpFile = new XMPFile(filePath, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
  var allXMP = xmpFile.getXMP();

  allXMP.setProperty(XMPConst.NS_XMP_MM, xmpProperty, xmpValue);

  if (xmpFile.canPutXMP(allXMP)) {
    xmpFile.putXMP(allXMP);
  }

  xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);

  // Useful for testing purposes....
  alert('Added: ' + xmpProperty + '\n' +
      'value: ' + xmpValue + '\n\n' +
      'Path: ' + filePath, 'Updated XMP', false);
}

要添加 instanceID 调用 addXmpPropertyAndValue 函数,如下所示:

// The `linkFilepath` argument should be the filepath to the Link you want to update
addXmpPropertyAndValue(linkFilepath, 'InstanceID', 'xmp.iid:' + generateUUID());

要添加 DocumentID 调用 addXmpPropertyAndValue 函数,如下所示:

// The `linkFilepath` argument should be the filepath to the Link you want to update
addXmpPropertyAndValue(linkFilepath, 'DocumentID', 'xmp.did:' + generateUUID());

补充说明:

在为 DocumentIDInstanceID 生成值时,指南指出:

An ID should be guaranteed to be globally unique (in practical terms, this means that the probability of a collision is so remote as to be effectively impossible). Typically 128- or 144-bit numbers are used, encoded as hexadecimal strings

可在 第 19Partner's guide to XMP for Dynamic Media (PDF) 中找到摘录(上文)

遗憾的是,ExtendScript 不提供 built-in 生成全局唯一标识符 (GUID) 的功能。但是 macOS 确实包含 uuidgen 这是一个 command-line utility/library 生成唯一标识符 (UUID/GUID)。

辅助函数(上):

function generateUUID() {
  var cmd = 'do shell script "uuidgen | tr -d " & quoted form of "-"';
  return app.doScript(cmd, ScriptLanguage.applescriptLanguage);
}

运行s 仅适用于 macOS。它利用 AppleScript 来 运行 uuidgen 命令。

您可能希望以这种方式生成标识符,而不是当前的 randomString(32) 函数调用。