Saturday, April 16, 2011

Save Layer States

OK, here's a really quick shot at saving layer states.

The script doesn't track by layer name, so if you add or remove a layer between uses, your layer visibility will be shifted. Fortunately, I'm using a simple system of 1's and 0's to track visibility, so you can easily edit the saved values if you added or removed a layer between runs.

Another limitation/caveat: This version doesn't look at sub-layers, just top level layers.

Someone who know how to write plugins should probably write something like this, but until then, if you're dying for a layer "save state" script, this should get you started.

Just run the script posted below and a prompt will come up with a bunch of ones and zeros in the text entry field. These represent the visibility of each root layer in your document. If you want to save your layer state, just manually copy the prompt data to a text document for use later.

When you want to reset your visibility to an older saved state, just rerun the script, and paste your saved string back into the prompt window, (erasing the existing data) and your layers will return to the original visibility settings.

Enjoy, and if you have any suggestions, requests or improvements, please email me, or post in the comments below.
 cheers,
-J

var doc = activeDocument;

var binArr=[];
var i = 0;



for(var d=doc.layers.length-1;d>=0;d--)
{
        i= doc.layers[d].visible ? 1:0;
        binArr.unshift(i);
}

var p=prompt("Copy values to store layer visibility states, paste new data below to change.",binArr.join(""));

if (p!=null)
{
    binArr = p.split("");
    for(var d=binArr.length-1;d>=0;d--)
    {
            if(doc.layers.length  > d){
                doc.layers[d].visible  = binArr[d] ==    0   ?  false : true;
            }
    }
    
}

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