script to insert file name into IPTC Headline field

General Discussion of Scripting for Adobe Bridge

Moderators: Tom, Kukurykus

goofy167

script to insert file name into IPTC Headline field

Post by goofy167 »

Mike Hale wrote this great script that applies the filename to the metadata "Source" field. I would like to use it but apply the filename to the "Headline" field AND remove the extension ".jpg". I don't know if Mike is around but does anyone know how to modify it?

#target bridge

addNametoMeta = {};

addNametoMeta.execute = function(){
var sels = app.document.selections;
for (var i = 0; i < sels.length; i++){

var md = sels.synchronousMetadata;
md.namespace = "http://ns.adobe.com/photoshop/1.0/";
md.Source = sels.name;
}
}

// this script only works in bridge
if (BridgeTalk.appName == "bridge"){

var menu = MenuElement.create( "command", "Save Filename in Metadata", "at the end of Tools");
menu.onSelect = addNametoMeta.execute;
}
Mike Hale

script to insert file name into IPTC Headline field

Post by Mike Hale »

Because headline is in the same namespace you just need to change one line

Code: Select allmd.Source = sels.name;

to:

Code: Select allmd.Headline =  sels.name.substring(0,sels.name.lastIndexOf('.'));
goofy167

script to insert file name into IPTC Headline field

Post by goofy167 »

Wow that sure was easy. THANK YOU. May I ask about removing the extension, is that a more complex issue? I really didn't know where to start in dealing with it.
Mike Hale

script to insert file name into IPTC Headline field

Post by Mike Hale »

The line I posted should remove the extension. That is the substring - lastIndexOf part.

It takes a substring of name starting at the beginning until the last '.' so an image named 'myImage.jpg' becomes 'myImage'

Mike
goofy167

script to insert file name into IPTC Headline field

Post by goofy167 »

Wow Mike I am in awe of your master kahuna scripting skills, you make it look so easy.

How can one line of JavaScript do all that, and btw it works like a charm!

Code: Select allmd.Headline =  sels.name.substring(0,sels.name.lastIndexOf('.'));


So in this code is the loop stepping through each letter of the file name?
I'd like to understand how it works so I can say learn how t append my own text to it. Is any of this documented in the adobe JavaScript manual?

Sorry for my ignorance.
Mike Hale

script to insert file name into IPTC Headline field

Post by Mike Hale »

Those are two of the String methods. Abode's docs for the most part only deal with javascript that is unique to Abobe's products.

Here is where you can find more info about strings on the web http://www.w3schools.com/jsref/jsref_obj_string.asp
goofy167

script to insert file name into IPTC Headline field

Post by goofy167 »

OK I get it. Can you tell me where to find out what something like

app.document.selections;

means?

or

addNametoMeta.execute
Mike Hale

script to insert file name into IPTC Headline field

Post by Mike Hale »

Info about app.document.selections can be found in the Bridge javascripting guide.

addNametoMeta.execute is the name of the function that I wrote. I could have called it anything as long as that matched the "menu.onSelect = .newFunctionName"

So perhaps I should have called it 'storeSelectedFullNameInSource.execute" then have "menu.onSelect = storeSelectedFullNameInSource.execute". You could name your version "storeSelectedNameInHeadline.execute" or something like that.

It doesn't matter what the function is name to Bridge. Is just runs whatever code is in the function when that menu item is selected. The name only matter to people reading the script and trying to understand what it does. Which is why storeSelectedFullNameInSource is a better name. It lets the reader know what the function does.

Mike
goofy167

script to insert file name into IPTC Headline field

Post by goofy167 »

That is very clear to me.

This code snippet seems like a great place to start my education.

I know this might be over the top but could you add comments to your code?

Being a novice if even a small number of comments were included I believe I could understand better how it works. I am not a C expert but I have downloaded the Adobe Bridge Javascript PDF manual and will study it to try and understand things like BridgeTalk.appName and var md = sels.synchronousMetadata; but some comments would help me move much faster in my learning curve.

If that is too much trouble I completely understand, you have taken me a long way in the right direction Mike.

Mitch Waite Group
Mike Hale

script to insert file name into IPTC Headline field

Post by Mike Hale »

Code: Select all#target bridge // let EntendScript know what app the script is for

addNametoMeta = {};// create an object

addNametoMeta.execute = function(){// create a method for that object
var sels = app.document.selections;// store the array of selected files
for (var i = 0; i < sels.length; i++){//loop though that array

var md = sels.synchronousMetadata;// get the metadata for the file
md.namespace = "http://ns.adobe.com/photoshop/1.0/";// set the namespace
md.Source = sels.name;//set the porperty
}
}

// this script only works in bridge
if (BridgeTalk.appName == "bridge"){
//creage the munuItem
var menu = MenuElement.create( "command", "Save Filename in Metadata", "at the end of Tools");
menu.onSelect = addNametoMeta.execute;
}

Here is the script with short comments. Things like 'MenuElement.create' you can find in the Bridge guide.

For what namespace to use, I open a file in Photoshop. Choose File-File Info. Then play around with entering data the looking in the advance section to see where that data is stored. There may be a better way but it works for me.

Mike