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

Need help with If/Then script adjustment

Explorer ,
Aug 13, 2017 Aug 13, 2017

Copy link to clipboard

Copied

Hello,

I am a digital painter that has been dabbling in PS scripting (or more accurately "hacking", as JJMack likes to put it) over the last few years to help increase the efficiency in my workflow.  I have a script that I have been utilizing for the last few years that I have been able to modify periodically to meet my increased needs, but alas,... I have reached the limits of my current level of knowledge and require assistance!   The basic concept of the script is that I utilize Layer styles (described in the script as "magic layer1, magic layer2, etc...") that I am continually rasterizing, merging down on to a "base paint" layer beneath, and creating a new "magic layer" on top again.  As I utilize several of these "magic layers", the script is designed to cycle through the various names of these Layer styles (magic layer1, magic layer2, magic layer3, etc...) in order to determine how to begin, continue, or end the utilization of the script.  I have recently begun to utilize different Layer styles that I refer to as "special magic layers"; for these "special magic layers", I would like the script to run a different (scripted) process, rather than the usual "rasterization, merge down, create a new magic layer above" process. 

Here is the script as it is written now:

function main() { 

     

    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; }; 

 

    var makeNewLayer = function() { 

        var currentLayer = doc.activeLayer; 

        var basePaintLayer = doc.artLayers.add(); 

        basePaintLayer.name = "base paint layer"; 

        basePaintLayer.move(currentLayer, ElementPlacement.PLACEBEFORE); 

  doc.activeLayer.merge();

        var newLayer = doc.artLayers.add(); 

        newLayer.name = 'Magic Layer1'; 

        newLayer.move(currentLayer, ElementPlacement.PLACEBEFORE); 

        try { 

            newLayer.applyStyle('Magic Layer1'); 

        } catch(e) { 

            throw new Error("Can't find Magic Layer Style"); 

        } 

        doc.activeLayer = newLayer;

    } 

 

    var doc = app.activeDocument, 

        lay = doc.activeLayer, 

        names = ['Magic Layer1',  

                 'Magic Layer2',  

                 'Magic Layer3',

                 'Magic Layer4', 

                 'Magic Layer5']; 

 

    // If the layer is not named as in names array 

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

        // create a new layer 

        var currentLayer = doc.activeLayer; 

        var basePaintLayer = doc.artLayers.add(); 

        basePaintLayer.name = "base paint layer"; 

        basePaintLayer.move(currentLayer, ElementPlacement.PLACEBEFORE);

            makeNewLayer(); 

            return; 

        }

 

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

        var dialog = confirm("End this Magic Layer?"); 

        if (dialog) { 

            app.activeDocument.activeLayer.remove();

    }

    // Dialogue to rename ended Magic Layer

        if (dialog) {

    var aDoc = activeDocument; 

    var actLay = aDoc.activeLayer; 

        if (actLay.typename == "ArtLayer") { 

            var newName = prompt ("Would you like to rename this layer?", actLay.name, "Rename"); 

        try {actLay.name = newName;} catch (e) {}; 

        } else { 

            alert("active Layer is not a LayerSet");

         }

        } 

        return; 

    } 

        var idrasterizeLayer = stringIDToTypeID( "rasterizeLayer" );

    var desc5 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref4 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref4.putEnumerated( idLyr, idOrdn, idTrgt );

    desc5.putReference( idnull, ref4 );

    var idWhat = charIDToTypeID( "What" );

    var idrasterizeItem = stringIDToTypeID( "rasterizeItem" );

    var idlayerStyle = stringIDToTypeID( "layerStyle" );

    desc5.putEnumerated( idWhat, idrasterizeItem, idlayerStyle );

executeAction( idrasterizeLayer, desc5, DialogModes.NO );

 

        doc.activeLayer.merge();

    makeNewLayer(); 

    return; 

 

 

main();

Here is the new script that I would like to have run INSTEAD if the layer is named "Special Magic Layer1, Special Magic Layer2, or Special Magic Layer3":

// Load transparency from layer

var idsetd = charIDToTypeID( "setd" );

    var desc272 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref127 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref127.putProperty( idChnl, idfsel );

    desc272.putReference( idnull, ref127 );

    var idT = charIDToTypeID( "T   " );

        var ref128 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idTrsp = charIDToTypeID( "Trsp" );

        ref128.putEnumerated( idChnl, idChnl, idTrsp );

    desc272.putReference( idT, ref128 );

executeAction( idsetd, desc272, DialogModes.NO );

// Refine selection edges

var idrefineSelectionEdge = stringIDToTypeID( "refineSelectionEdge" );

    var desc273 = new ActionDescriptor();

    var idrefineEdgeBorderRadius = stringIDToTypeID( "refineEdgeBorderRadius" );

    var idPxl = charIDToTypeID( "#Pxl" );

    desc273.putUnitDouble( idrefineEdgeBorderRadius, idPxl, 0.000000 );

    var idrefineEdgeBorderContrast = stringIDToTypeID( "refineEdgeBorderContrast" );

    var idPrc = charIDToTypeID( "#Prc" );

    desc273.putUnitDouble( idrefineEdgeBorderContrast, idPrc, 0.000000 );

    var idrefineEdgeSmooth = stringIDToTypeID( "refineEdgeSmooth" );

    desc273.putInteger( idrefineEdgeSmooth, 0 );

    var idrefineEdgeFeatherRadius = stringIDToTypeID( "refineEdgeFeatherRadius" );

    var idPxl = charIDToTypeID( "#Pxl" );

    desc273.putUnitDouble( idrefineEdgeFeatherRadius, idPxl, 0.000000 );

    var idrefineEdgeChoke = stringIDToTypeID( "refineEdgeChoke" );

    var idPrc = charIDToTypeID( "#Prc" );

    desc273.putUnitDouble( idrefineEdgeChoke, idPrc, 80.000000 );

    var idrefineEdgeAutoRadius = stringIDToTypeID( "refineEdgeAutoRadius" );

    desc273.putBoolean( idrefineEdgeAutoRadius, false );

    var idrefineEdgeDecontaminate = stringIDToTypeID( "refineEdgeDecontaminate" );

    desc273.putBoolean( idrefineEdgeDecontaminate, false );

    var idrefineEdgeOutput = stringIDToTypeID( "refineEdgeOutput" );

    var idrefineEdgeOutput = stringIDToTypeID( "refineEdgeOutput" );

    var idrefineEdgeOutputSelection = stringIDToTypeID( "refineEdgeOutputSelection" );

    desc273.putEnumerated( idrefineEdgeOutput, idrefineEdgeOutput, idrefineEdgeOutputSelection );

executeAction( idrefineSelectionEdge, desc273, DialogModes.NO );

// Select layer below current layer

var idslct = charIDToTypeID( "slct" );

    var desc301 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref154 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idBckw = charIDToTypeID( "Bckw" );

        ref154.putEnumerated( idLyr, idOrdn, idBckw );

    desc301.putReference( idnull, ref154 );

    var idMkVs = charIDToTypeID( "MkVs" );

    desc301.putBoolean( idMkVs, false );

