Skip to main content
Participant
Nov 14, 2026 1:34 PM UTC
Answered

How do I write XMP data to an open file in photoshop without running into errors

I am working on a project, and I  am trying to write an id in each file so I can check if the file is new or has been opened before. if there’s another tool I can use it would really be helpful 

    Eugene TysonCorrect answer
    Community Expert
    Nov 14, 2026 2:41 PM UTC

    You can store a unique identifier in a Photoshop document using XMP metadata, then check for that identifier whenever the document is opened again.

    The approach depends on whether you're using ExtendScript (.jsx) or the newer UXP scripting environment.

    As an example this checks the active document and only adds an ID if one doesn't already exist:


    #target photoshop

    if (app.documents.length > 0) {

    if (ExternalObject.AdobeXMPScript === undefined) {
    ExternalObject.AdobeXMPScript =
    new ExternalObject("lib:AdobeXMPScript");
    }

    var doc = app.activeDocument;
    var ns = "https://example.com/photoshop/id/1.0/";

    XMPMeta.registerNamespace(ns, "myid");

    var xmp = new XMPMeta(doc.xmpMetadata.rawData);
    var existingID = xmp.getProperty(ns, "FileID");

    if (existingID) {
    alert("Existing document ID: " + existingID);
    } else {
    var newID = String(new Date().getTime()) +
    "-" + String(Math.floor(Math.random() * 1000000000));

    xmp.setProperty(ns, "FileID", newID);
    doc.xmpMetadata.rawData = xmp.serialize();

    alert("New document ID assigned: " + newID);
    }
    }

    One consideration is that Photoshop 27 introduced reported problems with XMP with UXP, so older code examples may not work in newer versions.

     

    If your only requirement is to know whether a file has previously been processed, you could maintain an external record of file identifiers rather than modifying the documents themselves.

    3 replies

    Participant
    Nov 14, 2026 2:45 PM UTC

    Thank you 🙏🏿 

    Translate
    Participant
    Nov 14, 2026 2:44 PM UTC

    I am using extendscript, I guess that’s the problem 

    I found exiftool, and I think it works for me 

    Translate
    Eugene TysonCommunity ExpertCorrect answer
    Community Expert
    Nov 14, 2026 2:41 PM UTC

    You can store a unique identifier in a Photoshop document using XMP metadata, then check for that identifier whenever the document is opened again.

    The approach depends on whether you're using ExtendScript (.jsx) or the newer UXP scripting environment.

    As an example this checks the active document and only adds an ID if one doesn't already exist:


    #target photoshop

    if (app.documents.length > 0) {

    if (ExternalObject.AdobeXMPScript === undefined) {
    ExternalObject.AdobeXMPScript =
    new ExternalObject("lib:AdobeXMPScript");
    }

    var doc = app.activeDocument;
    var ns = "https://example.com/photoshop/id/1.0/";

    XMPMeta.registerNamespace(ns, "myid");

    var xmp = new XMPMeta(doc.xmpMetadata.rawData);
    var existingID = xmp.getProperty(ns, "FileID");

    if (existingID) {
    alert("Existing document ID: " + existingID);
    } else {
    var newID = String(new Date().getTime()) +
    "-" + String(Math.floor(Math.random() * 1000000000));

    xmp.setProperty(ns, "FileID", newID);
    doc.xmpMetadata.rawData = xmp.serialize();

    alert("New document ID assigned: " + newID);
    }
    }

    One consideration is that Photoshop 27 introduced reported problems with XMP with UXP, so older code examples may not work in newer versions.

     

    If your only requirement is to know whether a file has previously been processed, you could maintain an external record of file identifiers rather than modifying the documents themselves.

    Translate