Thursday, April 07, 2011

Quick and Dirty Label Layers

Here's a simple script I wrote to auto-label layers. It will only label the layers of objects that you've selected, and it will only label a layer if the object has a name, or is a placed image with a image link.
If an object has a name, that will take priority over the link name.  I might make this an official script, but meanwhile, take this for a testdrive. Just copy the script below and save it as
AutoLabelLayers.jsx
It should work cross platform.  Let me know how it goes...

cheers,
-J


// AutoLabelLayers.jsx beta JS4AI script by wundes.com
var doc = activeDocument;
var sel = doc.selection;
var selLen = sel.length;

//prevent renaming of existing layer names.
var renameLayers = true;

for(var x=0; x<selLen;x++)
{
    assignLayerName(sel[x]);
}
function assignLayerName(obj)
{
    var imageName = getName(obj);
    if(imageName != null)
    {
        var layer = obj.layer;
        while (layer.parent.typename=="Layer")
        {
            //name all layers up the layer tree:
           //layer.name = imageName;
            nameLayer(layer,imageName);
            layer = layer.parent;
                
        }
           nameLayer(layer,imageName);
     } 
}


function nameLayer(_layer,_name)
{
        if(   (_layer.name.indexOf("Layer ")  == 0)  ||   (renameLayers == true)  )
        {
            _layer.name = _name;
        }
}

function getName(obj)
{
       if (obj.name.length>0)
       {
           return obj.name;
        }
   else
   if(obj.typename == "PlacedItem" || obj.typename == "RasterItem" )
    {
        var nString = obj.file.toString();        
        var fileDiv="\\";
        if(nString.indexOf("/")  != -1)
        {
            fileDiv = "/";
        }
        var nArr = nString.split(fileDiv);
        var nString = nArr[nArr.length - 1];
        if(nString.indexOf(".") == nString.length-4)
        {
            nString = nString.split(".")[0];
         }
        return nString;        
    }
return null;
}

No comments: