Skip to main content
mikaelc13023541
Participating Frequently
December 30, 2019
Question

action script with condition if the layer is a smart object

  • December 30, 2019
  • 3 replies
  • 2504 views

Hi, I simply would like to batch add a filter on a smart object. Point is that the smart object is already created or not on the background layer. So I need to know if the background layer is already a smart object or not. In the conditional action preset, there is no such "layer is smart object" item. Has anyone a smart idea?

This topic has been closed for replies.

3 replies

Stephen Marsh
Adobe Expert
December 30, 2019

Perhaps I'm overthinking this...

 

A brute force approach would simply cycle through every layer and convert it to a SO if the layer kind supported this feature. If you record the Topaz filter into an action, then the script can run the filter on each SO layer by calling the action from within the script:

 

 

 

var doc = app.activeDocument;
var layerVisble

doc.activeLayer = doc.layers[0];

for (var i = 0; i < doc.layers.length; i++) {
	doc.activeLayer = doc.layers[i];
	layerVisible = doc.activeLayer.visible;
	doc.activeLayer.visible = true;
	main();
	doc.activeLayer.visible = layerVisible;
}

function main() {
	if (app.activeDocument.activeLayer.kind == "LayerKind.NORMAL") {
		app.runMenuItem(stringIDToTypeID('newPlacedLayer'));
		// Change the action set and action as required...
		app.doAction("My Action Name", "My Action Set Name.atn");
	}
}

 

 

 

 

Change the name of the referenced action and action set as required.

 

The downside of this simplistic approach is that any flattened Background image layers would be converted to a regular layer. A more complex conditional could check for such files and work around this issue if it was a problem.

mikaelc13023541
Participating Frequently
December 31, 2019

ok thanks very much Stephen. Finally I managed to select the right layer just using the built-in action of hotoshop. Then I used your very first command lines and tips, integrated in my action script and it works! Thanks again.

Stephen Marsh
Adobe Expert
December 30, 2019

As I wrote earlier:

 

What may be hard to automate is targeting/selecting the layer to filter. If you have varying layer structure, is there anything that is constant, such as layer name or relative position of the layer in the stack (such as the bottom layer).

 

It is all about identifying and targeting the correct layer, although it sometimes seems that scripts can work magic, they can't.

 

Once the normal raster target layer is selected, it is easy enough to convert it to a SO layer:

 

rasterToSO();

function rasterToSO() {
	if (app.activeDocument.activeLayer.kind == "LayerKind.NORMAL") {
		app.runMenuItem(stringIDToTypeID('newPlacedLayer'));
	}
}

 

I don't think we need to over-complicate the script for conditionally testing if the layer is already a SO, as multiple applications of converting to a SO layer have no effect if the layer is already a SO.

Stephen Marsh
Adobe Expert
December 30, 2019

I'm a little confused, a true Background "image" layer by definition can't be a smart object... However, you could have another layer type (raster, shape, adjustment etc) named "Background", however, this would not be a true Background "image" layer.

 

if (app.activeDocument.activeLayer.kind == "LayerKind.SMARTOBJECT") {
    alert("This is a Smart Object layer");
} else {
    alert("This is not a Smart Object layer");
}

 

Simply replace the alert lines with whatever you wish to do...

mikaelc13023541
Participating Frequently
December 30, 2019

Thanks Stephen, background is indeed the wrong terme, I just meant pixel or image layer. Thanks for you answer, but for the moment I need a solution with Photoshop action script (I am not familiar with programing in javascript). There are conditional status on the layers but nothing I found related to the smart objects

mikaelc13023541
Participating Frequently
December 30, 2019

There is no support in Photoshop conditional actions for testing for a Smart Object layer kind. Therefore the only option is scripting.

 

A script can play a hard-coded action set and action:

 

 

app.doAction("Molten Lead","Default Actions.atn");

 

 

Or a script GUI can be created for a conditional action script (this example does not feature Smart Object layers, however it could be modified):

 

http://actionlogic.blogspot.com/2006/03/sivas-photoshop-conditional-action.html

 

 


ok thanks so much. Seems very powerful, though I do not have so much time to learn about javascript programing. Thanks again for your time