What’s the correct way of setting and fetching xmp data int Photoshop using UXP. The following is my called and after I run it and visit, File->File info ->RawData->
nothing has been added or removed and no error.
import PS from 'photoshop'
function* sample(){
//....code
try{
let uploadAsNewVersionSamples = yield uploadAsNewVersionSample(
PS.app.activeDocument,
)
// check the return
}catch(error){
console.log('error', error)
}
}
xmp function logic
async function uploadAsNewVersionSample(activeDocument: any) {
if (activeDocument) {
// sample data to be set eg from the server
let xmpMetadata = {
'dc:title': 'metadata sample',
'dc:description': 'brief age description',
}
async function fetchXmpBatchPlay() {
await require('photoshop').action.batchPlay(
[
{
_obj: 'setMetaData',
target: [
{
ref: 'document',
enum: 'ordinal',
value: 'targetEnum',
},
],
metadata: {
xmpMetadata,
},
},
],
{},
)
}
return await require('photoshop').core.executeAsModal(fetchXmpBatchPlay, {
command: 'saving metadata',
})
}
}
Let me check it out. I’ll give feedback
Thankyou so much for that. I used the above code but with a few modifications to suite my purpose and it worked. I have only one major issue, every time . why does the prefix so very often add _1 and increment it if the same file metadata is reached. eg, When you save the preference immediately the preference is undavide, then incase you needed to re-update the inner key the preference becomes undavide_1 and incase you update it again it becomes undavide_2. What could be the reason ?
thanks alot. This is what worked for me. I modified it to suit MANIFEST 5. Posting it here for anyone else who might get stuck . NB: modify it to suit your taste , incase of errors update me.
NB: typescript used
import { convert } from 'xmlbuilder2'
const psCore = require('photoshop').core
// constants
const prefix = 'sample'
const key = 'fileId'
const namespace = 'http://forums.creativeclouddeveloper.com/t/uxp-batchplay-setxmp-data/5956/4'
interface XmpMetaObject {
'x:xmpmeta': {
'rdf:RDF': {
'rdf:Description': {
'@xmlns': {
[key: string]: string
}
[key: string]:
| string
| {
[key: string]:
| string
| number
| {
[key: string]: string | number
}
}
}
}
}
}
// call to fetch documentXMP
let getDocumentXMP = async () => {
try {
async function fetchDocumentXmp() {
return await require('photoshop').action.batchPlay(
[
{
_obj: 'get',
_target: {
_ref: [
{ _property: 'XMPMetadataAsUTF8' },
{ _ref: 'document', _enum: 'ordinal', _value: 'targetEnum' },
],
},
},
],
{ synchronousExecution: true },
)[0].XMPMetadataAsUTF8
}
return await require('photoshop').core.executeAsModal(fetchDocumentXmp, {
command: 'setting document xmp',
})
} catch (error) {
console.error('error fetching document xmp')
// optional. shows an alert to the user.
psCore.showAlert({ message: 'error fetching document xmp ', error })
}
}
// Set the Doc XMP for file
const setDocumentXMP = async (xmpString: any) => {
function setXmpDocumentBatchPlayM() {
require('photoshop').action.batchPlay(
[
{
_obj: 'set',
_target: [
{ _ref: 'property', _property: 'XMPMetadataAsUTF8' },
{ _ref: 'document', _enum: 'ordinal', _value: 'targetEnum' },
],
to: {
_obj: 'document',
XMPMetadataAsUTF8: xmpString,
},
},
],
{},
)
}
try {
let results = await require('photoshop').core.executeAsModal(
setXmpDocumentBatchPlayM,
{
command: 'setting document xmp',
},
)
return results
} catch (error) {
console.error('Error setting document XMP ', error)
// optional. show an alert to user
psCore.showAlert({ message: 'Error setting document xmp ', error })
}
}
// Fetch document xmp keys
export const getDocumentXmpData = async () => {
const xmpString = await getDocumentXMP()
const obj = convert(xmpString, {
format: 'object',
}) as unknown as XmpMetaObject
return (
obj['x:xmpmeta']['rdf:RDF']['rdf:Description'][`${prefix}:${key}`] ||
'nothing'
)
}
// set document metadata
export const setDocumentXmp = async ( value : string) => {
try {
const xmpString = await getDocumentXMP()
const obj: any = 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)
} catch (error) {
console.log('Error set Document xmp metData ', error)
psCore.showAlert({ message: 'Error set Document xmp metData ', error })
}
}