Copy link to clipboard
Copied
Hi
I have 100 similar PSD files with same word written inside it. but name of the files is diffrent
All 100 PSD files has one word written in it. for example. word is "TestWord"
Name if the PSD files is like
apple.psd
banana.psd
orange.psd
peas.psd
pears.psd
now I want to replace "TestWord" with name of the files. like
in apple.psd file, "TestWord" will be replaced with word "Apple"
in banana.psd file, "TestWord" will be replaced with word "banana"
please suggest some script to achieve this
regards
Copy link to clipboard
Copied
// 2017, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = activeDocument;
var theString = "TestWord";
var theReplacementString = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var textLayers = collectAndChangeTextLayers(app.activeDocument, [], theString, theReplacementString);
};
////// function collect all layers //////
function collectAndChangeTextLayers (theParent, allLayers, theString, theReplacementString) {
if (!allLayers) {var allLayers = new Array}
else {};
var theNumber = theParent.layers.length - 1;
for (var m = theNumber; m >= 0;m--) {
var theLayer = theParent.layers
// apply the function to layersets;
if (theLayer.typename == "ArtLayer") {
// edit typelayers;
if (theLayer.kind == LayerKind.TEXT) {
while (theLayer.textItem.contents.indexOf(theString) != -1) {theLayer.textItem.contents = theLayer.textItem.contents.replace(theString, theReplacementString)};
allLayers.push(theLayer)
}
}
else {
allLayers = collectAndChangeTextLayers(theLayer, allLayers, theString, theReplacementString)
// this line includes the layer groups;
// allLayers.push(theLayer);
}
};
return allLayers
};
(edited)
Copy link to clipboard
Copied
one more question, assume, there is text file containing one word in each line . for example:
apple
banana
orange
pears
peas
I have a PSD file having multiple text layers, and only one word written in all layers, for example sample,
so , looking for a script which take a word from txt file and replace this sample word and save the file
I can create action for saving the file. but i need a script which can take word from the txt file one by one
please help.
Copy link to clipboard
Copied
hi
can anyone help me with below query
one more question, assume, there is text file containing one word in each line . for example:
apple
banana
orange
pears
peas
I have a PSD file having multiple text layers, and only one word written in all layers, for example sample,
so , looking for a script which take a word from txt file and replace this sample word and save the file
I can create action for saving the file. but i need a script which can take word from the txt file one by one
please help.
Copy link to clipboard
Copied
jitenderbhatia – If you can’t write the script yourself or rely on others generosity, then you may need to find an alternative method.
One possibility is using Variables, otherwise known as “Data Driven Graphics”.
There are many forum topics, webpages, videos etc out there:
Creating data-driven graphics in Photoshop
Good luck!
Copy link to clipboard
Copied
This is an example on how to use text-files via JavaScript.
alert (readPref ("~/Desktop/aaa.txt"));
////// read prefs file //////
function readPref (thePath) {
if (File(thePath).exists == true) {
var file = File(thePath);
file.open("r");
file.encoding= 'BINARY';
var theText = new String;
for (var m = 0; m < file.length; m ++) {
theText = theText.concat(file.readch());
};
file.close();
return String(theText)
}
};
You could use the resulting Array in a for-clause.