16 Replies Latest reply on Jul 1, 2015 10:53 AM by cromaline

    psdom

    StrongBeaver Level 3

      I'm beginning in scripting using ps-dom.  What I want is to create a new document, that is 120x200, followed by adding another layer to the current document. The following code isn't working ?

       

      var psdoc = app.documents.add(120,200)

      psdoc.activeDocuments.layers.add()

        • 1. Re: psdom
          JJMack Most Valuable Participant

          Perhaps this may work

           

          var startRulerUnits = app.preferences.rulerUnits; // save users current unit setting

          app.preferences.rulerUnits = Units.PIXELS; // tell ps to work with pixels

          var psdoc = app.documents.add(120,200, 72); // open new document width height res default the rest

          newLayer = psdoc.artLayers.add(); // add a new art layer var newLayer

          newLayer.name = "New Layer"; // set the new layer name to "New Layer"

          • 2. Re: psdom
            matias.kiviniemi Level 3

            I've noticed that creating a document with high resolution in Javascript can fail even if you can create same doc from the menu. I think I tried to create a 4096x4096@300PPI and had to drop to 72 PPI.

            • 3. Re: psdom
              StrongBeaver Level 3

              I didn't declare a resolution for the new document, arghh.

              • 4. Re: psdom
                StrongBeaver Level 3

                Creating a 300dpi with JS will cause problems, thanks for the tip !

                • 5. Re: psdom
                  StrongBeaver Level 3

                  Must I set the units and the ruler units, can't it go by whatever defaults are set for Ps ?

                   

                  I declared the resolution for the variable psdoc and still get an error on this line;

                   

                  var psdoc = app.documents.add(120,200,72)

                  • 6. Re: psdom
                    JJMack Most Valuable Participant

                    If you copy and paste the code I posted into notepad and save it as psdom.jsx and then drop it on photoshop. You will see this.  I can not type do you think I post extra line of code for fun? In fact I left left out a line at the end thought you may figure it out.

                     

                    app.preferences.rulerUnits = startRulerUnits; //restore users units

                     

                    In my case the rules would be back in inches not displayed in pixels.  Woild be 1.66" wide by 2.77" high.

                    Capture.jpg

                    Capture.jpg

                    Capture.jpg

                    1 person found this helpful
                    • 7. Re: psdom
                      Chuck Uebele Adobe Community Professional & MVP

                      JJMacks code is the right way to do it using the DOM. As mentioned, you didn't add the resolution in your example. Also, this line it totally wrong:

                       

                      psdoc.activeDocuments.layers.add()

                       

                      psdoc is your document that you just created. It doesn't have any activeDocuments. You can make it an active document by stating:

                       

                      psdoc = app.activeDocument.

                       

                      As JJMack all ready mentioned, the way to add a layer is artLayers.Add() rather than just layers.add().

                      1 person found this helpful
                      • 8. Re: psdom
                        StrongBeaver Level 3

                        I have to declare the size as a string along with the measurement units.  I didn't have to declare the rulerUnits as I originally though in the beginning as JJMack did in his first & second screen capture, that is what I wanted to know and I got my answer

                        • 9. Re: psdom
                          StrongBeaver Level 3

                          var psdoc = app.documents.add("120px","200px",72);

                          newLayer = psdoc.artLayers.add();

                          newLayer.name = "NewName";

                          Have patience, if you don't well please don't reply

                          I know the syntax for creating an Array, not sure how to create an array of artLayers with each artLayer having their own name, when the script is executed X number of Layers are created with the respectful names ?

                          var LayerList = ["one","two","three","four","five"]

                          • 10. Re: psdom
                            JJMack Most Valuable Participant

                            If you would use your eyes and read and the help we try to give you. You would not have ask questions you have been given a solution for.

                             

                            If  you do not set the units,  You will not know what size document would be created if a document was added. For if the user had set units to percent and you did not set the units there would be a scripting error because a passing was percentage would be and invalid argument for the add document statement.

                             

                            In my example code I posted for you I also showed the correct way to add a layer and then to name that layer added. The code was commented  and I even posted screen capture showing the result of running the code.  I pointed out I omitted a line of code to restote the users ruler units therefore the rulers unites were shown in pixels in the screen capture.  I then added the missing line of code.  Fixed up my Photoshop Preferences setting ruler units to inches which Is my normal setting.  I then ran the Script again and this time the ruler units were shown in inches not pixels as the second screen capture showed.

                             

                            You seem to want to tell us that we were wrong that you did not have to set units.  That is true you do have to but if you do not the add document statement may generate a scripting error which your code would not catch so the script would fail with an error.  The way you would code your statement to catch your error would be like this.

                             

                            try {var psdoc = app.documents.add(120,200);}

                            catch(e) { alert(e);}

                             

                            You would need to loop through your array adding a layer each iteration and name the layer added each time you add the new layer.

                            artLayer.add() add a single layer not an array of layers. Look at page 65 in Photoshop CC javascripting guide.

                            • 11. Re: psdom
                              StrongBeaver Level 3

                              If you would use your eyes and read and the help we try to give you. You would not have ask questions you have been given a solution for.

                              Yes, but I do use my eyes, what else can I use to read that I'm unaware of.

                               

                              If  you do not set the units,  You will not know what size document would be created if a document was added. For if the user had set units to percent and you did not set the units there would be a scripting error because a passing was percentage would be and invalid argument for the add document statement.

                              The point was not whether or not the units were set, I did not care about that, the point was strictly to get the script to run, period. 

                              • 12. Re: psdom
                                StrongBeaver Level 3

                                Leave things as is, I will look though the CC Javscript guide, ok !

                                • 13. Re: psdom
                                  JJMack Most Valuable Participant

                                  Yes that is a problem. The point is you should care but you do not care.  You should want to learn but most of the time you do not seem willing to learn.  Users run out of  patience with you.  You do not seem to grasp what is important.  We try to help but you do not care for our help.  That is very disrespectful to say the least.

                                  Capture.jpg

                                   

                                  • 14. Re: psdom
                                    StrongBeaver Level 3

                                    I simply didn't care because wanted to understand the bare minimum, how it was perceived, to each his own. If it's disrespectful, well then it's going to have to be taken as that.

                                    • 15. Re: psdom
                                      JJMack Most Valuable Participant

                                      Since  you want to persist with your attitude I will put you on my ignore list like I'm sure many other have done. I the past I resisted doing that. Now I resign will no longer waste time reading what you write.

                                      • 16. Re: psdom
                                        cromaline Level 3

                                        Hi StrongBeaver

                                        as the topic for which you opened this thread has received the right answer you should flag it as "answered".