9 Replies Latest reply on Jan 14, 2011 6:56 AM by georgeholmes

    Help in writing a script

    georgeholmes

      Heys guys,

      I'm practically a newb in scripting, so I'm relying on your help to put my problem into proper javascript.

       

      ————————————————————————————————————————————

      1) Open all jpg files in a specified folder;

      2) Create new doc

       

      I've been using a set of actions for the two next steps, though I'm not sure if they can be included in a script

      3a) Select previous document > image size > canvas size > select all > copy > close doc without saving > paste in new created doc > move

      3b) Select previous document > image size > canvas size > select all > copy > close doc without saving > paste in new created doc > move (the "move" has different pixel value than the previous one)

       

      4) Save as (variables for the file name like "01.jpg", "02.jpg" etc)

      5) Delete layer 1 & 2

       

      repeat 3a) and 3b) until there are no more images (until previous doc cannot be selected)

      ————————————————————————————————————————————

       

      Please, you also can refer me to any posts that actually are similar to my problem description. I read this thread (http://forums.adobe.com/thread/776366?tstart=0) about implying variables in saving jpgs, though I cannot quite get what part of code I need for my purposes.

       

      Thanks

        • 1. Re: Help in writing a script
          Michael L Hale Level 5

          I think we would need more details. What size do you want the new document in step 2? 3a and 3b have a move, which you want to be different for each step. What is the amout of the move for each step?

          • 2. Re: Help in writing a script
            georgeholmes Level 1

            Mike, thanks for the comment,

             

            the specified size of the doc is 590×440, the difference in the action is simply the "move" step after pasting (150 to left and 150 pixels to right after pasting — to achive this result http://cl.ly/1e3o3w1P1d3L1P2N2X3R)

             

            actually I've already dublicated it at http://ps-scripts.com/bb/viewtopic.php?f=9&t=3846&sid=82bbd6247e4cb7bb463f68a2da6f1fbc

            I don't know whether we should continue conversation here or relocate

            • 3. Re: Help in writing a script
              JJMack Most Valuable Participant

              http://forums.adobe.com/message/1115113#1115113 read the whole thread there are several scripts in it.

              1 person found this helpful
              • 4. Re: Help in writing a script
                c.pfaffenbichler Level 9

                You could try this:

                 

                 

                // combine jpgs from  a folder into images by twos;

                // identically named files are replaced without promting;

                // if selected files are  open they get closed without promting;

                // 2011, use it at your own risk;

                #target photoshop

                // select a folder;

                var theFolder = Folder.selectDialog ("select folder");

                if (theFolder) {

                // get the files;

                var theFolderFiles = retrieveJPGs (theFolder, []);

                // if more than one jpg is in the folder;

                if (theFolderFiles.length > 1) {

                // change pref;

                var originalRulerUnits = app.preferences.rulerUnits;

                app.preferences.rulerUnits = Units.PIXELS;

                // jpg options;

                var jpegOptions = new JPEGSaveOptions();

                jpegOptions.quality = 9;

                jpegOptions.embedColorProfile = true;

                jpegOptions.matte = MatteType.NONE;

                // do the operation;

                for (var m = 0; m < theFolderFiles.length / 2; m++) {

                // create file;

                     var myDocument = app.documents.add(new UnitValue(590, "px"), new UnitValue(440, "px"), 300, "new",

                     NewDocumentMode.RGB, DocumentFill.WHITE, 1, BitsPerChannelType.EIGHT, "sRGB IEC61966-2.1");

                // get first image;

                     var doc1 = scaleInto(myDocument, theFolderFiles[m * 2], 290, 440);

                     doc1.translate(UnitValue(doc1.bounds[0] * (-1), "px"), UnitValue(0, "px"));

                     var theName = File(theFolderFiles[m]).name.slice(0, -4);

                // get second image;

                     if (m * 2 + 1 < theFolderFiles.length) {

                          var doc2 = scaleInto(myDocument, theFolderFiles[m * 2 + 1], 290, 440);

                          doc2.translate(UnitValue(myDocument.width - doc1.bounds[3], "px"), UnitValue(0, "px"));

                          theName = theName.concat("_"+File(theFolderFiles[m * 2 + 1]).name.slice(0, -4));

                          };

                //save jpg as a copy:

                     myDocument.saveAs((new File(theFolder+"/mont_"+theName+".jpg")),jpegOptions,true);

                     myDocument.close(SaveOptions.DONOTSAVECHANGES)

                     };

                // reset;

                app.preferences.rulerUnits = originalRulerUnits;

                alert ("done")

                }

                };

                ////// scale //////

                function scaleInto (myDocument, myPath, theWidth, theHeight) {

                     var theDoc = app.open(File(myPath));

                // flatten;

                     theDoc.flatten();

                // convert to same space as container document;

                     theDoc.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, true);

                // scale;

                     if (theDoc.width / theDoc.height >= theWidth / theHeight) {

                          var theFactor = new UnitValue (theHeight * 100 / theDoc.height, "%");

                          }

                     else {

                          var theFactor = new UnitValue (theWidth * 100 / theDoc.width, "%");

                          };

                     theDoc.resizeImage(theFactor, theFactor, myDocument.resolution, ResampleMethod.BICUBICSHARPER);

                     theDoc.resizeCanvas(theWidth, theHeight, AnchorPosition.MIDDLECENTER);

                // copy layer to container document;

                     var theLayer = theDoc.layers[0].duplicate(myDocument, ElementPlacement.PLACEATBEGINNING);

                     theDoc.close(SaveOptions.DONOTSAVECHANGES);

                     return theLayer

                     };

                ////// get from subfolders //////

                function retrieveJPGs (theFolder, theFiles) {

                if (!theFiles) {var theFiles = []};

                var theContent = theFolder.getFiles();

                for (var n = 0; n < theContent.length; n++) {

                var theObject = theContent[n];

                if (theObject.constructor.name == "Folder") {

                theFiles = retrieveJPGs(theObject, theFiles)

                };

                if (theObject.name.slice(-4) == ".jpg" || theObject.name.slice(-4) == ".JPG") {

                theFiles = theFiles.concat(theObject)

                }

                };

                return theFiles

                };

                • 5. Re: Help in writing a script
                  georgeholmes Level 1

                  c.pfaffenbichler, thanks a lot!

                   

                  that gets me nearly as close to my goal as ever,

                   

                  the only problem remaining i guess is the function scaleInto — it successfully makes images into 290×440, but only by enlarging them (so they look like this http://cl.ly/1R2Z1X1t1O1R1z3N0B0A instead of this http://cl.ly/0r1H2C3O3e0w3H063T1i)

                   

                  normally i use action for this — image size height=400 > canvas size width=290 > — which I don't know yet how to put into javascript

                  • 6. Re: Help in writing a script
                    c.pfaffenbichler Level 9

                    So you want white space at the top and bottom?

                    • 7. Re: Help in writing a script
                      georgeholmes Level 1

                      i mean that the scaling function is scaling two images, that will go into one new doc eventually, too big — they don't fully fit in the new doc, so they are shown like upscaled and noisy parts of the originals.

                       

                      here's the psd with the result of two processed images (there are also included as originals in the folder) http://cl.ly/23022o3I0x250p2h0r2y

                      • 8. Re: Help in writing a script
                        c.pfaffenbichler Level 9

                        Dang … I forgot to compensate for the resolution of the files.

                        Could you change the lines 52 - 58 to the following?

                         

                        // scale;

                             if (theDoc.width / theDoc.height >= theWidth / theHeight) {

                                  var theFactor = new UnitValue (theHeight * 100 / theDoc.height * theDoc.resolution / 300, "%");

                                  }

                             else {

                                  var theFactor = new UnitValue (theWidth * 100 / theDoc.width* theDoc.resolution / 300, "%");

                                  };

                        1 person found this helpful
                        • 9. Re: Help in writing a script
                          georgeholmes Level 1

                          thank you so much, that works perfectly well!