Highlighted

Control adding photoshop layers with script?

New Here ,
Sep 13, 2020

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting, How to, Problem or error

Views

227

Likes

Translate

Translate

Report

Report
This conversation has been locked.

Control adding photoshop layers with script?

New Here ,
Sep 13, 2020

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting, How to, Problem or error

Views

228

Likes

Translate

Translate

Report

Report
Sep 13, 2020 0
Most Valuable Participant ,
Sep 13, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Sep 13, 2020 0
New Here ,
Sep 13, 2020

Copy link to clipboard

Copied

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?

 

Likes

Translate

Translate

Report

Report
Sep 13, 2020 0
Most Valuable Participant ,
Sep 13, 2020

Copy link to clipboard

Copied

You script is a figment of your imagination.

image.png

JJMack

Likes

Translate

Translate

Report

Report
Sep 13, 2020 0
New Here ,
Sep 13, 2020

Copy link to clipboard

Copied

That's a very unprofessional response and it does absolutely nothing to address the problems with the script you already brought up. I am already aware of the reference guide, that's how I learned what I have so far. It's entirely possible to use a script of similar simplicty to create layers in other software, you simply lack the expertise to execute it properly in PS.

Likes

Translate

Translate

Report

Report
Sep 13, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

A script can be very simple and add a layer. It just need to use a valid add artlayer statement.  You can you made up method. You can program functions.  There is no method or operating in Adobe DOM  activeItem.     and if you want to user a dialog your script would need to show a dialog.  

myPanel.show();

 

You need to read the manual I showed that there is no activeItem  there are items like .activeDocument and ActiveLayer etc. there is no  activeItem

 

image.png

 

JJMack

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

That's because I already tried activeDocument and it didn't work. And just to be sure I had tried it even again hours before you posted and it still didn't work.

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
Adobe Community Professional ,
Sep 13, 2020

Copy link to clipboard

Copied

app.project.activeItem.layers.add
 
What is it in your opinion? Do you understand what you are writing?
 

Likes

Translate

Translate

Report

Report
Sep 13, 2020 0
New Here ,
Sep 13, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Sep 13, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

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

Will add a new layer above the current activeLayer.

JJMack

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

Tried that already. There's lots of individual objects you can add if you literally only have those objects one time and nothing else. Layers, guides, pixels, paths, shapes, etc. But, none of them work in the slider-controlled for loop.

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

Still, it's a novel approach when one thing doesn't work to try making up new methods that aren't in the documentation, then to ask us why they don't work. I think we're finding this rather baffling.

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

It's not really novel at all, it works in AE.

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

And as I also said, activeDocument didn't work, that's why I resorted to commands I knew did work in other software, but those also don't work. 

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

Your slider controlled loop. Your script did not display a slider and in your slider controlled loop there is a  .activeItem that does not exist in Photoshop scripting. Your loop can not be executed for there no slider that can be changed to trigger the loops execution so nothing will be pushed into your newarray.   

 

You should use there other software where what you wrote works.  What you wrote does not work in Photoshop and you do not seem to want to learn what does.

 

If activeDocument did not work there would be no Photoshop. For the most features in Photoshop require an active document.

JJMack

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

You literally did not suggest anything that would make this work, you've been entirely unhelpful. The script you posted does't work and you failed to explain even the most basic properties of it even if it had worked.

 

It does display a slider, I tested it. What it doesn't do is register adding layers. This is true whether I use activeDocument or activeItem. You have not proposed any functioning solutions.

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

I did suggested the you show you dialog you will your slider you willl be able to change the slider but that it. You did not seem to want to try  

myPanel.show();

 

Your slider look like this image.pngyou can move the sliderimage.png

but that about all the Close Dialod Box button does not function there no code for it. You can close the dialog wit Alt+F4

 

In fact your loop never even tries to execute the bad statment for n=0.

 

 

 

 

 

 

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 = [100, 10]
sliderText.text = "Points";
n = Math.round(slider.value);
newArray =[]
slider.onChange = function (){
	alert("onChange and n is " + n );
	var count = "";
    for (i=0; i<n; ++i){
		count = coint + i + "\n";
        newArray.push(app.project.activeItem.layers.add);
	}
	alert(count); 
}
myPanel.show();

 

 

 

 

 

I also gave you an exanple of a working slider dialog but you rejecteced as to much.  I also gave you a statement to add a layer  you state it does not.  I may not know JavaScript However, my Photoshop scripts work.

 

Where did you dig up .project  app.project??? Ther is am .activeDocumebt first thing

Capture.jpg

 

JJMack

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

You didn't give a working example of anything. I copied and pasted it and it didn't work. Twice I pointed that out to you that it doesn't work and tiwce you ignored me, that's your fault. I'm not going to address what you ask until you make the most basic effort to address what I broght up. The reason my script displays is because I already have show in my jsx file and an if statement. Those things work fine, what doesn't work is the for loop. As I already stated, activeDocument doesn't fix this. Getting rid of the array and push doesn't fix this. Adding an extra index for artLayers[i] doesn't change this.

 

 

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

You are right I am wrong so be it. Good luck...

JJMack

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

