Copy link to clipboard
Copied
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?
// 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};
};
inf.keywords = (inf = activeDocument.info).keywords
.toString().replace(/cat/gi, 'elephant').split(',')
or:
with(activeDocument.info)
keywords = keywords.toString()
.split('cat').join('elephant').split(',')
If the OP wants to run the Script on a largish batch of images it might pay off.
Copy link to clipboard
Copied
'cat; dog; mouse'.replace(/cat/, 'elephant')
or:
'cat; dog; mouse'.split('cat').join('elephant')
Copy link to clipboard
Copied
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')
Copy link to clipboard
Copied
inf.keywords = (inf = activeDocument.info).keywords
.toString().replace(/cat/gi, 'elephant').split(',')
or:
with(activeDocument.info)
keywords = keywords.toString()
.split('cat').join('elephant').split(',')
Copy link to clipboard
Copied
That should be faster.
Copy link to clipboard
Copied
Yes it is, but the speed really counts when there are really many items 😉
Copy link to clipboard
Copied
If the OP wants to run the Script on a largish batch of images it might pay off.
Copy link to clipboard
Copied
// 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};
};
Copy link to clipboard
Copied
Thank you once again. I had no clue how to do that. 🙂
Copy link to clipboard
Copied
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?