Copy link to clipboard
Copied
A stamp layer is easy via Shift+Ctrl+Alt+E but is pages long in scrptlistener, is there an easy way to scipt this?
If you want duplication was selected, use this code after bG.duplicate() (before semicolon):
, aD.activeHistoryState = (sH = aD.historyStates)[sH.length - 1]
Copy link to clipboard
Copied
You could try this...
function stampLayer(){
var adStampLayer = new ActionDescriptor();
adStampLayer.putBoolean( charIDToTypeID( "Dplc" ), true );
executeAction( charIDToTypeID( "MrgV" ), adStampLayer, DialogModes.NO );
};
Set the Dplc to false to emulate "Flatten Image", though you can do that in one line by removing adStampLayer and replace it by undefined in executeAction
Copy link to clipboard
Copied
many thanks unfortunately i get a message that mrge visible not available in this version of photoshop where there is only 1 layer
Copy link to clipboard
Copied
With one layer it won't be possible indeed. You could try this alternative...
function stampLayer(){
try {
var adStampLayer = new ActionDescriptor();
adStampLayer.putBoolean( charIDToTypeID( "Dplc" ), true );
executeAction( charIDToTypeID( "MrgV" ), adStampLayer, DialogModes.NO );
} catch (e){
$.writeln(e);
}
};
Copy link to clipboard
Copied
I have seeen plenty of scripts over the last few days that check the amount of layers before preceeding as what I need to do is create a copy of the background if there is not multiple layers
So stamp if multiple, copy if one and then name that layer as Blur
Copy link to clipboard
Copied
if (!((lrs = (aD = activeDocument).layers).length - 1) && (bG = lrs[0]).isBackgroundLayer) bG.duplicate()
Copy link to clipboard
Copied
Combining the answers and adding a small amount I get this function, it creates the new layer as required, for the stamp layer it does name it as BLUR but for the background only copy it renames the background layer as BLUR and the new layer is Background copy, so I need to get the new layer in focus before the rename action
function stampLayer(){
if (activeDocument.layers.length > 1){
var adStampLayer = new ActionDescriptor();
adStampLayer.putBoolean( charIDToTypeID( "Dplc" ), true );
executeAction( charIDToTypeID( "MrgV" ), adStampLayer, DialogModes.NO ); }
if (!((lrs = (aD = activeDocument).layers).length - 1) && (bG = lrs[0]).isBackgroundLayer) bG.duplicate();
var docRef = activeDocument
docRef.activeLayer.name = "BLUR";
docRef.activeLayer.applyGaussianBlur(20);
};
Copy link to clipboard
Copied
If you want duplication was selected, use this code after bG.duplicate() (before semicolon):
, aD.activeHistoryState = (sH = aD.historyStates)[sH.length - 1]
Copy link to clipboard
Copied
Many thanks for the help, final code is
function stampLayer(){
if (activeDocument.layers.length > 1){
var adStampLayer = new ActionDescriptor();
adStampLayer.putBoolean( charIDToTypeID( "Dplc" ), true );
executeAction( charIDToTypeID( "MrgV" ), adStampLayer, DialogModes.NO ); }
if (!((lrs = (aD = activeDocument).layers).length - 1) && (bG = lrs[0]).isBackgroundLayer) bG.duplicate(), aD.activeHistoryState = (sH = aD.historyStates)[sH.length - 1];
var docRef = activeDocument
docRef.activeLayer.name = "BLUR";
docRef.activeLayer.applyGaussianBlur(20);
};
Copy link to clipboard
Copied
Copy link to clipboard
Copied
you have lost me with that answer
Copy link to clipboard
Copied
I'm aware of that but he is beginner and don't need supsend history yet. Till then he learns how to achive what he wants by more convinient methods where suspendHistory can be used.
Copy link to clipboard
Copied
If you want it a bit shorter, try this one:
function stampLayer(layerName){
if (activeDocument.layers.length > 1){
var adStampLayer = new ActionDescriptor();
adStampLayer.putBoolean( charIDToTypeID( "Dplc" ), true );
executeAction( charIDToTypeID( "MrgV" ), adStampLayer, DialogModes.NO );
} else {
executeAction( charIDToTypeID( "CpTL" ), undefined, DialogModes.NO );
}
app.activeDocument.activeLayer.name = layerName;
}
stampLayer("yourName (BLUR)");
app.activeDocument.activeLayer.applyGaussianBlur(20);
Copy link to clipboard
Copied
so I would need to add
var layerName = 'BLUR';
stampLayer (layerName);
to use that if I am reading it correctly?
Copy link to clipboard
Copied
app.activeDocument.artLayers.add();
app.activeDocument.activeLayer.name = "BLUR";
var d = new ActionDescriptor();
d.putBoolean(stringIDToTypeID("duplicate"), true);
executeAction(stringIDToTypeID("mergeVisible"), d, DialogModes.NO);
app.activeDocument.activeLayer.applyGaussianBlur(20);
Copy link to clipboard
Copied
That's not my code but his. I didn't write it from beginning but only updated of that he wanted.
And I didn't say that code is for beginners, but only he is beginner so doesn't use everything yet.
Copy link to clipboard
Copied
I totally appreciate the elegant solution r-bin as much as I appreciate all the help that Kukurykus has given me over the last week. I am not new to coding, been doing bits and pieces over 40 years out of necessity mostly but very new to Photoshop Javascript - I was taught that first priority is to get it working and then if time allows make it elegant.
Bound to be issues or new things to try so many thanks to all the community and maybe someone else will benefit from my questions
Copy link to clipboard
Copied
A bit late to the party... I'm a beginner, so take from this what you will... Here is a basic conditional check for a background or layered file.
if (app.activeDocument.activeLayer.isBackgroundLayer)
{
alert("Flattened");
}
else
{
alert("Layered");
}
That being said, I'm not sure why you wish to put in this check?
The code being discussed will add a new layer, then merge visible layers with duplicate.
In my limited testing, the code works the same whether or not the image is flattened or layered. Am I missing something?
P.S. I would have offered this code for the new layer/merge visible with dupe:
app.activeDocument.artLayers.add();
var mrgVis = charIDToTypeID("MrgV");
var actDesc = new ActionDescriptor();
var withDupe = charIDToTypeID("Dplc");
actDesc.putBoolean(withDupe, true);
executeAction(mrgVis, actDesc, DialogModes.NO);
Copy link to clipboard
Copied
yes, the original answer I was given threw an error on a single layer document, hence the original need for a check
Copy link to clipboard
Copied
if (activeDocument.layers.length > 1) {/**do your code/}