Search code examples
javascriptadobeadobe-indesignextendscriptxmp

need setProperty syntax for XMP object


I am randomly generating DocumentID and InstanceID, but facing problem in setting property DocumentID and InstanceID to the xmp object.

How can I set the generated DocumentID and InstanceID to my 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);     
} 

Solution

  • You can utilize the setProperty() method in the AdobeXMPScript library to create and set the value for DocumentID and InstanceID

    Below are a couple of helper functions for adding a DocumentID and 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);
    }
    

    To add an instanceID invoke the addXmpPropertyAndValue function as follows:

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

    To add an DocumentID invoke the addXmpPropertyAndValue function as follows:

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

    Additional Note:

    When generating the value(s) for DocumentID and InstanceID the guidelines state:

    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

    The excerpt (above) can be found on page 19 of Partner's guide to XMP for Dynamic Media (PDF)

    Unfortunately, ExtendScript does not provide a built-in feature to generate a globally unique identifier (GUID). However macOS does include uuidgen which is a command-line utility/library for generating unique identifiers (UUID/GUID).

    The helper function (above):

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

    runs on macOS only. It utilizes AppleScript to run the uuidgen command.

    You may want to generate the identifier this way instead of your current randomString(32) function call.