Copy link to clipboard
Copied
I'm really, really struggling with this and I can't find any good examples for adding a stroke. Let's say I have a smart object and I want to add a stroke Layer Style to it that is only 1 pixel in size, is positioned on the inside, has 100% opacity and is solid black in color. I cannot figure out how to create a script that will add this specific layer style to the currently selected layer. (Remember, it should work for smart objects as well as regular layers.) How should I go about doing this? Can somebody please give me an example of applying a stroke layer style to a smart object layer using ExtendScript and Javascript for Photoshop?
I know this is now an old post, but figured I'd reply with an answer and a somewhat flexible script.
The only parameters I exposed in this function were size, color, opacity and position.
With the ScriptingListener plug-in you will get the ActionDescriptor code generated by Photoshop when you execute almost any action inside of PS. It's pretty easy to install, but it's not easy to read or understand, that will require a lot of testing on your end.
...Copy link to clipboard
Copied
I would use JAM framework: JsDoc Reference - jamStyles
JSON Action Manager | Tonton Pixel
In a certain sense it is a framework like jQuery is framework for the web.
Otherwise you can use this:
var idsetd = charIDToTypeID( "setd" );
var desc55 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref19 = new ActionReference();
var idPrpr = charIDToTypeID( "Prpr" );
var idLefx = charIDToTypeID( "Lefx" );
ref19.putProperty( idPrpr, idLefx );
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref19.putEnumerated( idLyr, idOrdn, idTrgt );
desc55.putReference( idnull, ref19 );
var idT = charIDToTypeID( "T " );
var desc56 = new ActionDescriptor();
var idScl = charIDToTypeID( "Scl " );
var idPrc = charIDToTypeID( "#Prc" );
desc56.putUnitDouble( idScl, idPrc, 100.000000 );
var idFrFX = charIDToTypeID( "FrFX" );
var desc57 = new ActionDescriptor();
var idenab = charIDToTypeID( "enab" );
desc57.putBoolean( idenab, true );
var idpresent = stringIDToTypeID( "present" );
desc57.putBoolean( idpresent, true );
var idshowInDialog = stringIDToTypeID( "showInDialog" );
desc57.putBoolean( idshowInDialog, true );
var idStyl = charIDToTypeID( "Styl" );
var idFStl = charIDToTypeID( "FStl" );
var idInsF = charIDToTypeID( "InsF" );
desc57.putEnumerated( idStyl, idFStl, idInsF );
var idPntT = charIDToTypeID( "PntT" );
var idFrFl = charIDToTypeID( "FrFl" );
var idSClr = charIDToTypeID( "SClr" );
desc57.putEnumerated( idPntT, idFrFl, idSClr );
var idMd = charIDToTypeID( "Md " );
var idBlnM = charIDToTypeID( "BlnM" );
var idNrml = charIDToTypeID( "Nrml" );
desc57.putEnumerated( idMd, idBlnM, idNrml );
var idOpct = charIDToTypeID( "Opct" );
var idPrc = charIDToTypeID( "#Prc" );
desc57.putUnitDouble( idOpct, idPrc, 100.000000 );
var idSz = charIDToTypeID( "Sz " );
var idPxl = charIDToTypeID( "#Pxl" );
desc57.putUnitDouble( idSz, idPxl, 10.000000 );
var idClr = charIDToTypeID( "Clr " );
var desc58 = new ActionDescriptor();
var idRd = charIDToTypeID( "Rd " );
desc58.putDouble( idRd, 0.000000 );
var idGrn = charIDToTypeID( "Grn " );
desc58.putDouble( idGrn, 180.015568 );
var idBl = charIDToTypeID( "Bl " );
desc58.putDouble( idBl, 255.000000 );
var idRGBC = charIDToTypeID( "RGBC" );
desc57.putObject( idClr, idRGBC, desc58 );
var idoverprint = stringIDToTypeID( "overprint" );
desc57.putBoolean( idoverprint, false );
var idFrFX = charIDToTypeID( "FrFX" );
desc56.putObject( idFrFX, idFrFX, desc57 );
var idLefx = charIDToTypeID( "Lefx" );
desc55.putObject( idT, idLefx, desc56 );
executeAction( idsetd, desc55, DialogModes.NO );
But this in not much readable code and it destroy other styles.
Copy link to clipboard
Copied
I cannot get the code that you provided to work for some reason. May I ask how you were able to generate it? I haven't been able to try the JAM framework yet.
Copy link to clipboard
Copied
I know this is now an old post, but figured I'd reply with an answer and a somewhat flexible script.
The only parameters I exposed in this function were size, color, opacity and position.
With the ScriptingListener plug-in you will get the ActionDescriptor code generated by Photoshop when you execute almost any action inside of PS. It's pretty easy to install, but it's not easy to read or understand, that will require a lot of testing on your end.
Installing and using the ScriptingListener plug-in - Adobe Photoshop Scripting | Adobe Developer Connection
// Add Stroke to layer
// Javier Aroche
// Set color as HEX
var strokeColor = new RGBColor();
strokeColor.hexValue = '000000';
addStroke(1, strokeColor, 100, 'inside');
/*
* Add Stroke Effect
* @param {Number} size : 1 - 250
* @param {Object} color : RGBColor object
* @param {Number} opacity : 0 - 100
* @param {Number} position : center / outside / inside
*/
function addStroke(size, color, opacity, position) {
var strokePosCharID;
switch(position) {
case 'center':
strokePosCharID = 'CtrF';
break;
case 'outside':
strokePosCharID = 'OutF';
break;
case 'inside':
strokePosCharID = 'InsF';
break;
default: break;
}
var desc = new ActionDescriptor();
var ref190 = new ActionReference();
ref190.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( "Lefx" ) );
ref190.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc.putReference( charIDToTypeID( "null" ), ref190 );
var fxDesc = new ActionDescriptor();
var fxPropDesc = new ActionDescriptor();
fxPropDesc.putBoolean( charIDToTypeID( "enab" ), true );
fxPropDesc.putBoolean( stringIDToTypeID( "present" ), true );
fxPropDesc.putBoolean( stringIDToTypeID( "showInDialog" ), true );
fxPropDesc.putEnumerated( charIDToTypeID( "Styl" ), charIDToTypeID( "FStl" ), charIDToTypeID( strokePosCharID ) );
fxPropDesc.putEnumerated( charIDToTypeID( "PntT" ), charIDToTypeID( "FrFl" ), charIDToTypeID( "SClr" ) );
fxPropDesc.putEnumerated( charIDToTypeID( "Md " ), charIDToTypeID( "BlnM" ), charIDToTypeID( "Nrml" ) );
fxPropDesc.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), opacity );
fxPropDesc.putUnitDouble( charIDToTypeID( "Sz " ), charIDToTypeID( "#Pxl") , size );
var colorDesc = new ActionDescriptor();
colorDesc.putDouble( charIDToTypeID( "Rd " ), color.red);
colorDesc.putDouble( charIDToTypeID( "Grn " ), color.green );
colorDesc.putDouble( charIDToTypeID( "Bl " ), color.blue );
fxPropDesc.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), colorDesc );
fxPropDesc.putBoolean( stringIDToTypeID( "overprint" ), false );
fxDesc.putObject( charIDToTypeID( "FrFX" ), charIDToTypeID( "FrFX" ), fxPropDesc );
desc.putObject( charIDToTypeID( "T " ), charIDToTypeID( "Lefx" ), fxDesc );
executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
}
Copy link to clipboard
Copied
Thank you so much, that works wonderfully. My script is able to use the code you provided with no issues whatsoever. I appreciate it!
Copy link to clipboard
Copied
In this script I want to add stoke in multiple layer when select the layer ?
function addStroke(size, color, opacity, position) {
var strokePosCharID;
switch(position) {
case 'center':
strokePosCharID = 'CtrF';
break;
case 'outside':
strokePosCharID = 'OutF';
break;
case 'inside':
strokePosCharID = 'InsF';
break;
default: break;
}
var desc = new ActionDescriptor();
var ref190 = new ActionReference();
ref190.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( "Lefx" ) );
ref190.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc.putReference( charIDToTypeID( "null" ), ref190 );
var fxDesc = new ActionDescriptor();
var fxPropDesc = new ActionDescriptor();
fxPropDesc.putBoolean( charIDToTypeID( "enab" ), true );
fxPropDesc.putBoolean( stringIDToTypeID( "present" ), true );
fxPropDesc.putBoolean( stringIDToTypeID( "showInDialog" ), true );
fxPropDesc.putEnumerated( charIDToTypeID( "Styl" ), charIDToTypeID( "FStl" ), charIDToTypeID( strokePosCharID ) );
fxPropDesc.putEnumerated( charIDToTypeID( "PntT" ), charIDToTypeID( "FrFl" ), charIDToTypeID( "SClr" ) );
fxPropDesc.putEnumerated( charIDToTypeID( "Md " ), charIDToTypeID( "BlnM" ), charIDToTypeID( "Nrml" ) );
fxPropDesc.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), opacity );
fxPropDesc.putUnitDouble( charIDToTypeID( "Sz " ), charIDToTypeID( "#Pxl") , size );
var colorDesc = new ActionDescriptor();
colorDesc.putDouble( charIDToTypeID( "Rd " ), color.red);
colorDesc.putDouble( charIDToTypeID( "Grn " ), color.green );
colorDesc.putDouble( charIDToTypeID( "Bl " ), color.blue );
fxPropDesc.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), colorDesc );
fxPropDesc.putBoolean( stringIDToTypeID( "overprint" ), false );
fxDesc.putObject( charIDToTypeID( "FrFX" ), charIDToTypeID( "FrFX" ), fxPropDesc );
desc.putObject( charIDToTypeID( "T " ), charIDToTypeID( "Lefx" ), fxDesc );
executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
}
Copy link to clipboard
Copied
Don't create new topics for existing threads. Just continue current one if there's no difference between original code and yours, copied from. Use always </> icon (with JAVASCRIPT label) when pasting the code otherwise you risk some double / triple spaces will be shrinked to just one. You were rebuked not once on this forum, so learn to comply with rules, or at least given tips. Don't repeat constantly the same questions when others ask you what you exaclty want, but respect them bringing constructive answers.
The problem with your code is Action Manager characters don't have sufficient spaces.
"Md ", "Sz ", "Rd ", "Bl " and "T " are too short. There is also lack of starting code:
var strokeColor = new RGBColor();
strokeColor.hexValue = '000000';
addStroke(1, strokeColor, 100, 'inside');
Simply copy full code originally posted in this thread and change addStroke function values to desired.
Once again, if you're going to ignore ACP's and moderators who take care of this (forum) section and its users by conducting everyone to smoothly gain their goal you may meet unnecessary problems. THX 🙂
Copy link to clipboard
Copied
I want to Selected Layer, Like Layer 1,Layer 2
Copy link to clipboard
Copied
Multiple Layer Selected Getting a Error
Copy link to clipboard
Copied
After using above script on one layer find the script on this forum which:
'Copy Layer Style', select all other document layers and 'Paste Layer Style.'
Copy link to clipboard
Copied
Can your provide sricpt
Copy link to clipboard
Copied
'Search this community' (on top of page) can provide it for you, just use proper keywords 😉
Copy link to clipboard
Copied
Did not found it
Copy link to clipboard
Copied
Then use ScriptingListener plug-in, by which you can record to a file your manual doings.
Copy link to clipboard
Copied
In this script Add Stoke Selected Layer Like Layer 1,Layer2, Layer 3,Layer 4 etc.
This script stoke apply only one layer, not these selected Layer
// Add Stroke to layer
var strokeColor = new RGBColor();
strokeColor.hexValue = '000000';
addStroke(10, strokeColor, 100, 'inside');
/*
* Add Stroke Effect
* @Param {Number} size : 1 - 250
* @Param {Object} color : RGBColor object
* @Param {Number} opacity : 0 - 100
* @Param {Number} position : center / outside / inside
*/
function addStroke(size, color, opacity, position) {
var strokePosCharID;
switch(position) {
case 'center':
strokePosCharID = 'CtrF';
break;
case 'outside':
strokePosCharID = 'OutF';
break;
case 'inside':
strokePosCharID = 'InsF';
break;
default: break;
}
var desc = new ActionDescriptor();
var ref190 = new ActionReference();
ref190.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( "Lefx" ) );
ref190.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc.putReference( charIDToTypeID( "null" ), ref190 );
var fxDesc = new ActionDescriptor();
var fxPropDesc = new ActionDescriptor();
fxPropDesc.putBoolean( charIDToTypeID( "enab" ), true );
fxPropDesc.putBoolean( stringIDToTypeID( "present" ), true );
fxPropDesc.putBoolean( stringIDToTypeID( "showInDialog" ), true );
fxPropDesc.putEnumerated( charIDToTypeID( "Styl" ), charIDToTypeID( "FStl" ), charIDToTypeID( strokePosCharID ) );
fxPropDesc.putEnumerated( charIDToTypeID( "PntT" ), charIDToTypeID( "FrFl" ), charIDToTypeID( "SClr" ) );
fxPropDesc.putEnumerated( charIDToTypeID( "Md " ), charIDToTypeID( "BlnM" ), charIDToTypeID( "Nrml" ) );
fxPropDesc.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), opacity );
fxPropDesc.putUnitDouble( charIDToTypeID( "Sz " ), charIDToTypeID( "#Pxl") , size );
var colorDesc = new ActionDescriptor();
colorDesc.putDouble( charIDToTypeID( "Rd " ), color.red);
colorDesc.putDouble( charIDToTypeID( "Grn " ), color.green );
colorDesc.putDouble( charIDToTypeID( "Bl " ), color.blue );
fxPropDesc.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), colorDesc );
fxPropDesc.putBoolean( stringIDToTypeID( "overprint" ), false );
fxDesc.putObject( charIDToTypeID( "FrFX" ), charIDToTypeID( "FrFX" ), fxPropDesc );
desc.putObject( charIDToTypeID( "T " ), charIDToTypeID( "Lefx" ), fxDesc );
executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
}
Copy link to clipboard
Copied
How to use ScriptingListner plugins !
Copy link to clipboard
Copied
There's description how to use it on a linked page. Don't ask of something that's available.
Copy link to clipboard
Copied
What is the issue that you're having?
Copy link to clipboard
Copied
Not a issue I want add stoke in Selected Layer in Script,
How to add Stoke Selected Layer
Copy link to clipboard
Copied
Please provide a meaningful description.
What kind of Stroke are you talking about? A Shape Layer Stroke or the Layer Style Stroke?
Copy link to clipboard
Copied
Layer Style Stoke
Copy link to clipboard
Copied
I know it has no bearing on the actual issue but why do you keep writing »Stoke« instead of »Stroke«?
Copy link to clipboard
Copied
This script i want to selected layer stoke
Copy link to clipboard
Copied
You can not select pixels that do not exist in layers. Layer Style Strokes do not exist in any layer they are rendered on the document composite image. Here do you see any red pixels in the layers thumbnails in the layers palette. I can select the Gray Pixels even the ones hidden by the stroke. I can select the White pixels even the one hidden under the late style Stroke. I actually clicked in the Red stroke to select the white pixels a and the gray pixels shown here.
You would need to rasterize the layer style first. You would no longer have a smart object layer with a layer style you would have a raster layers without a layer style.
Copy link to clipboard
Copied
In this script working fine by one laye but i want to add multiple layer on stoke,
// Add Stroke to layer
var strokeColor = new RGBColor();
strokeColor.hexValue = '000000';
addStroke(10, strokeColor, 100, 'inside');
/*
* Add Stroke Effect
* @Param {Number} size : 1 - 250
* @Param {Object} color : RGBColor object
* @Param {Number} opacity : 0 - 100
* @Param {Number} position : center / outside / inside
*/
function addStroke(size, color, opacity, position) {
var strokePosCharID;
switch(position) {
case 'center':
strokePosCharID = 'CtrF';
break;
case 'outside':
strokePosCharID = 'OutF';
break;
case 'inside':
strokePosCharID = 'InsF';
break;
default: break;
}
var desc = new ActionDescriptor();
var ref190 = new ActionReference();
ref190.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( "Lefx" ) );
ref190.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc.putReference( charIDToTypeID( "null" ), ref190 );
var fxDesc = new ActionDescriptor();
var fxPropDesc = new ActionDescriptor();
fxPropDesc.putBoolean( charIDToTypeID( "enab" ), true );
fxPropDesc.putBoolean( stringIDToTypeID( "present" ), true );
fxPropDesc.putBoolean( stringIDToTypeID( "showInDialog" ), true );
fxPropDesc.putEnumerated( charIDToTypeID( "Styl" ), charIDToTypeID( "FStl" ), charIDToTypeID( strokePosCharID ) );
fxPropDesc.putEnumerated( charIDToTypeID( "PntT" ), charIDToTypeID( "FrFl" ), charIDToTypeID( "SClr" ) );
fxPropDesc.putEnumerated( charIDToTypeID( "Md " ), charIDToTypeID( "BlnM" ), charIDToTypeID( "Nrml" ) );
fxPropDesc.putUnitDouble( charIDToTypeID( "Opct" ), charIDToTypeID( "#Prc" ), opacity );
fxPropDesc.putUnitDouble( charIDToTypeID( "Sz " ), charIDToTypeID( "#Pxl") , size );
var colorDesc = new ActionDescriptor();
colorDesc.putDouble( charIDToTypeID( "Rd " ), color.red);
colorDesc.putDouble( charIDToTypeID( "Grn " ), color.green );
colorDesc.putDouble( charIDToTypeID( "Bl " ), color.blue );
fxPropDesc.putObject( charIDToTypeID( "Clr " ), charIDToTypeID( "RGBC" ), colorDesc );
fxPropDesc.putBoolean( stringIDToTypeID( "overprint" ), false );
fxDesc.putObject( charIDToTypeID( "FrFX" ), charIDToTypeID( "FrFX" ), fxPropDesc );
desc.putObject( charIDToTypeID( "T " ), charIDToTypeID( "Lefx" ), fxDesc );
executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
}