Copy link to clipboard
Copied
Hi,
I'm trying to replace the content of text variables in my PSD thanks to a javascript script.
The script works but I'd like to push it a little bit further
I'd like to apply this script to all opened files. Ideally I'd like to get in a variable the list of all opened documents and then use a for loop to go through all items and apply my loop.
See my script below:
if(app.documents.length != 0){
var doc = app.activeDocument;
var headline = "new headline"
var body = "new body copy"
var cta = "you're going to be rich sucker"
var url = "http://www.nicesite.com"
for(i = 0; i < doc.artLayers.length; ++i){
var layer = doc.artLayers;
if(layer.name == "headline"){
layer.textItem.contents = headline;
}
if(layer.name == "body"){
layer.textItem.contents = body;
}
if(layer.name == "cta"){
layer.textItem.contents = cta;
}
if(layer.name == "url"){
layer.textItem.contents = url;
}
}
}
Would you mind sharing some code which could allow me to apply this treatment to all the open files I have in my photoshop environment?
Thanks
Copy link to clipboard
Copied
Easiest would be a while-function, I guess,
while (documents.length > 0) {
/*insert the operation here*/
app.activeDocument.close()
}
that concludes with closing the active document.
But a for-function should also work.
for (var m = 0; m < documents.length; m++) {
var theDoc = app.documents
; app.activeDocument = theDoc;
/*insert the operation here*/
}
Copy link to clipboard
Copied
I would go with the while loop. Your for loop is going to skip documents if you close them in the loop.
Copy link to clipboard
Copied
You are right, when closing in the for-function one should subtract and a while-function would be simpler.