编程语言
首页 > 编程语言> > javascript – 需要XMP对象的setProperty语法

javascript – 需要XMP对象的setProperty语法

作者:互联网

我随机生成DocumentID和InstanceID,但在将属性DocumentID和InstanceID设置为xmp对象时遇到问题.

如何将生成的DocumentID和InstanceID设置为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()方法来创建和设置DocumentID和InstanceID的值

下面是一些用于添加DocumentID和InstanceID的辅助函数.

// 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());

附加说明:

在为DocumentID和InstanceID生成值时,指南指出:

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

摘录(上)见于Partner’s guide to XMP for Dynamic Media第19页(PDF)

遗憾的是,ExtendScript不提供内置功能来生成全局唯一标识符(GUID).但是,macOS确实包含uuidgen,它是一个命令行实用程序/库
生成唯一标识符(UUID / GUID).

辅助函数(上图):

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

仅在macOS上运行.它利用AppleScript运行uuidgen命令.

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

标签:extendscript,adobe,javascript,xmp,adobe-indesign
来源: https://codeday.me/bug/20191003/1847954.html