Clean Up Layer Script (Error Help)

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

mycort

Clean Up Layer Script (Error Help)

Post by mycort »

I have crudely put this script together and using script listener for some code, it does the following:
- Delete all empty, hidden and layer comps in the psd file.

Now I am getting an error while I run it. It sometimes works and sometimes does not. I've attached a screenshot of the error and a code below.



Code: Select all    main();
    function main(){
    if(!documents.length) return;
    var layerInfo = getLayerInfo();
    for(var a in layerInfo){//delete blank layers
        if(layerInfo[a][2] == 'false') deleteLayerByID(Number(layerInfo[a][0]));
        }
    for(var z in layerInfo){
         if(layerInfo[z][2] == 'true'){//delete empty layerSets
             selectLayerById(Number(layerInfo[z][0]));
             if(activeDocument.activeLayer.layers.length == 0) deleteLayerByID(Number(layerInfo[z][0]));
             }
        }
    };
    function deleteLayerByID(ID) {
        var desc = new ActionDescriptor();
            var ref = new ActionReference();
            ref.putIdentifier(charIDToTypeID('Lyr '), ID);
        desc.putReference( charIDToTypeID('null'), ref );
        executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
    };
    function selectLayerById(ID, add) {
        add = (add == undefined)  ? add = false : add;
       var ref = new ActionReference();
       ref.putIdentifier(charIDToTypeID('Lyr '), ID);
       var desc = new ActionDescriptor();
       desc.putReference(charIDToTypeID('null'), ref);
       if (add) {
          desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
       }
       desc.putBoolean(charIDToTypeID('MkVs'), false);
       executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
    }
    function getLayerInfo(){
       var ref = new ActionReference();
       ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
       var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
       var Names=[];
    try{
        activeDocument.backgroundLayer;
    var i = 0; }catch(e){ var i = 1; };
       for(i;i<count;i++){
           if(i == 0) continue;
            ref = new ActionReference();
            ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
            var desc = executeActionGet(ref);
            var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
            var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
            if(layerName.match(/^<\/Layer group/) ) continue;
            var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
            var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
            var boundsDesc = desc.getObjectValue(stringIDToTypeID( "bounds" ));
    if(boundsDesc.getUnitDoubleValue(stringIDToTypeID('right')) - boundsDesc.getUnitDoubleValue(stringIDToTypeID('left')) == 0){
        Names.push([[Id],[layerName],[isLayerSet]]);
        }
    if(isLayerSet) Names.push([[Id],[layerName],[isLayerSet]]);
       };
    return Names;
    };



// Delete All Hidden Layers

    var idDlt = charIDToTypeID( "Dlt " );
    var desc2 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref1 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        var idOrdn = charIDToTypeID( "Ordn" );
        var idhidden = stringIDToTypeID( "hidden" );
        ref1.putEnumerated( idLyr, idOrdn, idhidden );
    desc2.putReference( idnull, ref1 );
executeAction( idDlt, desc2, DialogModes.NO );


// Delete All Comp Layers
if (app.documents.length > 0) {

var myDocument = app.activeDocument;

while (myDocument.layerComps.length > 0) {

myDocument.layerComps[0].remove()

}

};