Copy link to clipboard
Copied
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.
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 w
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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");}
Copy link to clipboard
Copied
Fantastic! Thanks so much it worked perfectly. One question if i may, it saves upon exit, is there a way to save as a copy ie psd1.psd instead of overwriting the original psd file. Thanks again
Copy link to clipboard
Copied
Here you are...
#target photoshop;
var fileRef = new File("/z/psd.psd");
if(fileRef.exists){
app.open (fileRef);
//get file name without extension
var Name = decodeURI(activeDocument.name).replace(/\.[^\.]+$/, '');
//create new filename
var newFileName = File(activeDocument.path + "/" + Name + "1.psd");
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 new file
SavePSD(newFileName);
//close file
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}else{alert("File not found");}
function SavePSD(saveFile){
psdSaveOptions = new PhotoshopSaveOptions();
psdSaveOptions.embedColorProfile = true;
psdSaveOptions.alphaChannels = true;
activeDocument.saveAs(File(saveFile+".psd"), psdSaveOptions, true, Extension.LOWERCASE);
};
Copy link to clipboard
Copied
Thank you, thats works great
Copy link to clipboard
Copied
I think you asked about putting a background behind a text layer and how to stroke a layer, here is an example of both. A text layer must be selected first before running this example.
#target photoshop;
if(documents.length) main();
function main(){
if(doc.activeLayer.kind != LayerKind.TEXT){
alert("you must have a text layer selected before using this script");
return;
}
//create a background layer to the text
highLightText();
var Colour = new SolidColor();
Colour.rgb.hexValue="ff0000";
//stroke the layer, colour, number of pixels
strokeLayer(Colour,3);
}
function highLightText(){
if(!documents.length) return;
var doc = activeDocument;
if(doc.activeLayer.kind != LayerKind.TEXT) return;
var Colour = new SolidColor();
Colour.rgb.hexValue='ffffff';
var strtRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var txtLayer = doc.activeLayer;
var LBS= doc.activeLayer.bounds;
doc.selection.select([[LBS[0],LBS[1]], [LBS[2],LBS[1]], [LBS[2],LBS[3]], [LBS[0], LBS[3]]], SelectionType.REPLACE, 0, false);
doc.selection.expand(10);
var highLightLayer = doc.artLayers.add();
doc.selection.fill(Colour);
doc.selection.deselect();
doc.activeLayer.move(txtLayer,ElementPlacement.PLACEAFTER);
app.preferences.rulerUnits = strtRulerUnits;
}
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){}
};
Copy link to clipboard
Copied
Hi, thanks very much for sending that thought it is much appreciated.
I tried to get it to work but couldn’t unfortunately but i think its more my issue of how i am using it.
What i have is a text layer, called "footer".
This layers contents is simply "ABC" and the footer is not visible
I would ideally like a command which
<<< i think we maybe able to get that far due to your help and code
and then
4. A white box #ffffff is placed behind the text to match the width of the text and height of the text ideally with say 5-10mm margin.
Is it possible to achive no.4?
Thanks so much for all your help
Copy link to clipboard
Copied
So it would look something like this, with the white box automatically being drawn below the text layer to the dynamic size of the text box
Copy link to clipboard
Copied
@SuperMerlin Maybe you can help me, the below script works to make multiple spaces into single spaces, but it defaults to the top most styling, instead of retaining preexisting styling.
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-remove-extra-spaces-in-text...
Copy link to clipboard
Copied
This example requires the file to be open and the active document in Photoshop.
#target photoshop;
if(documents.length) main();
function main(){
try{
var textColour = new SolidColor();
//set colour
textColour.rgb.hexValue="000000";
//select layer by name and make visible
selectLayerByName("footer",true);
//set font
activeDocument.activeLayer.textItem.font = "Verdana";
//set colour
activeDocument.activeLayer.textItem.color = textColour;
//create white background for text
highLightText();
}catch(e){alert(e +"\n" +e.line);}
};
function selectLayerByName(lyrName,visible){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putName( charIDToTypeID( "Lyr " ), lyrName);
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), visible );
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
};
function highLightText(){
if(!documents.length) return;
var doc = activeDocument;
if(doc.activeLayer.kind != LayerKind.TEXT) return;
var Colour = new SolidColor();
Colour.rgb.hexValue='ffffff';
var strtRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var txtLayer = doc.activeLayer;
var LBS= doc.activeLayer.bounds;
doc.selection.select([[LBS[0],LBS[1]], [LBS[2],LBS[1]], [LBS[2],LBS[3]], [LBS[0], LBS[3]]], SelectionType.REPLACE, 0, false);
doc.selection.expand(10);
var highLightLayer = doc.artLayers.add();
doc.selection.fill(Colour);
doc.selection.deselect();
doc.activeLayer.move(txtLayer,ElementPlacement.PLACEAFTER);
app.preferences.rulerUnits = strtRulerUnits;
}
Copy link to clipboard
Copied
You are a GENIUS! Works perfectly! Thank you.
Copy link to clipboard
Copied
Regarding text layers, is there a specific way to reference PC installed fonts? Verdana works fine, but Arial and some other fonts default to Myriad Pro. Any ideas? Thank you.
Copy link to clipboard
Copied
You have to use the Postscript name of the font.
here is an example script to show fonts.
#target photoshop
main();
function main(){
var Dlog =
"dialog{text:'Script Interface',bounds:[100,100,500,270],"+
"FontName:DropDownList{bounds:[30,40,370,60]},"+
"PostScriptName:EditText{bounds:[30,100,370,120]},"+
"statictext0:StaticText{bounds:[40,10,200,30] , text:'Font Name'},"+
"statictext1:StaticText{bounds:[40,70,141,87] , text:'Postscript Name'},"+
"button0:Button{bounds:[140,130,240,150] , text:'Ok' }}";
win = new Window(Dlog,"Use the Postscript Name in your script.");
try{
for (var i=0,len=app.fonts.length;i<len;i++) {
win.FontName.add ('item', app.fonts.name);
};
win.FontName.selection = 0 ;
win.PostScriptName.text = app.fonts[win.FontName.selection.index].postScriptName;
win.FontName.onChange = function() {
win.PostScriptName.text = app.fonts[win.FontName.selection.index].postScriptName;
};
win.center();
win.show();
}catch(e){alert(e + e.line); return;}
};
Copy link to clipboard
Copied
Excellent! Works perfectly now. Thank you
Copy link to clipboard
Copied
Hello,
None of your code seems to call invalid property nor methods
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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){}
};
Copy link to clipboard
Copied
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){}
};
Copy link to clipboard
Copied
Thanks, the stroke works great, is there a bit of the code i can amend to make it inline vs outline as at present its defaulting to outline?
Copy link to clipboard
Copied
Here you are..
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("insetFrame"));
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){}
};
Copy link to clipboard
Copied
Thank you thats excellent and now works perfectly
Copy link to clipboard
Copied
Is there a way to reduce the size of the font, only if when the "contents" section ie below is replaced:
activeDocument.activeLayer.textItem.contents="LONG TEXT FIELD";
And it doesnt fit within the text layer, so then the text size is reduced to fit?
Can the spacing gaps also be adjusted on the same ie -20 or -50 etc?
Thank you.
Copy link to clipboard
Copied
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.