Batch rename only some parts in IPTC description

General Discussion of Scripting for Adobe Bridge

Moderators: Tom, Kukurykus

Zanza

Batch rename only some parts in IPTC description

Post by Zanza »

Hello all, this is my first post, and I have browsed a lot of topics here to find if anybody has already found a scripting way to do the following task, but without success:

I have a catalog of pictures (Picasa) with a IPTC Core Description, which include informations and a website address in it. All these pictures are relaying in a forum and their informations is used in it

I need to modify only the website address but untouching the other informations available in the description of all the pictures. So I can't replace the whole Description of each files, but only part of their Description

Thanks in advance
Paul MR

Batch rename only some parts in IPTC description

Post by Paul MR »

You could try this...

Code: Select all#target bridge
var sels = app.document.selections;
for (var a in sels){
    updateDescription(sels[a], "www.mynewsite.co.uk")
}

function updateDescription(thumb, newWebSite){
try{
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var selectedFile = new Thumbnail(thumb).spec;   
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
var myXmp = myXmpFile.getXMP();
var Desc=[];
var count = myXmp.countArrayItems(XMPConst.NS_DC, "description");
for(var i = 1;i <= count;i++){
Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "description", i));
}
//Amend to suit, this assumes the web address starts with www
Desc = Desc.toString().replace(/www[^( |\r|\n)]*/i,newWebSite);
myXmp.deleteProperty(XMPConst.NS_DC, "description");
myXmp.setLocalizedText( XMPConst.NS_DC, "description", null, "x-default", Desc );
if (myXmpFile.canPutXMP(myXmp)) {
       myXmpFile.putXMP(myXmp);
         myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
         }
     }catch(e){alert(e+ " occured at line : " + e.line);}
}


Zanza

Batch rename only some parts in IPTC description

Post by Zanza »

Thanks Paul for your prompt reply
One little question: is it working also in CS3 (as I have only CS3 at home). Or I'll try tomorrow at work on CS5.

And one more question: how do I use your code in Bridge ?? Simply saving it and importing as a script or ???
Paul MR

Batch rename only some parts in IPTC description

Post by Paul MR »

Yes this should work with CS3 or better.
The script above was an example that could be run from ExtendScript.
This version can be installed in Bridge and run from there.

Make sure you change the WEB address!

To install the script..

Copy and paste the script into ExtendScript Toolkit
This gets installed with Photoshop and can be found:-
PC: C:\Program Files\Adobe\Adobe Utilities
MAC: <hard drive>/Applications/Utilities/Adobe Utilities

Start Bridge
PC: Edit - Preferences - Startup Scripts
Mac: Adobe Bridge menu - Preferences - Startup Scripts
At the bottom click the "Reveal Button" this will open the folder where the script should be saved.
Close and restart Bridge.
Accept the new script.

To use : select the documents and then Tools - Change WEB site

Code: Select all#target bridge   
if( BridgeTalk.appName == "bridge" ) { 
var changeWebAddress = new MenuElement( "command", "Change WEB site", "at the end of Tools" , "xx1C" );
}
changeWebAddress.onSelect = function () {
var sels = app.document.selections;
for (var a in sels){
    //Change web site to suit
    updateDescription(sels[a], "www.mynewsite.co.uk")
}

function updateDescription(thumb, newWebSite){
try{
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var selectedFile = new Thumbnail(thumb).spec;   
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
var myXmp = myXmpFile.getXMP();
var Desc=[];
var count = myXmp.countArrayItems(XMPConst.NS_DC, "description");
for(var i = 1;i <= count;i++){
Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "description", i));
}
//Amend to suit, this assumes the web address starts with www
Desc = Desc.toString().replace(/www[^( |\r|\n)]*/i,newWebSite);
myXmp.deleteProperty(XMPConst.NS_DC, "description");
myXmp.setLocalizedText( XMPConst.NS_DC, "description", null, "x-default", Desc );
if (myXmpFile.canPutXMP(myXmp)) {
       myXmpFile.putXMP(myXmp);
         myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
         }
     }catch(e){alert(e+ " occured at line : " + e.line);}
}
}

