Skip to main content
wanokuni
Known Participant
August 13, 2021
Answered

IPTC Title

  • August 13, 2021
  • 2 replies
  • 585 views

Hi guys, 

I am trying to write a scipt that add Title from the document name only if the title is empty. here is what i did so far but it does not work, not sure why. sometime the title field do have a name on it and I dont want to overwrite it. 

const doc = app.activeDocument;
const docName = doc.name.slice(0, -4);

if (doc.info.title == undefined) {
    try {
        doc.info.title = docName;

    } catch (e) {}
}
This topic has been closed for replies.
Correct answer Stephen Marsh

My take:

 

var doc = app.activeDocument;
var docName = doc.name.replace(/\.[^\.]+$/, '');
if (doc.info.title.length === 0) {
    try {
        doc.info.title = docName;
    } catch (e) {}
}

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
August 14, 2021

My take:

 

var doc = app.activeDocument;
var docName = doc.name.replace(/\.[^\.]+$/, '');
if (doc.info.title.length === 0) {
    try {
        doc.info.title = docName;
    } catch (e) {}
}
Kukurykus
Legend
August 14, 2021
with(activeDocument) !info.title && info.title = name.slice(0, -4)
Stephen Marsh
Community Expert
Community Expert
August 14, 2021

@Kukurykus 

That is a thing of beauty, and for once – I actually understand your code!  :]

Kukurykus
Legend
August 13, 2021

 

if (!(len = String(XML(rd = (aD = activeDocument).xmpMetadata
.rawData)['rdf:RDF']['rdf:Description']['dc:title']).length) ||
(len && new XMPMeta(rd).getLocalizedText(XMPConst.NS_DC, 'title', 0, 0))) {
	fsName = activeDocument.fullName.fsName; if (!ExternalObject.AdobeXMPScript)
		ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript')
	xmp = new XMPFile(fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE)
	md = new XMPMeta(fsName), md.setLocalizedText(XMPConst.NS_DC,
	'title', 0, 0, aD.name.split(/\..{3,4}$/)[0]), xmp.putXMP(md)
	xmp.closeFile(XMPConst.CLOSE_UPDATE_SAFELY)
}

 

wanokuni
wanokuniAuthor
Known Participant
August 14, 2021

Thanks a lot works perfctly, man I just start learning scripting, I can see i have long way to go, i try to study your script still dont understand it well yet.