Page 1 of 1

batch rename using description metadata

Posted: Thu Apr 11, 2013 6:46 am
by claudiu
Hello guy
Is there any chance to batch rename all my files using the "description" field from the metadata of a jpg image?
In this moment bridge doesn't have the description in this dropdown field only the ones below plus headline, urgency, title that are not visible


batch rename using description metadata

Posted: Fri Apr 12, 2013 5:42 am
by claudiu
nobody?

batch rename using description metadata

Posted: Fri Apr 12, 2013 10:23 am
by Paul MR
You could try this, it also sets the preserved filename so that you can revert if required...

Code: Select all#target bridge
if( BridgeTalk.appName == "bridge" ) {
RenDesc = new MenuElement("command", "Rename to Description", "at the end of tools");
}
RenDesc.onSelect = function () {
var sels = app.document.selections;
for(var i=0; i < sels.length; i++) {
try{
var thumb = sels;
var selectedFile = thumb.spec;
md = new Thumbnail(selectedFile).synchronousMetadata;
md.namespace = "http://purl.org/dc/elements/1.1/";
Desc = md.description;
if(Desc == '') continue;
Desc = Desc.toString().replace(/[:\/\\*\?\"\<\>\|]/g, "_");
md.namespace = "http://ns.adobe.com/xap/1.0/mm/";
md.PreservedFileName='';
md.PreservedFileName = selectedFile.name;
var ext = thumb.name.match(/\..+$/);
$.sleep(40);
File(selectedFile).rename((Desc+ext));
}catch(e){alert(e + " - " + e.line);}
}
};


batch rename using description metadata

Posted: Tue Apr 16, 2013 8:22 am
by claudiu
you are a genius Paul. Thank you a lot

batch rename using description metadata

Posted: Tue Apr 16, 2013 8:31 am
by claudiu
One more thing. For example through all my files will be also images without descrition in the metadata. could this script rename them with a letter or a number, or a sequence number? now i get this error and i can't close bridge only with end process


Uploaded with ImageShack.us

batch rename using description metadata

Posted: Tue Apr 16, 2013 5:59 pm
by Paul MR
The error is due to the nename being very fast and the metadata has not been updated.
I.E. the file name has been changed before the metadata field has been written to, that is why the $.sleep();
This time I have done two loops one to set the preserved name and one to do the rename, even doing it this way a sleep is required to allow time to update the metadata. If you still get an error increase the sleep time.

Code: Select all#target bridge
if( BridgeTalk.appName == "bridge" ) {
RenDesc = new MenuElement("command", "Rename to Description", "at the end of tools");
}
RenDesc.onSelect = function () {
var sels = app.document.selections;
try{
for(var i=0; i < sels.length; i++) {
var thumb = sels;
var selectedFile = thumb.spec;
md = new Thumbnail(selectedFile).synchronousMetadata;
md.namespace = "http://ns.adobe.com/xap/1.0/mm/";
md.PreservedFileName='';
md.PreservedFileName = selectedFile.name;
}
$.sleep(400);//allow time to update metadata fields
for(var z=0; z < sels.length; z++) {
var thumb = sels[z];
var selectedFile = thumb.spec;
md = new Thumbnail(selectedFile).synchronousMetadata;
md.namespace = "http://purl.org/dc/elements/1.1/";
Desc = md.description;
if(Desc == ''){
    //change to suit your needs
    Desc = "desc-" + zeroPad((z+1),4);
    }else{
Desc = Desc.toString().replace(/[:\/\\*\?\"\<\>\|]/g, "_");
}
var ext = thumb.name.match(/\..+$/);
File(selectedFile).rename((Desc+ext));
}
}catch(e){alert(e + " - " + e.line);}
function zeroPad(n, s) {
   n = n.toString();
   while (n.length < s)  n = '0' + n;
   return n;
};
};