You literally ignored my legal point, so even if you were professional enough to communicate an answer, it would have potentially caused legal problems for you, Adobe and anyone else. But of course you didn't care about that becase you ever actually read what I wrote in the first place.

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

Yes I'm not a lawyer I can can not understand professional legal writing.  I do not know  javascript  still I can see your javascript is code you made up not actual javascript not Adobe DOM objects, methods or Photoshop operations.  I did read you make belief code.  It not correct Photoshop scripting. That is what user are trying to tell you. R-bin is an expert Photoshop scripted he has come to me rescue many times.  We were trying to help you now we are giving up. Please run this script and tell me once again acriveDicument does not work.

var doc = app.documents.add(1001, 1001, 300);
var numLayers = 10;
for (i=0; i<numLayers; ++i){
		new_layer = app.activeDocument.artLayers.add(); // Add a Layer
		new_layer.name = i;	// Name Layer
}

 

JJMack

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

Why did you even comment here acting like you know everyhing when you know neither javascript nor extended script specific to Adobe's software?? You lied about being a professional, you were unqualified to address this question to begin with and you most certainly never tried to help, all you died was lie about being a professional as an excuse to pat yourself on the back for nothing.

 

If you're not a laywer, then that's why you need to check with Adobe before posting. I didn't ask you to go to law school, I implied you need to check the terms of use of your code or check whether your contract with Adobe requires you give up copywrite ownership of publicly published content. I could have potentially done that myself, but you failed to specify where you got it from.

All of these result from a complete failure in your communication, you are not a professional.

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

Because activeDocument does work. You script does not work. Your loop push statement is not executed. I'm retired I'm no longer a professional I never claimed I was.  You seem to the expert. Why do my scripts work and your do not?

 

 

var doc = app.documents.add(1001, 1001, 300);
var numLayers = 10;
for (i=0; i<numLayers; ++i){
		new_layer = app.activeDocument.artLayers.add(); // Add a Layer
		new_layer.name = i;	// Name Layer
}

 

 

JJMack

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

I didn't say active Document works nowhere, I said changing it to activeDocument doesn't fix my script specifically, which is correct. You very transparently lack the necessary communication skills to be a professional.

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

You script is junk it does not work, I added alerts  to show you your loop push step is never executed for n is 0. You script will never add a layer it does not contain code the will add a layer.

for (i=0; i<n; ++i){junk}

JJMack

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

Dude, the fact that it's junk is exactly why I asked on how to fix it. I don't need help with the show command nor the slider being 0, I didn't ask about those, I asked about getting the slider to properly interpret the for-loop values. You do not understand how professional support works. Changing it to activeDocument doesn't solve it because it also fails for other types of objects. It could be a fragment of a possible solution, but it alone is not at all a solution.

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
Most Valuable Participant ,
Sep 14, 2020

Copy link to clipboard

Copied

Paul use to help here. He was perhaps the best scripter that ever participated here. I post his sample script for a script with  working slider in its dialog. For one to user to set a value and when the were satisfied they could use a run button for the script to execute the function the slider was set for then after  the function was performed the script would terminate. The was also a cancel button in case the user changed their mind and did not want to execute the function. 

 

You never detailed how you want  you script to function there was no code to display the dialog.  or to terminate the script, no code for the dialog's exit button.   There is no code to add a layer.  No test to see if the is an active document.  Perhaps scripting is too much for you. You ask questions but do not seem the understand  the replies you receive.  Because we are unprofessional user with no communication skill. You the judge the that does not want to communicate.  You have received no good advice. Some have tried. I know I do not have the best communication skills but over the years I have help many users. You are an exception I'll note that down.  Good night  it past my bed time.

JJMack

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0
New Here ,
Sep 14, 2020

Copy link to clipboard

Copied

You keep saying "we" as a way to use your coworkers as a human shield to mask your own incompitence.

 

My problem isn't with Adobe, it's with you; *you* lack the communication skills and technical skills to be qualified in handling this question. You admitted you didn't know javascript and Adobe's extended script and you admitted that r-bin was the one secretly doing your job for you in the instances you got credit for scripting solutions and even yet again you now claim you took another person's code. You couldn't explain Paul's code after I brought up that problem because you don't understand it yourself. You don't unerstand it yourself because you are not qualified to handle this question.You have no basis whatsoever to tell anyone whether they can handle any kind of scripting, anything more is nothing less than the epitome of hypocricy.

 

Pual's script as you claim it is, did not work for me. It has an error on line 56. Since my scripting is "junk," I can't do much more than point that out to you, but you refused to listen.

 

I was assured by Adobe's customer support that the person who answers this question will be an expert of the required expertise. An expert wouldn't have trouble adding "myPanel.show();" nor would they have trouble adding

"if (myWindow !== null && myWindow instanceof Window)
{

myWindow.center();
myWindow.show();
}"

or an equivalent statement. Since those codes work for me, I have no questions about them, the main issue is the structure of the for-loop itself for which several variables and conditions need to be defined. As I repeatedly said, using "app.activeDocument.artLayers.add();" does not fix the loop-to-slider issue, but you refused to listen over and over and over and over and over.

 

 

 

 

Likes

Translate

Translate

Report

Report
Sep 14, 2020 0