Skip to main content
Inspiring
July 14, 2023
Answered

Need clarification in executing script commands

  • July 14, 2023
  • 2 replies
  • 816 views

Hi all.

There are two pictures A.png and B.png. I want to get B_result.png with the help of a script.

 

Maybe there is a more elegant way to do this, I will be glad to hear your advice.

var maskDoc = app.documents[0] // doc with name A.png
    app.activeDocument = maskDoc
    app.doAction("SelectWhiteColorArea", "Set 1") // run command select color range
    app.activeDocument.selection.makeWorkPath(7.0)
    var currentPathItem = app.activeDocument.pathItems[0]
    currentPathItem.select()
    app.doAction("SavePath", "Set 1") // run command  save Path

    app.doAction("pathCopy", "Set 1")// run command copy "Ctrl + C"
    
    var pictureDoc = app.documents[1] // doc with name B.png
    app.activeDocument = pictureDoc
    app.activeDocument.artLayers[app.activeDocument.artLayers.length - 1].isBackgroundLayer = false;
    
    // run command deselect the first layer
    var idselectNoLayers = stringIDToTypeID("selectNoLayers");
    var desc16 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref6 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idTrgt = charIDToTypeID( "Trgt" );
        ref6.putEnumerated( idLyr, idOrdn, idTrgt );
    desc16.putReference( idnull, ref6 );
    executeAction(idselectNoLayers, desc16, DialogModes.NO);

    app.doAction("appPast", "Set 1") // run command past "Ctrl +V"
    app.activeDocument.pathItems[0].makeSelection()
    app.doAction("ImageCrop", "Set 1")// run command Image/ Crop
    app.activeDocument.pathItems[0].makeSelection()
    app.activeDocument.pathItems[0].remove()
    
    // app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName("Layer 0");
    var idslct = charIDToTypeID( "slct" );
        var desc211 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref8 = new ActionReference();
            var idLyr = charIDToTypeID( "Lyr " );
            ref8.putName( idLyr, "Layer 0" );
        desc211.putReference( idnull, ref8 );
        var idMkVs = charIDToTypeID( "MkVs" );
        desc211.putBoolean( idMkVs, false );
        var idLyrI = charIDToTypeID( "LyrI" );
            var list5 = new ActionList();
            list5.putInteger( 2 );
        desc211.putList( idLyrI, list5 );
    executeAction( idslct, desc211, DialogModes.NO );

app.activeDocument.selection.invert()
    
app.activeDocument.selection.clear()
    ////////////////////////////////////////////
// >>> save to B_result.png

 

First question

When the command is executed,

app.doAction("ImageCrop", "Set 1")

the application displays a message.

The command "Set" is not currently available

Why the application refuses to execute this command ???

 

Second question

The manual says that you need to select a specific layer with this line

app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName("Layer 0")

But the script doesn't select "Layer 0" layer

I found out how to write the command of this layer through the listener, but what is the error in my line ???

 

This topic has been closed for replies.

2 replies

Stephen Marsh
Community Expert
Community Expert
July 14, 2023

@AnyON 


The action is trying to do something that isn't possible in the current file, such as set a selection from a named layer or channel etc. 

 

For the layer selection:

 

var selectLayer0 = app.activeDocument.artLayers.getByName("Layer 0");
app.activeDocument.activeLayer = selectLayer0;

// or

app.activeDocument.activeLayer = app.activeDocument.artLayers.getByName("Layer 0");

// or

app.activeDocument.activeLayer = app.activeDocument.layers.getByName("Layer 0");

// or

app.activeDocument.activeLayer = app.activeDocument.layers["Layer 0"];

// or

app.activeDocument.activeLayer = app.activeDocument.artLayers["Layer 0"];

 

 

r-binCorrect answer
Legend
July 14, 2023
Stephen Marsh
Community Expert
Community Expert
July 14, 2023

Ah yes, a good point r-bin!

 

EDIT: Curiously, I just tested in 2021 and the code from AnyON works even if no layer is selected.

AnyONAuthor
Inspiring
July 14, 2023

Forgot about the Action library. How can I submit it to the forum? The file type (.atn) is not supported.