Copy link to clipboard
Copied
Hello all
i need a script that will cycle only trough all layers contained in a group called "Colors" and stops once the last layer has been reached.
So far i do have a script that cycles trough ALL layers in the document, but i need it to cycle trough the defined Layerset/Group ONLY.
doc = app.activeDocument;
front = doc.layerSets.getByName("Front");
colors = front.layerSets.getByName("Colors");
cLayer = doc.activeLayer;
////////////////////////////////////////////////////////////////////////////////////
///////////////// FUNCTION TO HIDE ALL LAYERS IN LAYERSET "Colors" ////////////////
////////////////////////////////////////////////////////////////////////////////////
function resetColorsGroup() {
for (var i = colors.layers.length - 1; i >= 0; i--)
{
colors.layers.visible = false; // Hide all Layers
}
};
////////////////////////////////////////////////////////////////////////////////////
/////////////// FUNCTION TO SELECT THE NEXT LAYER EVEN IF NOT VISIBLE //////////////
////////////////////////////////////////////////////////////////////////////////////
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++;}
}
}
}
resetColorsGroup(); //Toggle visibility to OFF for all layers in the "Colors" layerset.
activ (nLayer);
i=parLln-1;
var currentLayer = activeDocument.activeLayer;
currentLayer.visible = !currentLayer.visible; // Make currently selected Layer Visible.
}
i++;
}
Thanks everyone, got stuck on this
Here's a modified script. It will first turn off the visibility of all the layers in the Colors layer set. Then it will cycle back through and turn each one on, you insert whatever code you want to save the file at that point, then it turns the visibility off on that layer, and moves to the next.
...#target photoshop
var doc = activeDocument;
var selectedGroup = doc.layers.getByName('Colors');
doc.activeLayer = selectedGroup;
var reset = true;
loopLayers (selectedGroup)//turn off visibility
reset = false;
Copy link to clipboard
Copied
Your script seems more complicated that what I assume you want to do. You need a recursive function to loop through all the layers in a layerset if it contains other layersets:
#target photoshop
var doc = activeDocument;
var selectedGroup = doc.layers.getByName('Colors');
doc.activeLayer = selectedGroup;
loopLayers (selectedGroup)
function loopLayers(gp){
for(var i=0;i<gp.layers.length;i++){
if(gp.layers.typename =='LayerSet'){
loopLayers (gp.layers);
}
else{
gp.layers.visible = false;
}
}
}
Copy link to clipboard
Copied
Hello Chuck (and others)
i've tried to get your script working for what i need. Unfortunately i could not (my fault). Tried so many things now but could not get it working and help is highly appreciated.
I'll try to explain again what i need with some pseudo code:
select First layer in Group "Colors" (i.E. "Green") and make only this Color/Layer visible.
{Play function "create"};
select next layer in Group "Colors" (i.E. "Yellow") and make only this Color/Layer visible.
{Play function "create"};
Repeat until all layers in Group "Colors" have been processed with function "create" (Amount and Names of layers in Group "Colors" is not consistent).
Copy link to clipboard
Copied
The Thing is that all layers in a docent can be processed. It is usually done using a recursive function that process the document object passed. If you passed the function a group instead of a document it would process the Group Object all the layers and other groups that may be in within a group. You could write a function to ignore groups in the object passes and just process the layers in the group, However all the layers in the lower groups ignored are logically in the group you processes where the visibility can be controlled by the lower groups and layers visibility settings.
Copy link to clipboard
Copied
So you need to be able to determine what the coloe code of each layer is, and run sime code accordingly?
Copy link to clipboard
Copied
https://forums.adobe.com/people/Chuck+Uebele schrieb
So you need to be able to determine what the coloe code of each layer is, and run sime code accordingly?
Thanks Chuck. Not really I try to explain again:
Currently i have the following code that will check for the currently visible layer in the "Colors" group and select it.
After each repetition it will select the next layer in the "Colors" group and makes only the newly selected visible.
Code has been put together from snippets and could be done easier and is not yet ready to work as it should.
stepTroughColors()
function stepTroughColors(){
var doc = activeDocument;
var front = doc.layerSets.getByName("Front");
var colors = front.layerSets.getByName("Colors");
var cLay = doc.activeLayer;
var layNum
var aLay = new Array()
//var i = [0]
for(var i=0;i<colors.layers.length;i++){
theLayer = colors.layers;
if(theLayer.visible == true){
doc.activeLayer = theLayer; // Sets the visible Layer in "Colors" as active Layer.
activeColor = theLayer.name; // Variable "activeColors" to use the Layername in the Save jpg actions.
}
aLay = colors.layers.name;
if(aLay==cLay.name){
layNum = i;
colors.layers.visible = false;
}
if(doc.activeLayer.name.indexOf("END") >= 0){
alert ("All Colors Have Been Processed and Saved!");
break;
}
};
try{
doc.activeLayer = colors.layers[layNum+1];
doc.activeLayer.visible = true;
alert (activeColor)
}
catch(e){}
};
Copy link to clipboard
Copied
Here's a modified script. It will first turn off the visibility of all the layers in the Colors layer set. Then it will cycle back through and turn each one on, you insert whatever code you want to save the file at that point, then it turns the visibility off on that layer, and moves to the next.
#target photoshop
var doc = activeDocument;
var selectedGroup = doc.layers.getByName('Colors');
doc.activeLayer = selectedGroup;
var reset = true;
loopLayers (selectedGroup)//turn off visibility
reset = false;
loopLayers (selectedGroup)//run loop again to run your inserted code
function loopLayers(gp){
for(var i=0;i<gp.layers.length;i++){
if(gp.layers.typename =='LayerSet'){
loopLayers (gp.layers);
}
else if(reset){
gp.layers.visible = false;
}
else{
gpLayers.visible = true;
//enter code here to save or whatever
gpLayers.visible = false;
}
}
}
Copy link to clipboard
Copied
https://forums.adobe.com/people/Chuck+Uebele schrieb
Here's a modified script. It will first turn off the visibility of all the layers in the Colors layer set. Then it will cycle back through and turn each one on, you insert whatever code you want to save the file at that point, then it turns the visibility off on that layer, and moves to the next.
Hello Chuck
thank you so much for your kind help! That did the trick, just had to correct 2 typos (also corrected below).
Have a great week and happy to get such a great support here in the forum!
Regards
Daniel
- #target photoshop
- var doc = activeDocument;
- var selectedGroup = doc.layers.getByName('Colors');
- doc.activeLayer = selectedGroup;
- var reset = true;
- loopLayers (selectedGroup)//turn off visibility
- reset = false;
- loopLayers (selectedGroup)//run loop again to run your inserted code
- function loopLayers(gp){
- for(var i=0;i<gp.layers.length;i++){
- if(gp.layers.typename =='LayerSet'){
- loopLayers (gp.layers);
- }
- else if(reset){
- gp.layers.visible = false;
- }
- else{
- gp.layers.visible = true;
- //enter code here to save or whatever
- gp.layers.visible = false;
- }
- }
- }
Copy link to clipboard
Copied
Я просто оставлю это здесь, может быть кому-то поможет:
1. Создайте группу switch
2. Разместите там слои которые требуется включать по очереди и сохранять (слои должны быть выключенными).
3. Создайте папку в C:/ с названием "wx"
4. Сохраните скрипт 'script.jsx'
5. File->Script->Browse выберите 'script.jsx'
Скрипт рекурсивно вызовет функцию, которая будет включать слой и сохранять результат в одноименный со слоем файл. Папка для сохранения указана в скрипте, это C:/wx
PS:
Отладка в Photoshop ужасная. Мне помогла утилита https://github.com/Trevor-/console
С помощью нее можно использовать консоль (тоже неудобно, но как есть)
Например $.writeln('C:\\wx\\'+gp.layers[i].name+'.jpg');
(протестировано под Windows 10 & Adobe Photoshop CC 2017)
#target photoshop
var doc = activeDocument;
var gp = doc.layers.getByName('switch');
doc.activeLayer = gp;
function saveJPEG( doc, saveFile, qty ) {
var saveOptions = new JPEGSaveOptions( );
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = qty;
doc.saveAs( saveFile, saveOptions, true );
}
function req( gp , qty) {
if(qty > 0){
var i = qty - 1;
gp.layers[i].visible=true;
saveJPEG( doc, new File('C:\\wx\\'+gp.layers[i].name+'.jpg'), 12 );
req(gp, qty - 1);
}
}
req(gp,gp.layers.length);