Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Extract portion of filename and add to IPTC Title

New Here ,
Sep 08, 2023 Sep 08, 2023

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;
};

 

TOPICS
How to , Metadata , Scripting
442
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 08, 2023 Sep 08, 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.

 

Translate
Community Expert ,
Sep 08, 2023 Sep 08, 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.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 08, 2023 Sep 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2023 Sep 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 08, 2023 Sep 08, 2023

Thank you - there's no immediate need to pull that information yet, it was mostly me taking it one step further for understanding what I was getting wrong the first time around. Thanks for the link - that's the resource I was looking for.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 08, 2023 Sep 08, 2023
LATEST

You're welcome.

 

Another good resource is the IPTC metadata spec. https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata

It will give you the XMP Spec for each field. For example: https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata#title

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines