10 Replies Latest reply on Aug 8, 2013 9:51 AM by bonalymac999

    Save All Open Files

    bonalymac999 Level 1

      Using CS3. I've created an action to Crop and straighten, and I'm using it on Batch options to split hundereds of scanned images.

       

      I managed to find two old scripts from c.pfaffenbichler (much appreciated, thanks) which I've managed to adapt a little into a single one, which save the open files. In essence it works perfectly, although it always saves it to the default location (in the original it was the desktop, I've amended it to a Folder on my images disk called Split).

       

      I'd be grateful if someone could help by explaining why the initial save to the source directory does not work.

       

      many thanks

       

      Colin

       

       

      // save all open documents;

      // 2011, use it at your own risk;

      #target photoshop;

      if (app.documents.length > 0) {

      var theFirst = app.activeDocument;

      var theDocs = app.documents;

      // psd options for unsaved files;

      psdOpts = new PhotoshopSaveOptions();

      psdOpts.embedColorProfile = true;

      psdOpts.alphaChannels = false;

      psdOpts.layers = true;

      psdOpts.spotColors = true;

      // go through all files;

      for (var m = 0; m < theDocs.length; m++) {

           var theDoc = theDocs[m];

           app.activeDocument = theDoc;

      // getting the name and location;

           var docName = theDoc.name;

      // thanks to xbytor for the regexp;

           if (docName.indexOf(&quot;.&quot;) != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}

           else {var basename = docName};

      // getting the location, if unsaved save to desktop;

                try {

                var docPath = theDoc.path;

      // jpg options;

      var jpegOptions = new JPEGSaveOptions();

      jpegOptions.quality = 10;

      jpegOptions.embedColorProfile = false;

      jpegOptions.matte = MatteType.NONE;

      // save the file if unsaved;

                if (theDoc.saved == false) {theDoc.save()}

                }

      // if file has not been saved yet save to E:\Split;

           catch (e) {

                var docPath = &quot;E:\Split&quot;;

                theDoc.saveAs((new File(docPath+'/'+basename+&quot;.jpg&quot;)), jpegOptions, false, Extension.LOWERCASE);

                };

           };

      app.activeDocument = theFirst;

      };

        • 1. Re: Save All Open Files
          c.pfaffenbichler Level 9

          I’m not sure I understand what you mean by »the initial save to the source directory«.

           

          Do you mean the line

            if (theDoc.saved == false) {theDoc.save()}

          does not work?

          That is only intended to apply to files that have been saved and therefore have a path already.

           

          The split files have no path yet and as far as I can tell no immediate connection to the files they were »cropped and straightened« from.

          But one could, I guess, deduce the file/s they originated from by their names and then use the path of the file that has the same name (save for the »copy« and numbers).

          • 2. Re: Save All Open Files
            Michael L Hale Level 5

            I think the problem is you are mixing how you spec the path, even on Windows I would use the following ( otherwise you end up with E:\Split/someDocName.jpg )

             

            var docPath = '/E/Split';

             

            If you really want to use the Windows style for the path do this instead

             

            var docPath = new Folder('E:\\Split');

             

            But the bigger problem may be the Split folder doesn't exists. I would add a line to make sure E:Split is an existing folder ( if it doesn't exists the save will fail )

             

            if(!docPath.exists) docPath.create();

             

            Finally, one of the save is in a catch block. Are you sure that the save in that block is ever called?

            1 person found this helpful
            • 3. Re: Save All Open Files
              Michael L Hale Level 5

              Thanks to c.pfaffenbichler I just noticed the line

               

              if(theDoc.saved==false) theDoc.save();

               

              That line will only save if the doc has been changed since it was opened. Could it be you want to save a doc that hasn't been changed like the doc you ran Crop and Straighten on?

              1 person found this helpful
              • 4. Re: Save All Open Files
                bonalymac999 Level 1

                Firstly thanks for the help. It is appreciated.

                 

                My apologies. I'm fogetting that they do not have a path yet.

                 

                My original thought had been to try and get the script to save the cropped/straightened fiiles into the same directory as the file they originated from. So no worries, it works as it is

                • 5. Re: Save All Open Files
                  bonalymac999 Level 1

                  Michael

                   

                  Firstly thanks for the help. It is appreciated.

                   

                  I have to say I do not know if the save is ever called, but the script as a whole seems to work.

                   

                  I will add the check you suggest on the existence of the folder. I had added it today, to make sure it exists. but the check will help.

                   

                  Thanks again

                  • 6. Re: Save All Open Files
                    c.pfaffenbichler Level 9

                    The only way I can think of to determine the »originator«-file and thus its path is the name-similarity.

                    I suppose one could run through app.files with a for-clause and compare the names.

                    • 7. Re: Save All Open Files
                      c.pfaffenbichler Level 9

                      You are a bit generous with your ratings of the posts.

                      I guess you are being polite, but I think the »correct« ranking is premature.

                      • 8. Re: Save All Open Files
                        Michael L Hale Level 5

                        c.pfaffenbichler wrote:

                         

                        The only way I can think of to determine the »originator«-file and thus its path is the name-similarity.

                        I suppose one could run through app.files with a for-clause and compare the names.

                        It's hard to tell without seeing the full script but if it is opening the files one at a time to run Crop and Straighten and saving the open documents created then app.documents[0] would be the 'source' file and you could base the new names off of that document name.

                        • 9. Re: Save All Open Files
                          c.pfaffenbichler Level 9

                          You are right; I had assumed that the Script is run after using »Crop&…« but that is not necessarily the case indeed.

                          • 10. Re: Save All Open Files
                            bonalymac999 Level 1

                            I am not a massive user of these forums, so I am not as well versed in the etiquette as regular users.

                             

                            But as far as I am concerned, you have/had solved the issue for me. The script already worked as it was posted, all I was trying to do was tinker with it. I'm a complete beginner - I think I've only written about three scripts in the last few years. I do like to learn when I can, and whilst I can't cope with starting from scratch, I find that I I find a similar script I can usually adapt it to suit my needs. That and Google and forums when stuck let me do the minimal amout of scripting I need. I have self-taught to learn to use vba for Office in the same way although I do use it more than scripting here.

                             

                            In this case I'd be wasting your and my time trying to go further. I am scanning in hundreds of old photos, and while it is time consuming, it is a one-off job, so I am happy that maybe I do have to do a bit of manual file movin before or after the action/script. I am able to run an action to trim my images to 99%. Follow that with this action which crops/straightens then invokes this script to save the documents. The net result is a huge time saving.

                             

                            So thanks very much for your input, you did really answer my question.

                             

                            Help from forums is often under-appreciated. I like to research first before asking questions (that's how I found these scripts from 2011!). I will never learn if all I do is rush to a forum with every question when I'm stuck. So when I get help, I do like to say thanks properly.

                             

                            The ability to get help from people like you is a great help to beginners like me, and really is appreciated.

                             

                            Colin