Zanza

Batch rename only some parts in IPTC description

Post by Zanza »

Thanks Paul, it's great, but I think my explanation were not sufficenty accurate I think, sorry

I was in need to change the website adress and retain all others infos... Your script is perfect for this, tested (only on 2 pics...) and it works well


But originally, the website adress is:

http://www.XXXX.org/forum/viewtopic.php?t=xyz123

And it got to change to:

http://forum.ZZZZ.org/viewtopic.php?t=xyz123


The unique topic xyz123 has to be retained and only part (in Bold) of the web adress should got changed


Sorry to bother and to be not sufficently precise in my request (I'm not en english speaking person BTW )
Paul MR

Batch rename only some parts in IPTC description

Post by Paul MR »

I see, this should do it...
http://www.XXXX.org/forum/viewtopic.php?t=xyz123
to
http://forum.ZZZZ.org/viewtopic.php?t=xyz123

Code: Select all#target bridge   
if( BridgeTalk.appName == "bridge" ) { 
var changeWebAddress = new MenuElement( "command", "Change WEB site", "at the end of Tools" , "MMX1" );
}
changeWebAddress.onSelect = function () {
var sels = app.document.selections;
for (var a in sels){
    updateDescription(sels[a])
}

function updateDescription(thumb){
try{
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var selectedFile = new Thumbnail(thumb).spec;   
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
var myXmp = myXmpFile.getXMP();
var Desc=[];
var count = myXmp.countArrayItems(XMPConst.NS_DC, "description");
for(var i = 1;i <= count;i++){
Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "description", i));
}
///////////// Change to suit. NB a / needs to be escaped with a backslash IE: \/
Desc = Desc.toString().replace(/http:\/\/www.XXXX.org\/forum\//i,"http:\/\/forum.ZZZZ.org\/");
myXmp.deleteProperty(XMPConst.NS_DC, "description");
myXmp.setLocalizedText( XMPConst.NS_DC, "description", null, "x-default", Desc );
if (myXmpFile.canPutXMP(myXmp)) {
       myXmpFile.putXMP(myXmp);
         myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
         }
     }catch(e){alert(e+ " occured at line : " + e.line);}
}
}



Or ....
Code: Select all#target bridge   
if( BridgeTalk.appName == "bridge" ) { 
var changeWebAddress = new MenuElement( "command", "Change WEB site", "at the end of Tools" , "MMX1" );
}
changeWebAddress.onSelect = function () {
var sels = app.document.selections;
for (var a in sels){
    //Change web site to suit
    updateDescription(sels[a], "http:\/\/forum.ZZZZ.org\/viewtopic.php?t=xyz123")
}

function updateDescription(thumb, newWebSite){
try{
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var selectedFile = new Thumbnail(thumb).spec;   
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
var myXmp = myXmpFile.getXMP();
var Desc=[];
var count = myXmp.countArrayItems(XMPConst.NS_DC, "description");
for(var i = 1;i <= count;i++){
Desc.push(myXmp.getArrayItem(XMPConst.NS_DC, "description", i));
}
//Amend to suit, this assumes the web address starts with http
Desc = Desc.toString().replace(/http[^( |\r|\n)]*/i,newWebSite);
myXmp.deleteProperty(XMPConst.NS_DC, "description");
myXmp.setLocalizedText( XMPConst.NS_DC, "description", null, "x-default", Desc );
if (myXmpFile.canPutXMP(myXmp)) {
       myXmpFile.putXMP(myXmp);
         myXmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
         }
     }catch(e){alert(e+ " occured at line : " + e.line);}
}
}

Zanza

Batch rename only some parts in IPTC description

Post by Zanza »

Paul, thanks a million times, it apparently works well