executeAction( idslct, desc301, DialogModes.NO );

// Create new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

    var desc274 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref129 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref129.putClass( idLyr );

    desc274.putReference( idnull, ref129 );

executeAction( idMk, desc274, DialogModes.NO );

// Stamp visible into new layer

var idMrgV = charIDToTypeID( "MrgV" );

    var desc275 = new ActionDescriptor();

    var idDplc = charIDToTypeID( "Dplc" );

    desc275.putBoolean( idDplc, true );

executeAction( idMrgV, desc275, DialogModes.NO );

// Invert selection

var idInvs = charIDToTypeID( "Invs" );

executeAction( idInvs, undefined, DialogModes.NO );

// Delete selection

var idDlt = charIDToTypeID( "Dlt " );

executeAction( idDlt, undefined, DialogModes.NO );

// Undo selection

var idsetd = charIDToTypeID( "setd" );

    var desc355 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref208 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref208.putProperty( idChnl, idfsel );

    desc355.putReference( idnull, ref208 );

    var idT = charIDToTypeID( "T   " );

    var idOrdn = charIDToTypeID( "Ordn" );

    var idNone = charIDToTypeID( "None" );

    desc355.putEnumerated( idT, idOrdn, idNone );

executeAction( idsetd, desc355, DialogModes.NO );

// Merge down

var idMrgtwo = charIDToTypeID( "Mrg2" );

    var desc331 = new ActionDescriptor();

executeAction( idMrgtwo, desc331, DialogModes.NO );

// Select layer above current layer

var idslct = charIDToTypeID( "slct" );

    var desc337 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref190 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idFrwr = charIDToTypeID( "Frwr" );

        ref190.putEnumerated( idLyr, idOrdn, idFrwr );

    desc337.putReference( idnull, ref190 );

    var idMkVs = charIDToTypeID( "MkVs" );

    desc337.putBoolean( idMkVs, false );

executeAction( idslct, desc337, DialogModes.NO );

// Delete selected layer

var idDlt = charIDToTypeID( "Dlt " );

    var desc349 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref202 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref202.putEnumerated( idLyr, idOrdn, idTrgt );

    desc349.putReference( idnull, ref202 );

executeAction( idDlt, desc349, DialogModes.NO );

// Make new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

    var desc80 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref77 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref77.putClass( idLyr );

    desc80.putReference( idnull, ref77 );

executeAction( idMk, desc80, DialogModes.NO );

// Add Magic Layer1 style

var idASty = charIDToTypeID( "ASty" );

    var desc15 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref14 = new ActionReference();

        var idStyl = charIDToTypeID( "Styl" );

        ref14.putName( idStyl, "Magic Layer1" );

    desc15.putReference( idnull, ref14 );

    var idT = charIDToTypeID( "T   " );

        var ref15 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref15.putEnumerated( idLyr, idOrdn, idTrgt );

    desc15.putReference( idT, ref15 );

executeAction( idASty, desc15, DialogModes.NO );

// Name layer style: Magic Layer1

var idsetd = charIDToTypeID( "setd" );

    var desc16 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref16 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref16.putEnumerated( idLyr, idOrdn, idTrgt );

    desc16.putReference( idnull, ref16 );

    var idT = charIDToTypeID( "T   " );

        var desc17 = new ActionDescriptor();

        var idNm = charIDToTypeID( "Nm  " );

        desc17.putString( idNm, """Magic Layer1""" );

    var idLyr = charIDToTypeID( "Lyr " );

    desc16.putObject( idT, idLyr, desc17 );

executeAction( idsetd, desc16, DialogModes.NO );

Thank you; I am greatful for any and all assistance!  Also (as an aside), how do you post scripts in the forums in the 'script editor' form?  I've been trying to figure that out!?

-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

correct answers 1 Correct answer

Community Expert , Aug 17, 2017 Aug 17, 2017

I made a slight mistake with my above code, which I corrected. The last case - if you want a default case if the other cases don't match, should be with default. See my above post.

You need to simplify your code. The AM code makes things hard to read, so I like to put that stuff in functions and call them in my main code as needed. So something like:

var doc = activeDocument;

var curLayer = doc.activeLayer

myCode01 ()//I want the code in this function to be applied to everything

switch(curLayer.name)

...

Votes

Translate

Translate
Adobe
Explorer ,
Aug 17, 2017 Aug 17, 2017

Copy link to clipboard

Copied

Hello,

Shameless bump!  (Okay,... not quite so shameless,... in fact, quite shameful!!)  Help?!  Please!

Thanks!

-Eric

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
Community Expert ,
Aug 17, 2017 Aug 17, 2017

Copy link to clipboard

Copied

I would use a switch statement to route the code to how you want it. Place routines that are common to several of the switch conditions in a function so that they can easily be referred to in the switch.

var doc = activeDocument;

var layerName = doc.activeLayer.name;

switch(layerName){

    case 'magic layer1':

    //code here

    break;

    case 'magic layer2':

    //code here

    break;

    case 'special magic layers':

    //code here

    break;

    default:

    //will run this code if non of the above cases match

    }

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 ,
Aug 17, 2017 Aug 17, 2017

Copy link to clipboard

Copied

Okay so I've tried this,... not sure if my functions are 'wrapped' or called correctly,... but I'm getting an error at "will run this code if none of the above cases match".  As you may notice (by all of my ugly code), I depend on the script listener a lot, and am not really much of a coder.  Although I am trying to learn, it's hard to manage to find the time to devote to learning javascript while working the day job.  Not sure how to proceed from here:

var doc = activeDocument; 

var layerName = doc.activeLayer.name;

