Document XMP metadata

Solved :wine_glass:
I had to use a different XML library called xmlbuilder2.

const { convert } = require('xmlbuilder2');

// GETTER
// -------------------------------------------------
// <rdf:Description xmlns:undavide="http://davidebarranca.com/notes/" etc...>
// <undavide:Notes>someValue</undavide:Notes>
// -------------------------------------------------
// key: Notes
// prefix: undavide
// namespace: http://davidebarranca.com/notes/

export const getDocumentMetadata = ({ key, prefix = 'undavide', namespace = 'http://davidebarranca.com/notes/' }) => {
  const xmpString = getDocumentXMP();
  const obj = convert(xmpString, { format: "object" });
  return obj["x:xmpmeta"]['rdf:RDF']['rdf:Description'][`${prefix}:${key}`] || "nothing";
}

// SETTER
// -------------------------------------------------
// <rdf:Description xmlns:undavide="http://davidebarranca.com/notes/" etc...>
// <undavide:Notes>someValue</undavide:Notes>
// -------------------------------------------------
// key: Notes
// value: someValue
// prefix: undavide
// namespace: http://davidebarranca.com/notes/

export const setDocumentMetadata = async ({ key, value, prefix = 'undavide', namespace = 'http://davidebarranca.com/notes/' }) => {
  const xmpString = getDocumentXMP();
  const obj = convert(xmpString, { format: "object" });
  obj["x:xmpmeta"]['rdf:RDF']['rdf:Description'][`@xmlns:${prefix}`] = namespace;
  obj["x:xmpmeta"]['rdf:RDF']['rdf:Description'][`${prefix}:${key}`] = value;
  const newXmpString = convert(obj, {format: "xml"})
  await setDocumentXMP(newXmpString);
}

Might need a bit of extra testing for edge cases. What fun XML is, who would have thought :slight_smile:
If you run the prerelease version of PS see my post in the forums there for details on the BatchPlay call.

Davide

6 Likes