• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Within my script, want new layer to open above selected layer, and NOT at top of layer stack.

Explorer ,
Jan 24, 2016 Jan 24, 2016

Copy link to clipboard

Copied

Hello there,

I am a digital painter that has (with lots of help here, lol!) written this script to help speed a particular function that I consistently use in my painting workflow.  While testing, I've found that the script works perfectly when executed at the top of my layer stack, however should I need to scroll down into my stack and utilize the script, it is thrown off because the 'new layer' added in line 49 opens above the current layer, but then jumps to the top of the stack; I need this 'new layer' to maintain its position AND not jump to the top of the stack.  Here is the script:

1)    function main() {

    2)          

    3)            var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this === item) return i; } return -1; };

    4)        

    5)            var makeNewLayer = function() {

    6)               var newLayer = doc.artLayers.add();

    7)                newLayer.name = 'magic layer1';

    8)                newLayer.applyStyle('magic layer1');

    9)                doc.activeLayer = newLayer;

    10)            }

    11)        

    12)            var doc = app.activeDocument,

    13)                lay = doc.activeLayer,

    14)                names = ['magic layer1', 

    15)                         'magic layer2', 

    16)                         'magic layer3', 

    17)                         'magic layer4'];

    18)        

    19)            if (__indexOf.call(names, lay.name) < 0 ) {

    20)                newLayer = doc.artLayers.add();

    21)                makeNewLayer();

    22)                return;

    23)            }

    24)        

    25)            if (doc.activeLayer.kind == LayerKind.NORMAL && doc.activeLayer.bounds[2] == 0 && doc.activeLayer.bounds[3] == 0) {

    26)                var dialog = confirm("End magic layers?");

    27)                if (dialog) {

    28)                    app.activeDocument.activeLayer.remove();

    29)                }

    30)                return;

    31)            }

    32)            var idrasterizeLayer = stringIDToTypeID( "rasterizeLayer" );

    33)        var desc5 = new ActionDescriptor();

    34)        var idnull = charIDToTypeID( "null" );

    35)            var ref4 = new ActionReference();

    36)            var idLyr = charIDToTypeID( "Lyr " );

    37)            var idOrdn = charIDToTypeID( "Ordn" );

    38)            var idTrgt = charIDToTypeID( "Trgt" );

    39)            ref4.putEnumerated( idLyr, idOrdn, idTrgt );

    40)        desc5.putReference( idnull, ref4 );

    41)        var idWhat = charIDToTypeID( "What" );

    42)        var idrasterizeItem = stringIDToTypeID( "rasterizeItem" );

    43)        var idlayerStyle = stringIDToTypeID( "layerStyle" );

    44)        desc5.putEnumerated( idWhat, idrasterizeItem, idlayerStyle );

    45)    executeAction( idrasterizeLayer, desc5, DialogModes.NO );

    46)    

    47)            doc.activeLayer.merge();

    48)            doc.activeLayer.name = "base paint layer";

    49)            makeNewLayer();

    50)            return;

    51)        

    52)        }

    53)        

    54)        main();

Any and all help and/or suggestions will be greatly appreciated!

Thanks,

-Eric

TOPICS
Actions and scripting

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Explorer ,
Jan 24, 2016 Jan 24, 2016

Copy link to clipboard

Copied

Oh,... and here is the original brief for the script,... how it works:

I'm a freelance illustrator that utilizes layer styles extensively in my workflow.  As the rasterization and implementation of a new layer style is constant and continual, I would like a way to streamline the 3-4 actions that I currently use into a single conditional script.  I'm currently using CS6.  Here is the itemized description of how I would like the script to function:

    1)    Is the selected layer named "magic layer 1"?  If no, go to line #2.  If yes, go to line #8.

    2)    Is the selected layer named "magic layer 2"?  If no, go to line #3.  If yes, go to line #8.

    3)    Is the selected layer named "magic layer 3"?  If no, go to line #4.  If yes, go to line #8.

    4)    Is the selected layer named "magic layer 4"?  If no, go to line #'s 5-7.  If yes, go to line #8.

    5)    Make new layer.

    6)    Name new layer "magic layer1".

    7)    Add layer style (also named) "magic layer1".  Script concludes.

    8)    Is layer empty?  If yes, go to line #9.  If no, go to line #'s 10-12.

    9)    Queue dialog box:  "End magic layers?"  with choice of "yes" or "no".

    •    If "yes" is selected, the layer is deleted.  Script concludes.

    •    If "no" is selected, the layer is kept (no change).  Script concludes.

   10)   Rasterize layer.

   11)   Merge layer down.

   12)   Rename layer "base paint layer".  Go to line #'s 5-7.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 24, 2016 Jan 24, 2016

Copy link to clipboard

Copied

Adding a layer to the document will always create it as the first layer, you have to move it to the right place.

var makeNewLayer = function(target_layer) {

     var newLayer = doc.artLayers.add();

     newLayer.name = 'magic layer1';

     newLayer.applyStyle('magic layer1');

     newLayer.move(target_layer, ElementPlacement.PLACEBEFORE)

     doc.activeLayer = newLayer;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2016 Jan 25, 2016

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 25, 2016 Jan 25, 2016

Copy link to clipboard

Copied

So I changed your function so it takes a parameter telling on top of which layer you want to create the new one. Since I don't know your case exactly, can't say how you should use it. But if you want it created above the active layer, you'd call it like makeNewLayer(app.activeDocument.activeLayer)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 25, 2016 Jan 25, 2016

Copy link to clipboard

Copied

is this what you're looking for:

// =======================================================

var idMk = charIDToTypeID( "Mk  " );

    var desc2805 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref775 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref775.putClass( idLyr );

    desc2805.putReference( idnull, ref775 );

executeAction( idMk, desc2805, DialogModes.NO );

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 25, 2016 Jan 25, 2016

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 25, 2016 Jan 25, 2016

Copy link to clipboard

Copied

That part I'm not sure about. All I can say is that the script I posted will create a new, empty layer above the active layer and *not* send it to the top of the stack. Try running the snippet I posted as is and test it to see if the new layer is created above the active layer. Hopefully it can help you move things along, but someone more knowledgeable might be able to help out with where exactly in your code you need to place it

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 30, 2016 Jan 30, 2016

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 04, 2016 Mar 04, 2016

Copy link to clipboard

Copied

Still looking for help with this,... anyone have any suggestions.  Sorry, my scripting knowledge is very limited,... any suggestions are greatly appreciated!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 25, 2017 Jun 25, 2017

Copy link to clipboard

Copied

LATEST

Bump,... still looking for help on this.  Please.  Pretty please!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines