Skip to main content
Known Participant
September 13, 2020
Question

Control adding photoshop layers with script?

  • September 13, 2020
  • 2 replies
  • 2689 views

I don't know exactly why the other post is hidden but adobe's tech support is getting tired of dealing with people abusing their mark spam system and is working on ways to fix that.

 

I'm trying to make a jsx script that allows me to add up to 20 layers at a time and any number of layers in between 0 and 20 and also take them away if I decrease the number before closing the script. I noticed that in Photoshop one is required to use the dialog class which could be part of the problem:

 

var myPanel = new Window("dialog","Slider Control",undefined,{resizeable: true});
var sliderText = myPanel.add("statictext",undefined,undefined);
var slider = myPanel.add ("slider", undefined, 0, 0, 20);
var buttonExit = myPanel.add("button",undefined,"Close Dialog Box");
slider.size = [80, 20]
sliderText.text = "Layers";
k = Math.round(slider.value);
newArray =[]
slider.onChange = function (){
    for (i=0; i<k; ++i){
        newArray.push(activeDocument.activeItem.layers.add);
}
}

 

This script should generate a window and slider but does not seem to add new layers. If there is an error in the code that prevents the window from loading, let me know and I will try to fix it. Any help would be appreciated!

This topic has been closed for replies.

2 replies

Legend
September 13, 2020
app.project.activeItem.layers.add
 
What is it in your opinion? Do you understand what you are writing?
 
Known Participant
September 13, 2020

This looks like command that adds a layer on top of whatever the active layer is in the application. Generally you would select a layer, then run the script so that it knows where to add a layer.

JJMack
Community Expert
Community Expert
September 14, 2020

newlayer = app.activeDocument.artLayers.add();

Will add a new layer above the current activeLayer.

JJMack
JJMack
Community Expert
Community Expert
September 13, 2020

I see no show you neen one for an onchange to work. Here is A script example I found  years ago you should find helpful.

 

var w = new Window( 'dialog', 'Slider-Dropdown Example' );
var settings = ['Low','Medium','High','Maxumum'];
w.qtyPnl = w.add( 'panel', undefined, 'Image Quality' );
w.qtyPnl.orientation = "row";
w.qtyPnl.alignment = "right";
w.qtyPnl.qtyMenu = w.qtyPnl.add("dropdownlist", undefined,  settings);
w.qtyPnl.qtyMenu.items[0].selected = true; 
w.qtyPnl.qtySlider = w.qtyPnl.add( "slider", undefined, 8, 0, 12 );
w.qtyPnl.qtyText = w.qtyPnl.add( "edittext", undefined, '8' );
w.qtyPnl.qtyText.preferredSize = [ 30, 20 ]
   // Add a panel for main dialog buttons
w.btnPnl = w.add( 'panel', undefined, 'Process' );
w.btnPnl.orientation = "row";
w.btnPnl.alignment = "center";
w.btnPnl.okBtn = w.btnPnl.add( 'button', undefined, 'Ok', { name:'ok' });
w.btnPnl.cancelBtn =w.btnPnl.add( 'button', undefined, 'Cancel', { name:'cancel' });
w.qtyPnl.qtyMenu.onChange = function() {
   var d = this.parent;
   switch (this.selection.index) {
      case 0:
         d.qtySlider.value = d.qtyText.text = 3;
         break;
      case 1:
         d.qtySlider.value = d.qtyText.text = 5;
         break;
      case 2:
         d.qtySlider.value = d.qtyText.text = 8;
         break;
      case 3:
         d.qtySlider.value = d.qtyText.text = 10;
         break;
   }
}

w.qtyPnl.qtySlider.onChanging = function() {
   var v = Math.floor(this.value);
   SetqtyDropDown(this.parent.qtyMenu, v);
   this.parent.qtyText.text = v;
}

w.qtyPnl.qtyText.onChanging = function() {
   var v = parseInt(this.text);
   if ( v >= 0 && v <= 1000) {
      SetqtyDropDown(this.parent.qtyMenu, v);
      this.parent.qtySlider.value = v;
   }
}

function SetqtyDropDown(m, v) {
   var i;
   if (v <= 3) {
      i = 0;
   } else if (v <= 5) {
      i = 1;
   } else if (v <=  {
      i = 2;
   } else if (v <= 10) {
      i = 3;
   } else {
      i = 3;
   }
   m.items[i].selected = true;
}

SetqtyDropDown(w.qtyPnl.qtyMenu, 8);
w.center();
w.show();

 

JJMack
Known Participant
September 13, 2020

I appreciate the suggestion, but there are a few problems.

 

One is that it says there is an error on line 56 with i = 2, I don't know why exactly.

 

Next, I don't know where you got that code from, so potentially neither you nor anyone else has the legal right to use that code anyway.


This script is a bit more complicated than what I had initially. Is there not a simpler way to modify the script in the original post up to a minimal functioning example?

 

JJMack
Community Expert
Community Expert
September 14, 2020

You script is a figment of your imagination.

JJMack