Skip to main content
Participant
September 8, 2023
Answered

Extract portion of filename and add to IPTC Title

  • September 8, 2023
  • 1 reply
  • 730 views

Hi - I've found something similar on here from some time ago, but altering the code has stumped me. Our naming convention is follows this pattern :  MMC-1234_20200304_123456. We'd like to take everything before the first underscore (MMC-1234) and insert that into the IPTC Title field. 

 

I have a script from an earlier post that does something similar, but I've tried to learn how to update the code without luck. I'll post it here in case it's just a line or two change. TIA!

 

#target bridge

if(BridgeTalk.appName == "bridge" ) {
	addTitle = MenuElement.create("command", "Add Title2", "at the end of Tools");
}

addTitle.onSelect = function () {
	var thumbs = app.document.selections;
	if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");

	for(var a = 0; a < thumbs.length; a++) {
		var selectedFile = new Thumbnail(thumbs[a]);
		Title = decodeURI(selectedFile.spec.name).split("_");
		if (Title.length < 3) continue;
		newTitle = (Title[1] + "_" + Title[2]);
		app.synchronousMode = true;
		var xmp = new XMPMeta(selectedFile.synchronousMetadata.serialize());
		xmp.setLocalizedText( XMPConst.NS_DC, "title", null, "en-US", newTitle);
		var newPacket = xmp.serialize(XMPConst.SERIALIZE_USE_COMPACT_FORMAT);
		selectedFile.metadata = new Metadata(newPacket);
	}

	ExternalObject.AdobeXMPScript.unload();
	ExternalObject.AdobeXMPScript = undefined;
};

 

This topic has been closed for replies.
Correct answer gregreser

Yes, just one line needs to be changed:

newTitle = (Title[1] + "_" + Title[2]);

should be:

newTitle = (Title[0]);

 

The filename is being split into an array using "_" as a delimiter, so it would end up being this array:

["MMC-1234", "20200304", "123456"]

newTitle = (Title[0]) means use the first item in the array: "MMC-1234" as the new title value.

 

1 reply

gregreser
gregreserCorrect answer
Legend
September 8, 2023

Yes, just one line needs to be changed:

newTitle = (Title[1] + "_" + Title[2]);

should be:

newTitle = (Title[0]);

 

The filename is being split into an array using "_" as a delimiter, so it would end up being this array:

["MMC-1234", "20200304", "123456"]

newTitle = (Title[0]) means use the first item in the array: "MMC-1234" as the new title value.

 

Participant
September 8, 2023

I knew that was the line to change! Thank you, and for the explanation as well. So if I used (Title[1]) that would pull the date as well, I'm assuming (2 being Time) and I could potentially change the destination field to something more appropriate.

gregreser
Legend
September 8, 2023

That's right. Arrays are 0-indexed, so you would just figure out which item you want to use and count from 0.

There are other was to work with text, like regular expressions. It just depends on how consistent your source is and what you want to do with.

 

If you want to write to a different field, you need to set the namespace, property name, and type correctly. Title is localized text, but there are also plain text, bag, and seq properties. If you saving to a date field, there is a specific format you have follow.

 

There's a lot good information and example scripts in the Bridge ExtendScript SDK:

https://developer.adobe.com/console/servicesandapis

Scroll down the page to the Bridge downloads.

 

I'm happy to help if you want to add another field.