One of the big problems I have had is trying to work with Adobe Illustrator layers. Editing layers is so manual that it really does take the bulk of my time if I have to do a lot of changes. This is a weak area where Adobe could improve the interface; but in the meantime, here is a couple Illustrator Javascript functions that will recurse through all layers, and run whatever function you send as a parameter.

Please read the commented section in the code for more clarification, or post a comment and I will try to answer your questions.

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

    var ogDoc = documents[0];

// Recurse through all layers, running the function passed as a parameter.
// Extra parameter entry available if needed.
// Note: Skips all layers where the layer's name begins with an underscore ("_").

// Display the name of all layers, one at a time.
//recurseLayers(ogDoc.layers, tellLayer, null);

// Add an underscore to the front of any layer name that does not already start with an underscore.
// Does not recurse into layers that already have an underscore, though; you can change the recurseLayers() function to do that.
//recurseLayers(ogDoc.layers, initUndescoreLayerName, true);

alert( "Edit this file to pick the function to run.");

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

function tellLayer(lyr)
{
    alert( lyr.name );
}

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

function recurseLayers(ogLyrs, func, param)
{
    var curLy;
    var isInitiallyLocked;

    for( var j = ogLyrs.length - 1; j >= 0; j--)
    {
        // Skip this layer if the layer's name begins with an underscore ("_").
        if( ogLyrs[j].name.substr(0,1) == '_' )
            continue;

        //alert(ogLys[j].name);

        // Store locked state of the source layer.
        // Note: Copying is allowed from a locked layer, but pasting to a locked layer is not.
        isInitiallyLocked = ogLyrs[j].locked;
        ogLyrs[j].locked = false;

        func(ogLyrs[j], param);

        // RECURSE: Crawl all sub-layers.
        if( ogLyrs[j].layers.length > 0 )
            recurseLayers( ogLyrs[j].layers, func, param );

        // restore locked state:
        if( isInitiallyLocked ) ogLyrs.locked = true;

        // For testing:
        //break;
    }
}

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

function initUndescoreLayerName(lyr, turnOnUnderscore)
{
    alert(lyr.name + " ... " + lyr.name.substr(0,1));

    // Add underscore.
    if( turnOnUnderscore )
    {
        // Skip if there is already an underscore.
        if( lyr.name.substr(0,1) == '_' )
            return;
        else
            lyr.name = "_" + lyr.name;
    }

        // Remove underscore.
    else
    {
        if( lyr.name.substr(0,1) == '_' )
            lyr.name = lyr.name.substr(1, lyr.name.length);
        else
            return;
    }
}

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

– 30 –

Advertisements