问题
I have a photoshop script file which opens up a template psd file:
var fileRef = new File("z:\psd.psd")
var docRef = app.open (fileRef)
Once this is open, i would like code which changes the text of a specific layer called "LAYER1" to "TEST".
I have researched and carried out numerous tests but i am having issues and errors with undefined variables.
回答1:
It it will be necessary to loop over all layers, (including layers within Layer Groups), to find your specific named Text Layer (e.g. LAYER1) before it's text content can be changed. To achieve this I recommend adding a custom function to your script.
The following code example will change the text content of the Text Layer(s) named LAYER1 to Hello World.
var fileRef = new File('z:\psd.psd');
var docRef = app.open(fileRef);
/**
* Change text content of a specific named Text Layer to a new text string.
*
* @param {Object} doc - A reference to the document to change.
* @param {String} layerName - The name of the Text Layer to change.
* @param {String} newTextString - New text content for the Text Layer.
*/
function changeTextLayerContent(doc, layerName, newTextString) {
for (var i = 0, max = doc.layers.length; i < max; i++) {
var layerRef = doc.layers[i];
if (layerRef.typename === "ArtLayer") {
if (layerRef.name === layerName && layerRef.kind === LayerKind.TEXT) {
layerRef.textItem.contents = newTextString;
}
} else {
changeTextLayerContent(layerRef, layerName, newTextString);
}
}
}
changeTextLayerContent(docRef, 'LAYER1', 'Hello World');
Explanation
Invoking the function:
The last line of code above which reads:
changeTextLayerContent(docRef, 'LAYER1', 'Hello World');
is where the
changeTextLayerContent
function gets invoked.We pass three arguments to the function as follows:
docRef
- which is a object reference of the document in which to change its layers.'LAYER1'
- which is the name of the Text Layer to change its contents.'Hello World'
- which is the new text string (i.e. content) to apply to the Text Layer (in this case, to the Text Layer namedLAYER1
).
Let's say we were to invoke the function as follows:
changeTextLayerContent(docRef, 'MainTitle', 'The quick brown fox');
This would set the text content of the Text Layer named
MainTitle
to The quick brown fox.Note: If your document/template included multiple Text Layers named
MainTitle
then they would all have their content changed to The quick brown fox.
The changeTextLayerContent
function:
The function firstly utilizes a for statement to loop over each top level Layer or Group which is listed in Photoshop's Layers Palette.
It then checks whether the layers
typename
isArtLayer
.If its
typename
isArtLayer
it subsequently then checks the layersname
equals the layerName you provided and whether the layerskind
is equal toLayerKind.TEXT
. If these conditional checks are both true, only then will it set the new text content for the Text Layer via the line which reads:layerRef.textItem.contents = newTextString;
Alternatively, if the layers
typename
is not aArtLayer
then it must be aLayerSet
(i.e. a Layer Group). In this scenario the function re-invokes itself via the line reading:changeTextLayerContent(layerRef, layerName, newTextString);
However, this time it passes the
layerRef
as the first argument, which causes the function to loop over all layers in the group/set and check them too.
来源:https://stackoverflow.com/questions/50574218/photoshop-script-change-specific-text-layer-contents