var rasterizeMagiclayer = function() {

    //Rasterize Magiclayer layer

        var idrasterizeLayer = stringIDToTypeID( "rasterizeLayer" );

    var desc5 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref4 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref4.putEnumerated( idLyr, idOrdn, idTrgt );

    desc5.putReference( idnull, ref4 );

    var idWhat = charIDToTypeID( "What" );

    var idrasterizeItem = stringIDToTypeID( "rasterizeItem" );

    var idlayerStyle = stringIDToTypeID( "layerStyle" );

    desc5.putEnumerated( idWhat, idrasterizeItem, idlayerStyle );

executeAction( idrasterizeLayer, desc5, DialogModes.NO );

// Merge layer down

var idMrgtwo = charIDToTypeID( "Mrg2" );

    var desc43 = new ActionDescriptor();

executeAction( idMrgtwo, desc43, DialogModes.NO );

// Make new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

    var desc80 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref77 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref77.putClass( idLyr );

    desc80.putReference( idnull, ref77 );

executeAction( idMk, desc80, DialogModes.NO );

// Name layer: Magiclayer1

var idsetd = charIDToTypeID( "setd" );

    var desc16 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref16 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref16.putEnumerated( idLyr, idOrdn, idTrgt );

    desc16.putReference( idnull, ref16 );

    var idT = charIDToTypeID( "T   " );

        var desc17 = new ActionDescriptor();

        var idNm = charIDToTypeID( "Nm  " );

        desc17.putString( idNm, """Magiclayer1””” );

    var idLyr = charIDToTypeID( "Lyr " );

    desc16.putObject( idT, idLyr, desc17 );

executeAction( idsetd, desc16, DialogModes.NO );

// Add Magiclayer1 style

var idASty = charIDToTypeID( "ASty" );

    var desc15 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref14 = new ActionReference();

        var idStyl = charIDToTypeID( "Styl" );

        ref14.putName( idStyl, "Magiclayer1” );

    desc15.putReference( idnull, ref14 );

    var idT = charIDToTypeID( "T   " );

        var ref15 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref15.putEnumerated( idLyr, idOrdn, idTrgt );

    desc15.putReference( idT, ref15 );

executeAction( idASty, desc15, DialogModes.NO );

}

var rasterizeSpecialMagic1 = function() {

// Load transparency from layer

var idsetd = charIDToTypeID( "setd" );

    var desc272 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref127 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref127.putProperty( idChnl, idfsel );

    desc272.putReference( idnull, ref127 );

    var idT = charIDToTypeID( "T   " );

        var ref128 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idChnl = charIDToTypeID( "Chnl" );

        var idTrsp = charIDToTypeID( "Trsp" );

        ref128.putEnumerated( idChnl, idChnl, idTrsp );

    desc272.putReference( idT, ref128 );

executeAction( idsetd, desc272, DialogModes.NO );

// Refine selection edges

var idrefineSelectionEdge = stringIDToTypeID( "refineSelectionEdge" );

    var desc273 = new ActionDescriptor();

    var idrefineEdgeBorderRadius = stringIDToTypeID( "refineEdgeBorderRadius" );

    var idPxl = charIDToTypeID( "#Pxl" );

    desc273.putUnitDouble( idrefineEdgeBorderRadius, idPxl, 0.000000 );

    var idrefineEdgeBorderContrast = stringIDToTypeID( "refineEdgeBorderContrast" );

    var idPrc = charIDToTypeID( "#Prc" );

    desc273.putUnitDouble( idrefineEdgeBorderContrast, idPrc, 0.000000 );

    var idrefineEdgeSmooth = stringIDToTypeID( "refineEdgeSmooth" );

    desc273.putInteger( idrefineEdgeSmooth, 0 );

    var idrefineEdgeFeatherRadius = stringIDToTypeID( "refineEdgeFeatherRadius" );

    var idPxl = charIDToTypeID( "#Pxl" );

    desc273.putUnitDouble( idrefineEdgeFeatherRadius, idPxl, 0.000000 );

    var idrefineEdgeChoke = stringIDToTypeID( "refineEdgeChoke" );

    var idPrc = charIDToTypeID( "#Prc" );

    desc273.putUnitDouble( idrefineEdgeChoke, idPrc, 80.000000 );

    var idrefineEdgeAutoRadius = stringIDToTypeID( "refineEdgeAutoRadius" );

    desc273.putBoolean( idrefineEdgeAutoRadius, false );

    var idrefineEdgeDecontaminate = stringIDToTypeID( "refineEdgeDecontaminate" );

    desc273.putBoolean( idrefineEdgeDecontaminate, false );

    var idrefineEdgeOutput = stringIDToTypeID( "refineEdgeOutput" );

    var idrefineEdgeOutput = stringIDToTypeID( "refineEdgeOutput" );

    var idrefineEdgeOutputSelection = stringIDToTypeID( "refineEdgeOutputSelection" );

    desc273.putEnumerated( idrefineEdgeOutput, idrefineEdgeOutput, idrefineEdgeOutputSelection );

executeAction( idrefineSelectionEdge, desc273, DialogModes.NO );

// Select layer below current layer

var idslct = charIDToTypeID( "slct" );

    var desc301 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref154 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idBckw = charIDToTypeID( "Bckw" );

        ref154.putEnumerated( idLyr, idOrdn, idBckw );

    desc301.putReference( idnull, ref154 );

    var idMkVs = charIDToTypeID( "MkVs" );

    desc301.putBoolean( idMkVs, false );

executeAction( idslct, desc301, DialogModes.NO );

// Create new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

    var desc274 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref129 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref129.putClass( idLyr );

    desc274.putReference( idnull, ref129 );

executeAction( idMk, desc274, DialogModes.NO );

// Stamp visible into new layer

var idMrgV = charIDToTypeID( "MrgV" );

    var desc275 = new ActionDescriptor();

    var idDplc = charIDToTypeID( "Dplc" );

    desc275.putBoolean( idDplc, true );

executeAction( idMrgV, desc275, DialogModes.NO );

// Invert selection

var idInvs = charIDToTypeID( "Invs" );

executeAction( idInvs, undefined, DialogModes.NO );

// Delete selection

var idDlt = charIDToTypeID( "Dlt " );

executeAction( idDlt, undefined, DialogModes.NO );

// Undo selection

var idsetd = charIDToTypeID( "setd" );

    var desc355 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref208 = new ActionReference();

        var idChnl = charIDToTypeID( "Chnl" );

        var idfsel = charIDToTypeID( "fsel" );

        ref208.putProperty( idChnl, idfsel );

    desc355.putReference( idnull, ref208 );

    var idT = charIDToTypeID( "T   " );

    var idOrdn = charIDToTypeID( "Ordn" );

    var idNone = charIDToTypeID( "None" );

    desc355.putEnumerated( idT, idOrdn, idNone );

executeAction( idsetd, desc355, DialogModes.NO );

// Merge down

var idMrgtwo = charIDToTypeID( "Mrg2" );

    var desc331 = new ActionDescriptor();

executeAction( idMrgtwo, desc331, DialogModes.NO );

// Select layer above current layer

var idslct = charIDToTypeID( "slct" );

    var desc337 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref190 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idFrwr = charIDToTypeID( "Frwr" );

        ref190.putEnumerated( idLyr, idOrdn, idFrwr );

    desc337.putReference( idnull, ref190 );

    var idMkVs = charIDToTypeID( "MkVs" );

    desc337.putBoolean( idMkVs, false );

executeAction( idslct, desc337, DialogModes.NO );

// Delete selected layer

var idDlt = charIDToTypeID( "Dlt " );

    var desc349 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref202 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref202.putEnumerated( idLyr, idOrdn, idTrgt );

    desc349.putReference( idnull, ref202 );

executeAction( idDlt, desc349, DialogModes.NO );

// Make new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

    var desc80 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref77 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref77.putClass( idLyr );

    desc80.putReference( idnull, ref77 );

executeAction( idMk, desc80, DialogModes.NO );

// Name layer: Magiclayer1

var idsetd = charIDToTypeID( "setd" );

    var desc16 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref16 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref16.putEnumerated( idLyr, idOrdn, idTrgt );

    desc16.putReference( idnull, ref16 );

    var idT = charIDToTypeID( "T   " );

        var desc17 = new ActionDescriptor();

        var idNm = charIDToTypeID( "Nm  " );

        desc17.putString( idNm, """Magiclayer1""" );

    var idLyr = charIDToTypeID( "Lyr " );

    desc16.putObject( idT, idLyr, desc17 );

executeAction( idsetd, desc16, DialogModes.NO );

// Add Magiclayer1 style

var idASty = charIDToTypeID( "ASty" );

    var desc15 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref14 = new ActionReference();

        var idStyl = charIDToTypeID( "Styl" );

        ref14.putName( idStyl, "Impasto" );

    desc15.putReference( idnull, ref14 );

    var idT = charIDToTypeID( "T   " );

        var ref15 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref15.putEnumerated( idLyr, idOrdn, idTrgt );

    desc15.putReference( idT, ref15 );

executeAction( idASty, desc15, DialogModes.NO );

}

switch(layerName){ 

    case 'Magiclayer1’: 

        call (rasterizeImpasto) 

    break; 

    case 'Magiclayer2’: 

        call (rasterizeImpasto) 

    break; 

    case 'SpecialMagic1': 

        call (rasterizeSpecialMagic1) 

    break; 

    case: 

    // Make new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

    var desc80 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref77 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref77.putClass( idLyr );

    desc80.putReference( idnull, ref77 );

executeAction( idMk, desc80, DialogModes.NO );

// Name layer style: base paint layer

var idsetd = charIDToTypeID( "setd" );

    var desc16 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref16 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref16.putEnumerated( idLyr, idOrdn, idTrgt );

    desc16.putReference( idnull, ref16 );

    var idT = charIDToTypeID( "T   " );

        var desc17 = new ActionDescriptor();

        var idNm = charIDToTypeID( "Nm  " );

        desc17.putString( idNm, """base paint layer""" );

    var idLyr = charIDToTypeID( "Lyr " );

    desc16.putObject( idT, idLyr, desc17 );

executeAction( idsetd, desc16, DialogModes.NO );

// Make new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

    var desc80 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref77 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref77.putClass( idLyr );

    desc80.putReference( idnull, ref77 );

executeAction( idMk, desc80, DialogModes.NO );

// Name layer: Magiclayer1

var idsetd = charIDToTypeID( "setd" );

    var desc16 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref16 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref16.putEnumerated( idLyr, idOrdn, idTrgt );

    desc16.putReference( idnull, ref16 );

    var idT = charIDToTypeID( "T   " );

        var desc17 = new ActionDescriptor();

        var idNm = charIDToTypeID( "Nm  " );

        desc17.putString( idNm, """Impasto""" );

    var idLyr = charIDToTypeID( "Lyr " );

    desc16.putObject( idT, idLyr, desc17 );

executeAction( idsetd, desc16, DialogModes.NO );

// Add Magiclayer1 layer style

var idASty = charIDToTypeID( "ASty" );

    var desc15 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref14 = new ActionReference();

        var idStyl = charIDToTypeID( "Styl" );

        ref14.putName( idStyl, "Impasto" );

    desc15.putReference( idnull, ref14 );

    var idT = charIDToTypeID( "T   " );

        var ref15 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref15.putEnumerated( idLyr, idOrdn, idTrgt );

    desc15.putReference( idT, ref15 );

executeAction( idASty, desc15, DialogModes.NO ); 

    break; 

    } 

Thanks for your assistance!
-Eric

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
Community Expert ,
Aug 17, 2017 Aug 17, 2017

Copy link to clipboard

Copied

I made a slight mistake with my above code, which I corrected. The last case - if you want a default case if the other cases don't match, should be with default. See my above post.

You need to simplify your code. The AM code makes things hard to read, so I like to put that stuff in functions and call them in my main code as needed. So something like:

var doc = activeDocument;

var curLayer = doc.activeLayer

myCode01 ()//I want the code in this function to be applied to everything

switch(curLayer.name){

    case 'layer name 1':

        myCode02 ()//I only want the code for this function used on this layer name

        break;//don't forget to use a break and a semicolon after each case.

    case 'layer name 2':

        myCode03 ()//I only want the code for this function used on this layer name

        break;

    default:

        myCode04 ()//I want the code in this function to run only if none of the other layer names match. A default section is optional.

        //no break; is needed

    }

myCode05 ()//This function will run after the switch and apply to all layer names

function myCode01(){

    //code here for this function

    }//end function

function myCode02(){

    //code here for this function

    }//end function

function myCode03(){

    //code here for this function

    }//end function

function myCode04(){

    //code here for this function

    }//end function

function myCode05(){

    //code here for this function

    }//end function

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 ,
Aug 20, 2017 Aug 20, 2017

Copy link to clipboard

Copied

function myCode01(){

   var doc = app.activeDocument, 

        lay = doc.activeLayer, 

        names = [‘magic layer1’,  

                 ‘magic layer2’,  

                 ‘magic layer3’

                   ‘special magic layer’]; 

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

        var dialog = confirm("End this Magic layer?"); 

        if (dialog) { 

            app.activeDocument.activeLayer.remove();

    }

    // Dialogue to rename ended Magic Layer

        if (dialog) {

    var aDoc = activeDocument; 

    var actLay = aDoc.activeLayer; 

        if (actLay.typename == "ArtLayer") { 

            var newName = prompt ("Would you like to rename this layer?", actLay.name, "Rename"); 

        try {actLay.name = newName;} catch (e) {}; 

           }

        }  

     }   

}

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
Community Expert ,
Aug 20, 2017 Aug 20, 2017

Copy link to clipboard

Copied

I'm assuming you have more code outside of this function. If not you really don't need to wrap it all in a function. You also seem to have one extra brace, so not sure what's going on with that. You have two if statements right next to each other for your dialog (lines 11 and 15) Those should be combined. If you want the script to end, you just have to make sure there is no code to follow. So if there is more code outside this function, and you call the function, and there is code after that function call, it will still play the code. If you really want to stop the script, you could put the entire script in a try/catch block then when you want the script to end do something like this

try{

   alert('before the throw')

   throw 'The script will end'

   alert('after the throw')

   }

catch(e){alert(e)}

This will play the alert, throw an error and the show the alert of the throw, but not play the second alert.

As far as suspend history, I'll have to look into that. I really don't use that.

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 ,
Aug 20, 2017 Aug 20, 2017

Copy link to clipboard

Copied

Yes, that is what is occurring,... the script is going to the next code (this function is line 4 of the script that you supplied for me:)

myCode01 ()//I want the code in this function to be applied to everything

So the switch statement follows.  What I am trying to accomplish is that if I enact the script and the name of the layer match's any of the designated names AND there are NO active pixels in the layer (the layer is empty), then it will throw the "End this magic layer?" dialog.  If I answer 'yes', the active layer will be deleted and the dialog to rename the new active layer (the layer that was below the previous layer) is enacted.  This IS happening,... but,... as you said, the script is continuing on to the switch statement and calling an error; As this script will ONLY be enacted should the layer be empty, I want it to conclude after the dialog to rename the layer.

The same issue seems to occur should I answer 'no' to the "End magic layer?" dialog; rather than just concluded,... it continues on to the switch statement.  I think I understand what you are suggesting, but I would rather the script just conclude on its own for those two circumstances without throwing an additional alert for me to interact with.

Is there a way to ask the script to only run this function if the layer has NO active pixels (layer is empty) AND then without continuing to the switch statement?

PS. The extra brace was because I had it wrapped in a function,... which I now realize that I didn't need to do....

Thanks,

-Eric

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
Community Expert ,
Aug 20, 2017 Aug 20, 2017

Copy link to clipboard

Copied

You can also set a variable to end the script. Say

var run = true;

Then when you want to end the script, make sure any code after your switch or whatever you have is in an if statement to check if run is still true.

If you are doing a loop, and want to end it, which it sounds like you're doing, put a break; in an if statement and the loop will stop.

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 ,
Aug 20, 2017 Aug 20, 2017

Copy link to clipboard

Copied

I'm sorry, but my javascript knowledge is minimal;  I kind of understand what you're saying intellectually, but I can't seem to figure out where to put  the break; in order to make it work:

var doc = activeDocument; 

var curLayer = doc.activeLayer, 

        names = [‘magic layer1’,  

                 ‘magic layer2’,  

                 ‘magic layer3’,

                 ‘special magic layer1’,

                 ‘special magic layer2’]; 

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

        var dialog = confirm("End this Magic layer?"); 

        if (dialog) { 

            app.activeDocument.activeLayer.remove();

    }

    // Dialogue to rename ended Magic Layer

        if (dialog) {

    var aDoc = activeDocument; 

    var actLay = aDoc.activeLayer; 

        if (actLay.typename == "ArtLayer") { 

            var newName = prompt ("Would you like to rename this layer?", actLay.name, "Rename"); 

        try {actLay.name = newName;} catch (e) {}; 

           }

        }  

     }   

switch(curLayer.name){ 

    case 'magic layer1': 

        rasterizeMagic() 

    break; 

    case 'magic layer2’: 

        rasterizeMagic() 

    break;

    case 'magic layer3’: 

        rasterizeMagic() 

    break;

    case 'special magic layer1': 

        rasterizeSpecialmagic() 

    break;

    case 'special magic layer2’: 

        rasterizeSpecialmagic() 

    break;  

    default: 

    begin magiclayers() 

    } 

//functions for switch statements begin below here

low here

////functions for switch statements begin below here for switch statements begin below here

I seemed to me, that placing the break; between line 20 and 21 should work,... but alas it doesn't end the script.  Also, I'm at a lost for how to end the script if the dialog on line 10 is NOT confirmed.

Thanks for your patience and consideration,

-Eric

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
Community Expert ,
Aug 21, 2017 Aug 21, 2017

Copy link to clipboard

Copied

You might want to try the structure of this script:

var doc = activeDocument;

var actLayer = doc.activeLayer;

var run = true;

var layArray = new Array();

/////////

//loop to get layers and put into an array so you can make them active. If you delete layers in a loop, you need to do something like this

for(var i = 0;i>doc.layers.Length;i++){

    layArray.push (doc.activeLayer);

    }//end loop

//This is the loop that can cycle through layers or whatever you want

for(var i = 0;i<layArray.length;i++){

    //if for checking if layer is pixel based and not empty

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

        actLayer = layArray;//goes through all layers that were put in the layer array

        doc.activeLayer = actLayer;

       

        //add confirm here to remove layer

        var removeLayer = confirm('Do you want to remove magic layer?');

        if(removeLayer){

            actLayer.remove();

            //add confirm to end the script. if no, the script will continue and go to the next layer in the array

            //if you alway want to end the script if you remove a layer, you don't have to have this second confirm statment

            //just put the change the run variable to false within this if statement

            var endScript = confirm ('Do you want to end the script?');

            if(endScript){run = false}//set run to false to end the script

            }//end if for removing layer

        else{//the else will run the name of the layer through the switch and continue the script

            //add prompt to rename the layer

            var chgLayerName = prompt ('Do you want to rename this layer?', actLayer.name, 'Rename');

            if(chgLayerName !=null){actLayer.name = chgLayerName};//makes sure the cancel button isn't pressed

            switchForLayerNames ();

            };//end else

    }//end if to see if the active layer is a pixel based layer

    if(!run){break;}//This will stop the loop and the script should stop if you don't have any code after the loop.

    };//end brace for loop

function switchForLayerNames(){

    switch(actLayer.name){

        case 'some layer name 1':

            //code here to do something to this layer

            break;//need break after each case statement

        case 'some layer name 1':

            //more code

            break;

        default:

            //this will catch the layers that don't have a name matched in your switch statement.

            //If this is the layer you want the confirm statement, you can put it here, you may not need it here,

            //and it can be removed since it's also in the code in the loop:

            var askQuestion = confirm ('Do you want to end the script?');

            //if statement to check which choice was selected: a true would be yes, to ending the script

            if(askQuestion){run = false}// if the selection to end the script is true, the run variable is set to false;

        };//end brace for switch

    ////Here you use an if statement to end the loop if run was set to false;  

    }//end function switchForLayerNames

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 ,
Aug 27, 2017 Aug 27, 2017

Copy link to clipboard

Copied

var doc = activeDocument; 

var curLayer = doc.activeLayer, 

        names = [‘magic layer1’,  

                 ‘magic layer2’,  

                 ‘magic layer3’,

                 ‘special magic layer1’,

                 ‘special magic layer2’]; 

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

        var dialog = confirm("End this Magic layer?"); 

        if (dialog) { 

            app.activeDocument.activeLayer.remove();

    }

    // Dialogue to rename ended Magic Layer

        if (dialog) {

    var aDoc = activeDocument; 

    var actLay = aDoc.activeLayer; 

        if (actLay.typename == "ArtLayer") { 

            var newName = prompt ("Would you like to rename this layer?", actLay.name, "Rename"); 

        try {actLay.name = newName;} catch (e) {}; 

           }

        }  

     }   

switch(curLayer.name){ 

    case 'magic layer1': 

        rasterizeMagic() 

    break; 

    case 'magic layer2’: 

        rasterizeMagic() 

    break;

    case 'magic layer3’: 

        rasterizeMagic() 

    break;

    case 'special magic layer1': 

        rasterizeSpecialmagic() 

    break;

    case 'special magic layer2’: 

        rasterizeSpecialmagic() 

    break;  

    default: 

    begin magiclayers() 

    } 

//functions for switch statements begin below here

  • Lines 3-7 are for the purpose of ensuring that the code that begins on line 9 enacts ONLY if the layer matches the array of DESIGNATED NAMES.  For some reason, that is NOT occurring,… the code for line 9 is enacting for ANY layer that has zero active pixels.  How can I ensure that this script only enacts when the layer name matches the 'names array'?

  • Lines 9-13 should (if the layer name matches the array AND the layer has zero active pixels) produce a dialog box that will ask if I would like to “End this Magic Layer?”  If I answer “No”, I simply want the dialog box to close and nothing else.  Script over.  That is NOT happening,… the script is continuing on to the switch statement;  how do I stop that from occurring?  This is because sometimes in the midst of painting, I accidentally enact the script when I don’t intend to; this allows me to stop the script in such cases. 

  • If I answer “Yes”, then line 12 will simply delete the active layer (the layer that has zero active pixels and matches the array of names) and then:

  • Lines 15-19 will allow me to rename the layer below the previously deleted layer, should I wish to. (You previously asked me why I have two statements right next to each other for dialog, lines 11 and 15.  I believe that is because the enacting of the dialog on line 15 is dependent upon the answer to the dialog on line 11,... that is to say, should I answer "No" to the dialog on line 11, then it is unnecessary to continue to the dialog for line 15, as the script should simply end.)  Once the dialog to rename to remaining layer is complete, I would like the script to stop.  Nothing else,… no additional dialogs,…. Nothing.  Script over.  This is also NOT happening, as the script is also continuing on to the switch statement; how do I stop that from occurring?

Anyone and Everyone and especially ,

Thanks for the time and effort,

-Eric

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
Community Expert ,
Aug 27, 2017 Aug 27, 2017

Copy link to clipboard

Copied

I made a few changes, but you still might have issues. Your list of name is just an array of names, I can't see any place where you check that list of name against the current layer name. I added a loop for that that will then set the script to run by setting the variable 'run' to true.

On your confirm if you want to delete the layer, you don't have an else for if you select no, so of course the script will keep running. You have to set the run variable to false, so it will stop any other code from running by wrapping it in a if statement.

I still don't think you need two if statements for your confirm. The else to end the script takes are of that, and all the code that is suppose to run if you select yes should be in that one if statement.

var doc = activeDocument;   

var curLayer = doc.activeLayer, 

var run = false;

        names = [‘magic layer1’,     //this is just an array. I don't see where you're checking it against the names of the layer

                 ‘magic layer2’,    

                 ‘magic layer3’, 

                 ‘special magic layer1’, 

                 ‘special magic layer2’]; 

                

for (var i = 0; i<names.length;i++){//This loops through the names in the array and sees if there is a match with the current layer. If so it sets the rest of the script to run.

    if(curLayer.name == names){run = true}

    }//end loop

 

    if (doc.activeLayer.kind == LayerKind.NORMAL && doc.activeLayer.bounds[2] == 0 && doc.activeLayer.bounds[3] == 0 && run) { //add run check to see if script needs to run  

        var dialog = confirm("End this Magic layer?");   

        if (dialog) {   

            app.activeDocument.activeLayer.remove();  //you defined doc as the activedocument, so why use app.activeDocument here?

            var aDoc = activeDocument;    //move code to main if for dialog, also you defined the activedocument at the top. You don't need to redefine it, unless there are two open docs.

            var actLay = aDoc.activeLayer;   

            if (actLay.typename == "ArtLayer") {   

                var newName = prompt ("Would you like to rename this layer?", actLay.name, "Rename");   

                try {actLay.name = newName;} catch (e) {};   

               }   //end if for artLayer           

            }  //end if for dialog

        else{run = false};//add to end script if you select no on confirm

     }   //end if for layer checking  

 

if(run){//wrap switch in if statement

    switch(curLayer.name){   

        case 'magic layer1':   

            rasterizeMagic()   

        break;   

        case 'magic layer2':   

            rasterizeMagic()   

        break; 

        case 'magic layer3':   

            rasterizeMagic()   

        break; 

        case 'special magic layer1':   

            rasterizeSpecialmagic()   

        break; 

        case 'special magic layer2':   

            rasterizeSpecialmagic()   

        break;    

        default:   

        begin magiclayers() //What's the begin? looks like that will throw an error. Plus this will not run unless you have a match now with the names in your array.  

        } //end switch  

}//end if to check to see if switch should run

//functions for switch statements begin below here 

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 ,
Aug 27, 2017 Aug 27, 2017

Copy link to clipboard

Copied

I seem to be catching an error on line 3 at “run + false;”

How the script functions in its entirety:

When I run the script:

    ▪    If the active layer does NOT match any of the names in the array, the script will make two layers:

                  A layer labeled: “base paint”

                  A layer above the “base paint” layer labeled, for instance: “Magic layer 1” (which has a corresponding layer style).

    ▪    If I draw on this “Magic layer1” (active pixels on the layer) and then enact the script, the code should run through the array of names, determine that there are active pixels and then continue on to the switch statement, which will cause it to rasterize the layer style, merge it down on to the “base paint” layer, and then create a new layer above the “base paint” layer, that is labeled, for instance, “Magic layer 3” (the “Magic layer” that is created will depend upon the script that I enact,… the change in name and applied layer style is dictated in the functions).

    ▪    If I DON’T draw on this “Magic layer1” (no active pixels on the layer) and then enact the script, the code should run through the array of names, determine that there are NO active pixels and then run the script to end (should I answer “No” to the initial dialog) or run the script to delete the active layer and give the dialog to rename the remaining layer and then end.

The script is supposed to work as a “one button” solution for utilizing all of the layer styles that I use in my art.

Thanks again,

-Eric

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
Community Expert ,
Aug 28, 2017 Aug 28, 2017

Copy link to clipboard

Copied

Line 3 should be:

var run = false;

not

var run + false;

To create your new layers, add the code to do that in the else statement at line 25 in my last post.

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 ,
Aug 28, 2017 Aug 28, 2017

Copy link to clipboard

Copied

Oops,....

  1. var run = false

... is actually what I have in the script (just missed hitting the shift key whilst typing that message),... but still getting an error.

All of the making of layers, rasterization and such is achieved in the switch statements; I only need the code that takes place before the switch statement to work properly (the code responsible for ensuring that if the layer matches the names array AND does/does not have active pixels).

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
Community Expert ,
Aug 29, 2017 Aug 29, 2017

Copy link to clipboard

Copied

What error are you getting with that line? Post the code that you have upto and including it, to make sure there's no type. It shouldn't error on something simple like that.

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 ,
Aug 31, 2017 Aug 31, 2017

Copy link to clipboard

Copied

Okay.  Here is the entire script.

var doc = activeDocument;

var curLayer = doc.activeLayer,   

var run = false; 

    names = [‘magic layer1’, //this is just an array. I don't see where you're checking it against the names of the layer 
             ‘magic layer2’, 
             ‘magic layer3’,   
             ‘special magic layer1’,   
             ‘special magic layer2’];   
             

for (var i = 0; i<names.length;i++){//This loops through the names in the array and sees if there is a match with the current layer. If so it sets the rest of the script to run. 

if(curLayer.name == names){run = true} 
}//end loop 

   

if (doc.activeLayer.kind == LayerKind.NORMAL && doc.activeLayer.bounds[2] == 0 && doc.activeLayer.bounds[3] == 0 && run) { //add run check to see if script needs to run
    var dialog = confirm("End this Magic layer?");
    if (dialog) {
        app.activeDocument.activeLayer.remove();  //you defined doc as the activedocument, so why use app.activeDocument here? 
        var aDoc = activeDocument;//move code to main if for dialog, also you defined the activedocument at the top. You don't need to redefine it, unless there are two open docs. 
        var actLay = aDoc.activeLayer;
        if (actLay.typename == "ArtLayer") {
            var newName = prompt ("Would you like to rename this layer?", actLay.name, "Rename");
            try {actLay.name = newName;} catch (e) {};
           }   //end if for artLayer        
        }  //end if for dialog 
    else{run = false};//add to end script if you select no on confirm 
}   //end if for layer checking

   

if(run){//wrap switch in if statement  

switch(curLayer.name){
    case 'magic layer1':
        rasterizeMagic()
    break;
    case 'magic layer2':
        rasterizeMagic()
    break;   
    case 'magic layer3':
        rasterizeMagic()
    break;   
    case 'special magic layer1':
        rasterizeSpecialmagic()
    break;   
    case 'special magic layer2':
        rasterizeSpecialmagic()
    break; 
    default:
    begin magiclayers() //What's the begin? looks like that will throw an error. Plus this will not run unless you have a match now with the names in your array.
    } //end switch

}//end if to check to see if switch should run  

//functions for switch statements begin below here

function rasterizeMagic(){ 

    //Rasterize Impasto layer

    var idrasterizeLayer = stringIDToTypeID( "rasterizeLayer" );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref4 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref4.putEnumerated( idLyr, idOrdn, idTrgt );
desc5.putReference( idnull, ref4 );
var idWhat = charIDToTypeID( "What" );
var idrasterizeItem = stringIDToTypeID( "rasterizeItem" );
var idlayerStyle = stringIDToTypeID( "layerStyle" );
desc5.putEnumerated( idWhat, idrasterizeItem, idlayerStyle );

executeAction( idrasterizeLayer, desc5, DialogModes.NO );

// Merge layer down

var idMrgtwo = charIDToTypeID( "Mrg2" );

var desc43 = new ActionDescriptor();

executeAction( idMrgtwo, desc43, DialogModes.NO );

// Make new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

var desc80 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref77 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref77.putClass( idLyr );
desc80.putReference( idnull, ref77 );

executeAction( idMk, desc80, DialogModes.NO );

// Name layer style: magic layer1

var idsetd = charIDToTypeID( "setd" );

var desc16 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref16 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref16.putEnumerated( idLyr, idOrdn, idTrgt );
desc16.putReference( idnull, ref16 );
var idT = charIDToTypeID( "T   " );
    var desc17 = new ActionDescriptor();
    var idNm = charIDToTypeID( "Nm  " );
    desc17.putString( idNm, """magic layer1""" );
var idLyr = charIDToTypeID( "Lyr " );
desc16.putObject( idT, idLyr, desc17 );

executeAction( idsetd, desc16, DialogModes.NO );

// Add magic layer1 style

var idASty = charIDToTypeID( "ASty" );

var desc15 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref14 = new ActionReference();
    var idStyl = charIDToTypeID( "Styl" );
    ref14.putName( idStyl, "magic layer1" );
desc15.putReference( idnull, ref14 );
var idT = charIDToTypeID( "T   " );
    var ref15 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref15.putEnumerated( idLyr, idOrdn, idTrgt );
desc15.putReference( idT, ref15 );

executeAction( idASty, desc15, DialogModes.NO ); 

    }//end function 

 

function rasterizeSpecialmagic(){ 

  

// Load transparency from layer

var idsetd = charIDToTypeID( "setd" );

var desc272 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref127 = new ActionReference();
    var idChnl = charIDToTypeID( "Chnl" );
    var idfsel = charIDToTypeID( "fsel" );
    ref127.putProperty( idChnl, idfsel );
desc272.putReference( idnull, ref127 );
var idT = charIDToTypeID( "T   " );
    var ref128 = new ActionReference();
    var idChnl = charIDToTypeID( "Chnl" );
    var idChnl = charIDToTypeID( "Chnl" );
    var idTrsp = charIDToTypeID( "Trsp" );
    ref128.putEnumerated( idChnl, idChnl, idTrsp );
desc272.putReference( idT, ref128 );

executeAction( idsetd, desc272, DialogModes.NO );

// Refine selection edges

var idrefineSelectionEdge = stringIDToTypeID( "refineSelectionEdge" );

var desc273 = new ActionDescriptor();
var idrefineEdgeBorderRadius = stringIDToTypeID( "refineEdgeBorderRadius" );
var idPxl = charIDToTypeID( "#Pxl" );
desc273.putUnitDouble( idrefineEdgeBorderRadius, idPxl, 0.000000 );
var idrefineEdgeBorderContrast = stringIDToTypeID( "refineEdgeBorderContrast" );
var idPrc = charIDToTypeID( "#Prc" );
desc273.putUnitDouble( idrefineEdgeBorderContrast, idPrc, 0.000000 );
var idrefineEdgeSmooth = stringIDToTypeID( "refineEdgeSmooth" );
desc273.putInteger( idrefineEdgeSmooth, 0 );
var idrefineEdgeFeatherRadius = stringIDToTypeID( "refineEdgeFeatherRadius" );
var idPxl = charIDToTypeID( "#Pxl" );
desc273.putUnitDouble( idrefineEdgeFeatherRadius, idPxl, 0.000000 );
var idrefineEdgeChoke = stringIDToTypeID( "refineEdgeChoke" );
var idPrc = charIDToTypeID( "#Prc" );
desc273.putUnitDouble( idrefineEdgeChoke, idPrc, 80.000000 );
var idrefineEdgeAutoRadius = stringIDToTypeID( "refineEdgeAutoRadius" );
desc273.putBoolean( idrefineEdgeAutoRadius, false );
var idrefineEdgeDecontaminate = stringIDToTypeID( "refineEdgeDecontaminate" );
desc273.putBoolean( idrefineEdgeDecontaminate, false );
var idrefineEdgeOutput = stringIDToTypeID( "refineEdgeOutput" );
var idrefineEdgeOutput = stringIDToTypeID( "refineEdgeOutput" );
var idrefineEdgeOutputSelection = stringIDToTypeID( "refineEdgeOutputSelection" );
desc273.putEnumerated( idrefineEdgeOutput, idrefineEdgeOutput, idrefineEdgeOutputSelection );

executeAction( idrefineSelectionEdge, desc273, DialogModes.NO );

// Select layer below current layer

var idslct = charIDToTypeID( "slct" );

var desc301 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref154 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idBckw = charIDToTypeID( "Bckw" );
    ref154.putEnumerated( idLyr, idOrdn, idBckw );
desc301.putReference( idnull, ref154 );
var idMkVs = charIDToTypeID( "MkVs" );
desc301.putBoolean( idMkVs, false );

executeAction( idslct, desc301, DialogModes.NO );

// Create new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

var desc274 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref129 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref129.putClass( idLyr );
desc274.putReference( idnull, ref129 );

executeAction( idMk, desc274, DialogModes.NO );

// Stamp visible into new layer

var idMrgV = charIDToTypeID( "MrgV" );

var desc275 = new ActionDescriptor();
var idDplc = charIDToTypeID( "Dplc" );
desc275.putBoolean( idDplc, true );

executeAction( idMrgV, desc275, DialogModes.NO );

// Invert selection

var idInvs = charIDToTypeID( "Invs" );

executeAction( idInvs, undefined, DialogModes.NO );

// Delete selection

var idDlt = charIDToTypeID( "Dlt " );

executeAction( idDlt, undefined, DialogModes.NO );

// Undo selection

var idsetd = charIDToTypeID( "setd" );

var desc355 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref208 = new ActionReference();
    var idChnl = charIDToTypeID( "Chnl" );
    var idfsel = charIDToTypeID( "fsel" );
    ref208.putProperty( idChnl, idfsel );
desc355.putReference( idnull, ref208 );
var idT = charIDToTypeID( "T   " );
var idOrdn = charIDToTypeID( "Ordn" );
var idNone = charIDToTypeID( "None" );
desc355.putEnumerated( idT, idOrdn, idNone );

executeAction( idsetd, desc355, DialogModes.NO );

// Merge down

var idMrgtwo = charIDToTypeID( "Mrg2" );

var desc331 = new ActionDescriptor();

executeAction( idMrgtwo, desc331, DialogModes.NO );

// Select layer above current layer

var idslct = charIDToTypeID( "slct" );

var desc337 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref190 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idFrwr = charIDToTypeID( "Frwr" );
    ref190.putEnumerated( idLyr, idOrdn, idFrwr );
desc337.putReference( idnull, ref190 );
var idMkVs = charIDToTypeID( "MkVs" );
desc337.putBoolean( idMkVs, false );

executeAction( idslct, desc337, DialogModes.NO );

// Delete selected layer

var idDlt = charIDToTypeID( "Dlt " );

var desc349 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref202 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref202.putEnumerated( idLyr, idOrdn, idTrgt );
desc349.putReference( idnull, ref202 );

executeAction( idDlt, desc349, DialogModes.NO );

// Make new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

var desc80 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref77 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref77.putClass( idLyr );
desc80.putReference( idnull, ref77 );

executeAction( idMk, desc80, DialogModes.NO );

// Name layer style: magic layer1

var idsetd = charIDToTypeID( "setd" );

var desc16 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref16 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref16.putEnumerated( idLyr, idOrdn, idTrgt );
desc16.putReference( idnull, ref16 );
var idT = charIDToTypeID( "T   " );
    var desc17 = new ActionDescriptor();
    var idNm = charIDToTypeID( "Nm  " );
    desc17.putString( idNm, """magic layer1""" );
var idLyr = charIDToTypeID( "Lyr " );
desc16.putObject( idT, idLyr, desc17 );

executeAction( idsetd, desc16, DialogModes.NO );

// Add magic layer1 style

var idASty = charIDToTypeID( "ASty" );

var desc15 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref14 = new ActionReference();
    var idStyl = charIDToTypeID( "Styl" );
    ref14.putName( idStyl, "magic layer1" );
desc15.putReference( idnull, ref14 );
var idT = charIDToTypeID( "T   " );
    var ref15 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref15.putEnumerated( idLyr, idOrdn, idTrgt );
desc15.putReference( idT, ref15 );

executeAction( idASty, desc15, DialogModes.NO );

    }//end function 

 

function begin magiclayers(){ 

    // Make new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

var desc80 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref77 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref77.putClass( idLyr );
desc80.putReference( idnull, ref77 );

executeAction( idMk, desc80, DialogModes.NO );

// Name layer style: base paint layer

var idsetd = charIDToTypeID( "setd" );

var desc16 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref16 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref16.putEnumerated( idLyr, idOrdn, idTrgt );
desc16.putReference( idnull, ref16 );
var idT = charIDToTypeID( "T   " );
    var desc17 = new ActionDescriptor();
    var idNm = charIDToTypeID( "Nm  " );
    desc17.putString( idNm, """base paint layer""" );
var idLyr = charIDToTypeID( "Lyr " );
desc16.putObject( idT, idLyr, desc17 );

executeAction( idsetd, desc16, DialogModes.NO );

// Make new layer above current layer

var idMk = charIDToTypeID( "Mk  " );

var desc80 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref77 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    ref77.putClass( idLyr );
desc80.putReference( idnull, ref77 );

executeAction( idMk, desc80, DialogModes.NO );

// Name layer style: magic layer1

var idsetd = charIDToTypeID( "setd" );

var desc16 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref16 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref16.putEnumerated( idLyr, idOrdn, idTrgt );
desc16.putReference( idnull, ref16 );
var idT = charIDToTypeID( "T   " );
    var desc17 = new ActionDescriptor();
    var idNm = charIDToTypeID( "Nm  " );
    desc17.putString( idNm, """magic layer1""" );
var idLyr = charIDToTypeID( "Lyr " );
desc16.putObject( idT, idLyr, desc17 );

executeAction( idsetd, desc16, DialogModes.NO );

// Add magic layer1 style

var idASty = charIDToTypeID( "ASty" );

var desc15 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref14 = new ActionReference();
    var idStyl = charIDToTypeID( "Styl" );
    ref14.putName( idStyl, "magic layer1" );
desc15.putReference( idnull, ref14 );
var idT = charIDToTypeID( "T   " );
    var ref15 = new ActionReference();
    var idLyr = charIDToTypeID( "Lyr " );
    var idOrdn = charIDToTypeID( "Ordn" );
    var idTrgt = charIDToTypeID( "Trgt" );
    ref15.putEnumerated( idLyr, idOrdn, idTrgt );
desc15.putReference( idT, ref15 );

executeAction( idASty, desc15, DialogModes.NO );

    }//end function 

For some reason this script is coming out double spaced?!  Anyway,... if you want to test it, you only need to make a layer style named "magic layer1".   Doesn't matter what kind of layer style it is,.... I guess you could make it a 'drop shadow' style.  Also,... I'm not sure if I ever specified that I am running CS6.

Thanks again,

-Eric

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 ,
Sep 03, 2017 Sep 03, 2017

Copy link to clipboard

Copied

,

So I got a little time to play around with this today; it was beneficial for me to step away, give myself a breather, and come back with a fresh eye.  I finally figured it out!!  I basically put the "layer pixel count/dialog box function" within the functions that are already in the switch statement with an "if/else" and voila,.... it worked perfectly!  I want to thank you so, so much for all of your assistance, patience, and especially for the learning experience.  I wouldn't call myself anywhere near 'proficient' in javascript now, but I do understand so much more than I did when started this script rewrite.  Once again,... much appreciation; I wouldn't have been able to figure this out without your help.

Thank you,

-Eric

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
Community Expert ,
Sep 03, 2017 Sep 03, 2017

Copy link to clipboard

Copied

LATEST

Glad you got it figured out. I've been a bit swamped with other things, so haven't had time to look at 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