Copy link to clipboard
Copied
I found a script to select the previous layer and set it as visible. It works but I'd like to modify it slightly if possible.
At the moment if you start from the selected layer like so.
I'd like it so that each time the script is run it selects the previous layer/group and make it shown/visible like this.
But what currently happens with the script is that when it gets to a group is expands the folder and starts selecting layers inside the folder and turning on visibility.
Does anyone know how to modify the script (or use a different one) so that for groups it just turns the visibility on without expanding the group and selecting layers inside the groups?
Here's the current code:
// Select previous layer/group down & set to visible
var doc = app.activeDocument;
var cLayer = doc.activeLayer;
function activ (nLayer_f)
{
var check = nLayer_f.visible;
doc.activeLayer = nLayer_f;
if (check == false)
doc.activeLayer.visible = false;
}
var parL = doc.activeLayer.parent;
var parLln = parL.layers.length;
for(i=0; i<parLln;)
{
if(parL.layers==cLayer)
{
try
{
nLayer = cLayer.layers[0];
}
catch(e)
{
if(i!=parLln-1)
{
nLayer=parL.layers[i+1];
}
else
{
upmem = parL;
while (upmem!=doc && upmem.parent.layers[upmem.parent.layers.length-1]==upmem)
{
upmem = upmem.parent;
}
if (upmem==doc)
{
upmem=upmem.layers[0];
var lastmem = 1;
}
for(k=0;k<upmem.parent.layers.length;)
{
if(upmem.parent.layers
==upmem) {
aa=k;
if(lastmem==1)
{aa=-1;}
nLayer=upmem.parent.layers[aa+1];
k=upmem.parent.layers.length;
}
else {k++;}
}
}
}
activ (nLayer);
i=parLln-1;
}
i++;
}
// Show current layer
app.activeDocument.activeLayer.visible = true;
Change
try
{
nLayer = cLayer.layers[0];
}
to
try
{
throw(0)
nLayer = cLayer.layers[0];
}
Copy link to clipboard
Copied
len = (lrs = (aD = activeDocument).layers).length
for(i = 0; i < len; i++) {
if (lrs == aD.activeLayer) {
if (i != len - 1) {
lrs[i + 1].visible = true
}
else lrs[0].visible = true
function sTT(v) {return stringIDToTypeID(v)}
(ref = new ActionReference()).putEnumerated
(sTT('layer'), sTT('ordinal'), sTT('backwardEnum'));
(dsc = new ActionDescriptor()).putReference(sTT('null'), ref)
dsc.putBoolean(sTT('makeVisible'), false);
executeAction(sTT('select'), dsc); break
}
}
Copy link to clipboard
Copied
You can make your code even shorter.
sTT = stringIDToTypeID;
Copy link to clipboard
Copied
I tried the code on it's own but nothing seems to happen.
Should it be used on it's own, or added into the existing script? (Or replace just part of the existing script?)
Copy link to clipboard
Copied
Change
try
{
nLayer = cLayer.layers[0];
}
to
try
{
throw(0)
nLayer = cLayer.layers[0];
}
Copy link to clipboard
Copied
Thanks, that works!
It does expand the folders as it sets them as visible (a bit annoying but I can live with that), but doesn't move into the folders, which what I wanted
Copy link to clipboard
Copied
Expanded or not does not matter
Copy link to clipboard
Copied
Yes you are right, but I'm not sure it will always work as never tried. I'll see on next scripts, thx
Copy link to clipboard
Copied
The code of me doesn't expand folders and do the same
Copy link to clipboard
Copied
Strange, doesn't do anything for me
Copy link to clipboard
Copied
What doesn't do anything for you? Ah you probably mean of my code. Yes you are right, I forgot about Artwork folder
btw if you don't care subfolders are beeing expanded despite of that you asked in your original post you can use also this:
len = (lrs = (aD = activeDocument).layers).length
for(i = 0; i < len; i++) {
if (lrs == aD.activeLayer) {
if (i == len - 1) aD.activeLayer = lrs[0]
else {aD.activeLayer = lrs[i + 1]} break
}
}
Copy link to clipboard
Copied
I've tried both scripts and neither do anything for me.
Should I run them on their own, or add the code to my existing code?
Here's the full code, in case it helps to see what I'm doing:
// Set current layer as visible
app.activeDocument.activeLayer.visible = true;
// Save as PNG to ~/Desktop appending Layer Name
var doc = app.activeDocument;
var docBaseName = app.activeDocument.name.slice(0,-13);
var docDimensions = app.activeDocument.name.slice(-13,-4);
var layerName = app.activeDocument.activeLayer.name
var saveFile = new File("~/Desktop/" + docBaseName + "." + layerName + docDimensions + ".png");
pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.interlaced = false;
doc.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
// Hide current layer
app.activeDocument.activeLayer.visible = false;
// Select Previous Layer Down
var doc = app.activeDocument;
var cLayer = doc.activeLayer;
function activ (nLayer_f)
{
var check = nLayer_f.visible;
doc.activeLayer = nLayer_f;
if (check == false)
doc.activeLayer.visible = false;
}
var parL = doc.activeLayer.parent;
var parLln = parL.layers.length;
for(i=0; i<parLln;)
{
if(parL.layers==cLayer)
{
try
{
throw(0)
nLayer = cLayer.layers[0];
}
catch(e)
{
if(i!=parLln-1)
{
nLayer=parL.layers[i+1];
}
else
{
upmem = parL;
while (upmem!=doc && upmem.parent.layers[upmem.parent.layers.length-1]==upmem)
{
upmem = upmem.parent;
}
if (upmem==doc)
{
upmem=upmem.layers[0];
var lastmem = 1;
}
for(k=0;k<upmem.parent.layers.length;)
{
if(upmem.parent.layers
==upmem) {
aa=k;
if(lastmem==1)
{aa=-1;}
nLayer=upmem.parent.layers[aa+1];
k=upmem.parent.layers.length;
}
else {k++;}
}
}
}
activ (nLayer);
i=parLln-1;
}
i++;
}
Copy link to clipboard
Copied
Here's corrected code:
len = (lrs = (lyr = (aD = activeDocument).layers[0]).layers).length
for(i = 0; i < len; i++) {
if (lrs == aD.activeLayer) {
if (i == len - 1) aD.activeLayer = lrs[0]
else {aD.activeLayer = lrs[i + 1]} break
}
}
According to described scenario it works while you are inside of main group on some first level layer(Set).
btw first code that uses AM needs folders you are selecting (& making visible) by a script to be collapsed.
Answering your question they work independly so without a script you originally posted. But once again they were wrong as I forgot about 'Artwork' so if you want to use something instead on the top of this post you find correctly working script.
Copy link to clipboard
Copied
Ah, cool it works now
Copy link to clipboard
Copied
del
Find more inspiration, events, and resources on the new Adobe Community
Explore Now