Search code examples
javascriptmetadataadobe-indesign

Indesign script list all authors from metadata in an alert


I want to list all authors that are stored in the metadata in an alert message (As a finishing alert after they were added via a script). But my script always only shows the first entry. The names are separated by a semicolon in the document info.

I wrote the following script with the help of chatGPT. (I'm still a beginner in scripting but I'm learning a lot with it.)

Does anyone know how to access all entries?

// Load the XMP Toolkit library
ExternalObject.TI = ExternalObject.TI || {};
ExternalObject.TI["AdobeXMPScript"] = "XMPScript";

// Get the XMP metadata of the current document
var xmp = app.activeDocument.metadataPreferences;

// Get the document title from the XMP metadata
var docTitle = xmp.documentTitle;

// Get the dc:creator values from the XMP metadata
var creatorString = xmp.author;
var creators = creatorString.split(";");

// Remove any extra whitespace or semicolons from each creator name
for (var i = 0; i < creators.length; i++) {
  creators[i] = creators[i].replace(/^\s+|\s+$/gm,'');
}

// Create a message with the document title and the list of creators
var message = "Document title: " + docTitle + "\nCreators:\n";
for (var i = 0; i < creators.length; i++) {
  message += "- " + creators[i] + "\n";
}

// Display the message in an alert box
alert(message);

Edit: Since I already got the one minus point here (I assume because of the use of ChatGPT): I worked on that script for hours. I didn't just put in information and got it like that.


Solution

  • Try this:

    var xmp = app.activeDocument.metadataPreferences;
    var ns = 'http://purl.org/dc/elements/1.1/';
    var num = xmp.countContainer(ns, 'creator');
    var creators = [];
    for (var i = 1; i <= num; i++) {
        creators.push(xmp.getProperty(ns, 'dc:creator[' + i + ']'));
    }
    alert('Creators:\n' + creators.join('\n'));
    

    Or this (about the same approach):

    var xmp = app.activeDocument.metadataPreferences;
    var ns = 'http://purl.org/dc/elements/1.1/';
    var creators = [];
    var i = 1;
    while (с = xmp.getProperty(ns, 'dc:creator[' + i++ + ']')) creators.push(с);
    alert('Creators:\n' + creators.join('\n'));