Skip to main content
Known Participant
July 22, 2021
Answered

Filter Photoshop Keywords | Find & Replace

  • July 22, 2021
  • 3 replies
  • 715 views

Let's say my Photoshop keywords are:

cat; dog; mouse

 

I want to filter through the keywords, find "cat" and replace it with "elephant" via javascript. 

 

How do I filter through photoshop's keywords to replace keywords?

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Yes it is, but the speed really counts when there are really many items 😉


If the OP wants to run the Script on a largish batch of images it might pay off. 

3 replies

Legend
July 22, 2021

You are aware that there are four different keyword namespaces in EXIF/XMP? Do you want to limit this to dc:subject or address the others as well?

c.pfaffenbichler
Community Expert
Community Expert
July 22, 2021
// 2021, use it at your own risk;
if (documents.length > 0) {
var myDocument = activeDocument;
    var theKeyw = myDocument.info.keywords;
    alert (theKeyw.join("\n"));
    var newKeyw = new Array;
    var check = false;
    for (var m = 0; m < theKeyw.length; m++) {
        var thisOne = theKeyw[m];
        if (thisOne == "cat") {newKeyw.push("elephant");
        check = true}
        else {newKeyw.push (thisOne)}
    };
    if (check == true) {myDocument.info.keywords = newKeyw};
};
Known Participant
July 22, 2021

Thank you once again. I had no clue how to do that. 🙂

Kukurykus
Legend
July 22, 2021
'cat; dog; mouse'.replace(/cat/, 'elephant')

or:

'cat; dog; mouse'.split('cat').join('elephant')
Known Participant
July 22, 2021

I tried the below, because I need to call to the the Document Keywords first before I can search for cat - right? I tried the below but it didnt work. 

 

 

 

var doc = app.activeDocument;
var docKeywords = doc.info.keywords;

docKeywords.replace(/cat/, 'elephant')
or 
docKeywords.split('cat').join('elephant')

 

Kukurykus
Legend
July 22, 2021

 

inf.keywords = (inf = activeDocument.info).keywords
.toString().replace(/cat/gi, 'elephant').split(',')

 

or:

 

with(activeDocument.info)
	keywords = keywords.toString()
	.split('cat').join('elephant').split(',')