Skip to main content
Known Participant
May 29, 2018
Answered

Photoshop Scripts for Text Layers (Javascript)

  • May 29, 2018
  • 4 replies
  • 20038 views

HI Everyone,

I am trying to write a script for photoshop which carries out the below procedures:

1. opens a template psd file with below code:

var fileRef = new File("z:\psd.psd")

var docRef = app.open (fileRef)

2. There is a text layer named "text1", i would like to a

     a. change the text value to "100" currently its showing as "50"

     b. change the font to verdana from times new roman

     c. change font colour to hex #000000 from hex #ffffff

     d. duplicate the layer ie "text2"

     e. make the original layer "text1" not visible.

Could someone script this for me?

Sorry for the basics i am still trying to learn the code.

Thanks so much.

This topic has been closed for replies.
Correct answer SuperMerlin

Does this help...

#target photoshop;

var fileRef = new File("/z/psd.psd");

if(fileRef.exists){

app.open (fileRef);

var textColour = new SolidColor();

//set colour

textColour.rgb.hexValue="000000";

//select layer named "text1"

activeDocument.activeLayer = activeDocument.artLayers.getByName("text1");

//set font

activeDocument.activeLayer.textItem.font = "Verdana";

//set colour

activeDocument.activeLayer.textItem.color = textColour;

//set text

activeDocument.activeLayer.textItem.contents="100";

//duplicate layer with new name

activeDocument.activeLayer.duplicate(activeDocument.activeLayer, ElementPlacement.PLACEBEFORE).name="text2";

//hide "text1" layer

activeDocument.activeLayer.visible=false;

//save and close file

app.activeDocument.close(SaveOptions.SAVECHANGES);

}else{alert("File not found");}

4 replies

Participant
January 17, 2019

SuperMerlin

Could you help me with my mission,

I have an array (list of different names) and I need JavaScript code to open certificate.psd and type all there names In loop of certificates and save it in PDF format.

could you help me to write it in photoshop please?

Thanks in advance.

web2000Author
Known Participant
May 30, 2018

Is there a way to access stroke values of text layers? For example i have a text layer called "text1" with a stroke of 5px and it’s black. I would like to change it to a different colour for example grey, but also make it non visible? I am assuming if i had another layer also like a square box with a stroke i could also access and amend the stroke with the same code reference? Thank you

SuperMerlin
Inspiring
May 30, 2018

When you get into the realms of effects, things get very messy and not straight forward, also it would depend on the Photoshop version.

I have given an example to change the stroke colour and size above, here it is again.

var Colour = new SolidColor(); 

Colour.rgb.hexValue="00ff00"; 

//stroke the layer, colour, number of pixels 

strokeLayer(Colour,7);

function strokeLayer(colour,size){ 

try{ 

var d=new ActionDescriptor(); 

var r=new ActionReference(); 

r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("layerEffects")); 

r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); 

d.putReference(stringIDToTypeID("null"), r); 

var d1=new ActionDescriptor(); 

d1.putUnitDouble(stringIDToTypeID("scale"), stringIDToTypeID("percentUnit"), 416.666666666667); 

var d2=new ActionDescriptor(); 

d2.putBoolean(stringIDToTypeID("enabled"), true); 

d2.putBoolean(stringIDToTypeID("present"), true); 

d2.putBoolean(stringIDToTypeID("showInDialog"), true); 

d2.putEnumerated(stringIDToTypeID("style"), stringIDToTypeID("frameStyle"), stringIDToTypeID("outsetFrame")); 

d2.putEnumerated(stringIDToTypeID("paintType"), stringIDToTypeID("frameFill"), stringIDToTypeID("solidColor")); 

d2.putEnumerated(stringIDToTypeID("mode"), stringIDToTypeID("blendMode"), stringIDToTypeID("normal")); 

d2.putUnitDouble(stringIDToTypeID("opacity"), stringIDToTypeID("percentUnit"), 100); 

d2.putUnitDouble(stringIDToTypeID("size"), stringIDToTypeID("pixelsUnit"), size); 

var d3=new ActionDescriptor(); 

d3.putDouble(stringIDToTypeID("red"), colour.rgb.red); 

d3.putDouble(stringIDToTypeID("green"), colour.rgb.green); 

d3.putDouble(stringIDToTypeID("blue"), colour.rgb.blue); 

d2.putObject(stringIDToTypeID("color"), stringIDToTypeID("RGBColor"), d3); 

d2.putBoolean(stringIDToTypeID("overprint"), false); 

d1.putObject(stringIDToTypeID("frameFX"), stringIDToTypeID("frameFX"), d2); 

d.putObject(stringIDToTypeID("to"), stringIDToTypeID("layerEffects"), d1); 

executeAction(stringIDToTypeID("set"), d, DialogModes.NO); 

}catch(e){} 

};

SuperMerlin
Inspiring
May 30, 2018

Here is a function to hide the active layers stroke...

Photoshop CC2018

hideStroke();

function hideStroke(){

try{

var d=new ActionDescriptor();

var list=new ActionList();

var r=new ActionReference();

r.putIndex(stringIDToTypeID("frameFX"), 1);

r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

list.putReference(r);

d.putList(stringIDToTypeID("null"), list);

executeAction(stringIDToTypeID("hide"), d, DialogModes.NO);

}catch(e){}

};

Loic.Aigon
Legend
May 29, 2018

Hello,

https://wwwimages2.adobe.com/content/dam/acom/en/devnet/photoshop/scripting/photoshop_cs3_javascript_ref.pdf

None of your code seems to call invalid property nor methods   

web2000Author
Known Participant
May 29, 2018

Hi

Yes i have indeed but the CS3 version. I have been testing with several code functions but i have been set back with several errors ie 1302 NO SUCH ELEMENT and some other issues i could not get working.

I am on version CS3 extended.

I managed to get this working to change the text layer, but it also changes all other text layers.

if(app.documents.length != 0){

    var doc = app.activeDocument;

    for(i = 0; i < doc.artLayers.length; ++i){

        var myLayer1 = doc.artLayers;

        if(myLayer1.kind == LayerKind.TEXT){

            //layer.textItem.font = "ArialMT";

            myLayer1.textItem.contents = "test";

        }

    }

}

I tried have several other examples but have issued with the 1302 error or undefined errors, when it seems to work for others.

For example even trying to make a text layer called name not visible gives a "no such element error" when the layer is present

var HideLayer = app.activeDocument.layers.itemByName("test");

HideLayer.visible = false;

I am new to photoshop scripting.

Regards

SuperMerlin
SuperMerlinCorrect answer
Inspiring
May 29, 2018

Does this help...

#target photoshop;

var fileRef = new File("/z/psd.psd");

if(fileRef.exists){

app.open (fileRef);

var textColour = new SolidColor();

//set colour

textColour.rgb.hexValue="000000";

//select layer named "text1"

activeDocument.activeLayer = activeDocument.artLayers.getByName("text1");

//set font

activeDocument.activeLayer.textItem.font = "Verdana";

//set colour

activeDocument.activeLayer.textItem.color = textColour;

//set text

activeDocument.activeLayer.textItem.contents="100";

//duplicate layer with new name

activeDocument.activeLayer.duplicate(activeDocument.activeLayer, ElementPlacement.PLACEBEFORE).name="text2";

//hide "text1" layer

activeDocument.activeLayer.visible=false;

//save and close file

app.activeDocument.close(SaveOptions.SAVECHANGES);

}else{alert("File not found");}