36 Replies Latest reply on Feb 5, 2010 12:26 PM by JoOosty

    automate save with adding a number

    JoOosty Level 1

      Hy all,

       

      this is the situation:

       

      I have got a psd file. I want this file to be saved automatically as Documentname1.png while it originally is Documentname.psd.

       

      The script doesn't have to ask what number i want to add, i can find that in the script and change that myself.

       

      It also have to save the file in another folder than where the Documentname.psd is saved.

       

      Could anyone creat a script for this action? I don't have any experience at scripting at all.

       

      Tnx!

        • 1. Re: automate save with adding a number
          Kasyan Servetsky Level 5

          if (app.documents.length > 0) {
              var myDoc = app.activeDocument;
              var myDocFullName = myDoc.fullName.fsName;
              if (myDoc.name.lastIndexOf('psd') > 0) {
                  var myNewPath = myDocFullName.replace( /.psd$/, '1.png' );
                  var myNewFile = new File(myNewPath);
                  myDoc.saveAs(myNewFile, SaveDocumentType.PNG, true, Extension.LOWERCASE);
              }
              else {
                  alert("The current document is not PSD-file.");
              }
          }
          else {
              alert("Please open a document and try again.");
          }

          1 person found this helpful
          • 2. Re: automate save with adding a number
            Kasyan Servetsky Level 5

            As an option, you can record your action with Action Manager and then make a few changes – much easier for a newbie-scripter:

             

            if (app.documents.length > 0) {
                var myDoc = app.activeDocument;
                var myDocFullName = myDoc.fullName.fsName;
                if (myDoc.name.lastIndexOf('psd') > 0) {
                    var myNewPath = myDocFullName.replace( /.psd$/, '1.png' );
                    var myNewFile = new File(myNewPath);
                    var id3 = charIDToTypeID( "save" );
                    var desc2 = new ActionDescriptor();
                    var id4 = charIDToTypeID( "As  " );
                    var desc3 = new ActionDescriptor();
                    var id5 = charIDToTypeID( "PGIT" );
                    var id6 = charIDToTypeID( "PGIT" );
                    var id7 = charIDToTypeID( "PGIN" );
                    desc3.putEnumerated( id5, id6, id7 );
                    var id8 = charIDToTypeID( "PNGf" );
                    var id9 = charIDToTypeID( "PNGf" );
                    var id10 = charIDToTypeID( "PGAd" );
                    desc3.putEnumerated( id8, id9, id10 );
                    var id11 = charIDToTypeID( "PNGF" );
                    desc2.putObject( id4, id11, desc3 );
                    var id12 = charIDToTypeID( "In  " );
                    desc2.putPath( id12, new File( myNewPath ) );
                    var id13 = charIDToTypeID( "Cpy " );
                    desc2.putBoolean( id13, true );
                    executeAction( id3, desc2, DialogModes.NO );
                }
                else {
                    alert("The current document is not PSD-file.");
                }
            }
            else {
                alert("Please open a document and try again.");
            }

            • 3. Re: automate save with adding a number
              JoOosty Level 1

              how can i change the fact that the new png is saved at the same place as the original psd file??

               

              But the script works great, only this little thing.

               

              I think your first script is easier.

               

              <offtopic> btw how did you learn javascript? I also want to learn it, so what is a good tutorial and where can you find all the names etc?? </offtopic>

              • 4. Re: automate save with adding a number
                Kasyan Servetsky Level 5

                Do you want the script to offer you to choose a folder for dng-files, or is this folder always the same – e.g. "Desktop/DNG files"?

                • 5. Re: automate save with adding a number
                  JoOosty Level 1

                  this folder is always the same, say that it is: C:\Documents and Settings\png

                   

                  Btw this does have to be changable in the script (so i can make 3 version of the script for 3 different folders).

                  • 6. Re: automate save with adding a number
                    Kasyan Servetsky Level 5
                    I also want to learn it, so what is a good tutorial and where can you find all the names etc??

                    Check out this site:

                    http://www.tranberry.com/photoshop/photoshop_scripting/index.html

                    Find PhotoshopScripting.pdf – a good tutorial for total begginer.

                     

                    Read also 'Photoshop CS3 Scripting Guide.pdf' – it's in your 'Adobe Photoshop CS3\Scripting Guide' folder.

                     

                    http://www.pstut.com/tutorial-photoshop-scripts-an-introduction.html

                    http://www.good-tutorials.com/tutorials/photoshop/scripting?page=1

                    • 7. Re: automate save with adding a number
                      Kasyan Servetsky Level 5
                      this folder is always the same, say that it is: C:\Documents and Settings\png

                      I think you meant your 'My Documents' folder, so here is a new version:

                       

                      if (app.documents.length > 0) {
                          var myDoc = app.activeDocument;
                          var myDocName = myDoc.name;
                          if (myDocName.lastIndexOf("psd") > 0) {
                              var myNewFolder = new Folder(Folder.myDocuments.fsName + "/png");
                              if (!myNewFolder.exists) myNewFolder.create();
                              var myNewName = myDocName.replace(/.psd$/, "1.png");
                              var myNewFile = new File(myNewFolder.fsName + "/" + myNewName);
                              myDoc.saveAs(myNewFile, SaveDocumentType.PNG, true, Extension.LOWERCASE);
                          }
                          else {
                              alert("The current document is not PSD-file.");
                          }
                      }
                      else {
                          alert("Please open a document and try again.");
                      }

                       

                      You can change Folder.myDocuments.fsName to whatever path you want:

                      e.g. "~/Desktop" or "/c/images"

                      • 8. Re: automate save with adding a number
                        JoOosty Level 1

                        so if i want the file to be saved in the folder C:\Documents\images\png then I should put C/Documents/images/png in stead of the Folder.myDocuments.fsName thing?

                         

                        i can't test it right now, but it is definetly looking good!

                        • 9. Re: automate save with adding a number
                          Kasyan Servetsky Level 5

                          var myNewFolder = new Folder("/C/Documents/images/png");

                          • 10. Re: automate save with adding a number
                            JoOosty Level 1

                            You are a genius!!! Thanks a lot!

                             

                            One little question? Is it possible to have a script, which asks you what name you want to save it, but in which the location is already determined.

                             

                            I mean: normally when you save something in PS, and afterwards you save something else, you start there when you want to save something new. If it's possible to write a script which asks you what name you want to save the psd file, and this script saves it a place you already determined, then the other script can be done afterwards to make it save in various folders.

                             

                            I hope you understand my story, but if this isn't possible, i'm already extremely happy with this script!

                            • 11. Re: automate save with adding a number
                              Kasyan Servetsky Level 5
                              Is it possible to have a script, which asks you what name you want to save it, but in which the location is already determined.

                              certainly

                              I hope you understand my story...

                              Not quite. Do you want this script to show a dialog at start, and then resave the active (psd) document to the predefined location, using a new name that you typed in the dialog, and finally, save a png-copy of the file to the same location?

                              • 12. Re: automate save with adding a number
                                JoOosty Level 1
                                Is it possible to have a script, which asks you what name you want to save it, but in which the location is already determined.

                                 

                                actually it's all about this sentence. This script is the first script to start with. I ask: "what's the name you want to save the file?" I give the name, and the script saves the file at a determined location as a psd (call the location /c/Documents/png for example).

                                 

                                This is what it's all about. This is the script to start with, so the file isn't already saved as a psd, this is the script which will take care of that. When this script is finished, i can run the script you made a few post ago (which works perfectly ) to save the psd file to a png file, so this one doesn't have to be included in this script, i just use the previous one.

                                 

                                it's all about the underlined sentences, the other sentences are just explenation.

                                • 13. Re: automate save with adding a number
                                  Kasyan Servetsky Level 5

                                  if (app.documents.length > 0) {
                                      var myDoc = app.activeDocument;
                                      var myDocName = myDoc.name;
                                      var myFolder = new Folder('~/Desktop/psd'); // change location to your own folder
                                      if (!myFolder.exists) myFolder.create();
                                      var myDialog = new Window('dialog', 'Save file as PSD');
                                      myDialog.size = {width:300, height:90};
                                      var myEditText = myDialog.add('edittext');
                                      myEditText.preferredSize = {width:200, height:20};
                                      var myNewName = myEditText.text;
                                      var myGroup = myDialog.add('group');
                                      myGroup.orientation = 'row';
                                      var myOkBtn = myGroup.add('button', undefined, 'Resave', {name:'ok'});
                                      var myCancelBtn = myGroup.add('button', undefined, 'Cancel', {name:'cancel'});
                                      if (myDialog.show() == 1) {
                                          var myNewName = myEditText.text;
                                          if (myNewName != "") {
                                              myNewName += '.psd';
                                              var myNewFile = new File(myFolder.fsName + "/" + myNewName);
                                              myDoc.saveAs(myNewFile, SaveDocumentType.PHOTOSHOP, true, Extension.LOWERCASE);
                                          }
                                      }
                                  }
                                  else {
                                      alert('Please open a document and try again.');
                                  }

                                   

                                  I am on Mac right now, so I can't test it on PC.

                                  1 person found this helpful
                                  • 14. Re: automate save with adding a number
                                    JoOosty Level 1

                                    This one is working with 1 file, so when i have got a file, run the script, enter the name, and voila, it's saved.

                                     

                                    The problem is: when i include this one in my action, i do can give it a name, but afterwards it goes wrong.

                                     

                                    Cause the script which you made before this one, doesn't work anymore by then. It gives an error like: Script error: the current document isn't a psd file.

                                     

                                    I'll try to describe what kind of an action i have:

                                     

                                    - i have got a picture which i'm gonna change with various actions.

                                    - after i've done that, i save the picture to a psd file (i didn't save the picture earlier in the process, this is the first save!).

                                    - therefor i need your latest script.

                                    - after that's done, the document is saved as a psd file, and i directly run the other script you made to save this file at different locations and with different additions. This is now all going automatically, which is fantastic, cause the name which the images are going to have, was already given by saving the psd file.  

                                     

                                    I hope this describes my process a bit better, and helps you to get this one out

                                    • 15. Re: automate save with adding a number
                                      Kasyan Servetsky Level 5

                                      if (app.documents.length > 0) {
                                          var myDoc = app.activeDocument;
                                          var myDocName = myDoc.name;
                                          var myFolder = new Folder('~/Desktop/psd'); // change location to your own folder
                                          if (!myFolder.exists) myFolder.create();
                                          var myDialog = new Window('dialog', 'Save file as PSD');
                                          myDialog.size = {width:300, height:90};
                                          var myEditText = myDialog.add('edittext');
                                          myEditText.preferredSize = {width:200, height:20};
                                          var myNewName = myEditText.text;
                                          var myGroup = myDialog.add('group');
                                          myGroup.orientation = 'row';
                                          var myOkBtn = myGroup.add('button', undefined, 'Save', {name:'ok'});
                                          var myCancelBtn = myGroup.add('button', undefined, 'Cancel', {name:'cancel'});
                                          if (myDialog.show() == 1) {
                                              var myNewName = myEditText.text;
                                              if (myNewName != "") {
                                                  myNewName += '.psd';
                                                  var myNewFile = new File(myFolder.fsName + "/" + myNewName);
                                                  var id51 = charIDToTypeID( "save" );
                                                  var desc8 = new ActionDescriptor();
                                                  var id52 = charIDToTypeID( "As  " );
                                                  var desc9 = new ActionDescriptor();
                                                  var id53 = stringIDToTypeID( "maximizeCompatibility" );
                                                  desc9.putBoolean( id53, true );
                                                  var id54 = charIDToTypeID( "Pht3" );
                                                  desc8.putObject( id52, id54, desc9 );
                                                  var id55 = charIDToTypeID( "In  " );
                                                  desc8.putPath( id55, new File( myNewFile ) );
                                                  var id56 = charIDToTypeID( "LwCs" );
                                                  desc8.putBoolean( id56, true );
                                                  executeAction( id51, desc8, DialogModes.NO );
                                              }
                                          }
                                      }
                                      else {
                                          alert('Please open a document and try again.');
                                      }

                                       

                                       

                                      • 16. Re: automate save with adding a number
                                        JoOosty Level 1

                                        It's working like a ferrari

                                         

                                        Thanks a lot for all the help, you guys really help newbie scripters out of their problems

                                         

                                        Greetz

                                        • 18. Re: automate save with adding a number
                                          JoOosty Level 1

                                          Is it also possible to have a script which deletes a file??

                                           

                                          I mean, in the beginning i create a script which saves a psd file, but actually i don't need that file after the process anymore.

                                          So could it be possible to run a script after the action, which deletes this file?? Call the folder in which the file is C:\Documents\Psd or something.

                                           

                                          Is it possible to make a script for this one??

                                           

                                          Greetz

                                          • 19. Re: automate save with adding a number
                                            Kasyan Servetsky Level 5

                                            Sure. Do you want delete the psd-file in the script that saves a png-file? Or do you want to do this in a separate, 3rd script?

                                            • 20. Re: automate save with adding a number
                                              JoOosty Level 1

                                              I would rather like it to be a 3rd script, so that the other scripts which are between these 1st and 3rd script, can run on the name of the psd file.

                                               

                                              It would be great!

                                              • 21. Re: automate save with adding a number
                                                Kasyan Servetsky Level 5

                                                Ok. I'll make it, but not tonight – I'm finishing the current issue of our magazine.

                                                • 22. Re: automate save with adding a number
                                                  JoOosty Level 1

                                                  ok, cool!

                                                   

                                                  I'll see it comming

                                                   

                                                  btw, what does i have to change on the save as psd document, to let it save as a png??

                                                  • 23. Re: automate save with adding a number
                                                    Kasyan Servetsky Level 5

                                                    Hi,

                                                     

                                                    I combined all the three scripts into one: it creates a floating panel with three buttons in Photoshop:

                                                    Save PSD – saves the active file in psd format to 'psd' folder on the desktop (if the folder doesn't exist, it will be created).

                                                    Save PNG – if the active files is in psd format, saves a copy in in png format to 'png' folder on the desktop (if the folder doesn't exist, it will be created).

                                                    Delete – deletes the last opened psd-file.

                                                    You can change the locations of the folders to what you need and change alignment of the buttons in the panel – read the comments.

                                                    Question: If you press ‘Delete’ button, but the document (last saved psd-file) is open in PS, what do you want to happen?

                                                    1.      An alert appears "Can't delete…"

                                                    2.      Close it silently and delete.

                                                    3.      Same as 2, but ask confirmation

                                                    The script has grown too big to post it here, so download it from my site:

                                                    http://www.kasyan.ho.com.ua/save_psd_png_delete.html

                                                     

                                                    Kasyan

                                                    • 24. Re: automate save with adding a number
                                                      JoOosty Level 1

                                                      It's really looking cool! but the thing is, this is all meant for an action, to do all the things a lot quicker.

                                                      So when i now have to press the right thing every time, isn't as handy as a script which does it automatical.

                                                       

                                                      So i have got a little question: could you also create a seperate delete script, cause when i try to cut i out of the big script, and run it, it gives the error: undifined is not an object :S and to answer your question: if i use the delete script, i want the file to be closed and deleted (no conformation).

                                                       

                                                      And could you also make something that when the save as png script runs, that when the new name already excists, the script will use (1).png in stead of .png??

                                                       

                                                      Thanks a lot!

                                                      • 25. Re: automate save with adding a number
                                                        Kasyan Servetsky Level 5

                                                        Ok, I’ll set myself to it tomorrow. it's easier to pull down than to build.

                                                         

                                                        And could you also make something that when the save as png script runs, that when the new name already excists, the script will use (1).png in stead of .png

                                                         

                                                        The first time you save it as png it will have the same base name as psd-file, the next time “1” will be appended to the end of the file name and every subsequent saving the number will be increment by 1:

                                                        e.g.

                                                        somefile.png

                                                        somefile1.png

                                                        somefile2.png

                                                        Right?

                                                        • 26. Re: automate save with adding a number
                                                          JoOosty Level 1

                                                          tnx a lot!! You are my hero in every case

                                                           

                                                          The first time you save it as png it will have the same base name as psd-file, the next time “1” will be appended to the end of the file name and every subsequent saving the number will be increment by 1:

                                                          e.g.

                                                          somefile.png

                                                          somefile1.png

                                                          somefile2.png

                                                           

                                                           

                                                          Right?

                                                           

                                                          No, i mean: The file always saves as the base name (from the psd-file). But when it saves it takes a look: does this name already excists?? If so it adds (1) behind the name, if not it just saves as the base name. And if (1) also excists, save it with (2) behind the name. (just until (3) much more it isn't going to be).

                                                           

                                                          Thanks in advance!

                                                          • 27. Re: automate save with adding a number
                                                            Kasyan Servetsky Level 5

                                                            I made three separate scripts, as you asked. And made  the cursor to appear blinking in the text field, so it is ready for typing right after the dialog appears (save psd script).

                                                            Here is the link for download:

                                                            http://www.kasyan.ho.com.ua/downloads/SavePsdPngDel3.zip

                                                            • 28. Re: automate save with adding a number
                                                              JoOosty Level 1

                                                              It's working PERFECTLY!!!!

                                                               

                                                              Thanks a lot, i didn't face any problems yet, but if so, i know where to find my rescueing angle

                                                               

                                                              Thanks for everything, this is really going to save me tons of time!

                                                               

                                                              I don't know how to thank you more, but you are really awesome!

                                                              • 29. Re: automate save with adding a number
                                                                JoOosty Level 1

                                                                Heyy, im working with your scripts for a little while now, and i have to say that it works PERFECT!

                                                                 

                                                                one little question: Is there a way to get it to work automatically with everyone??

                                                                 

                                                                Let me explain it: Now i set the script to save it on the desktop. That's ok. But when i give the action to someone else, they first have to record the scripts and the "open" actions again. This is because when i record the action, it records from the location: C:\Documents and Settings\Pluim\Desktop (Pluim is the name i gave the pc). But ofcourse this name is different with everybody. So how can i record an action in PS in such a way, that it will also directly work on somebody else his pc??

                                                                 

                                                                Tnx!

                                                                • 30. Re: automate save with adding a number
                                                                  Kasyan Servetsky Level 5

                                                                   

                                                                  So how can i record an action in PS in such a way, that it will also directly work on somebody else his pc??

                                                                   

                                                                  I would use X-Tools to convert the action into a script and then would change the recorded path -- C:\Documents and Settings\Pluim\Desktop -- to Folder ("~/Desktop")

                                                                  • 31. Re: automate save with adding a number
                                                                    JoOosty Level 1

                                                                    but do i have to download whole x-tools?? Cause there is such a lot i don't use in that program. Is there mayby another program which can do it (windows)?? And is it then also possible to convert it back to a photoshop action (atn file)??

                                                                     

                                                                    EDIT: never mind, found the right x-tools. it works good, but the weird thing is, that it should already work correct. But, can't test it myself, so i'll ask someone else.

                                                                     

                                                                    Tnx again!

                                                                    • 32. Re: automate save with adding a number
                                                                      Kasyan Servetsky Level 5

                                                                      I can't answer your questions right how because I have X-tools installed on my home computer, but I am at work.

                                                                      I am not sure, but I think that you can convert a script back into action.

                                                                      • 33. Re: automate save with adding a number
                                                                        xbytor2 Level 4

                                                                        I am not sure, but I think that you can convert a script back into action.

                                                                         

                                                                        With xtools you can convert actions/.atn files to XML or JavaScript.

                                                                        You can modify the translated XML (carefully) and convert it back to a .atn file.

                                                                        You cannot modify the translated JavaScript back to a .atn file.

                                                                         

                                                                        -X

                                                                        • 34. Re: automate save with adding a number
                                                                          Kasyan Servetsky Level 5

                                                                          Thank you for quick response, X. Now it's more clear.

                                                                           

                                                                          One more question:

                                                                          I created a new set and a new action in it, which opens a file from the desktop. The action looks like so:

                                                                          Open

                                                                          C:\Documents and Settings\Kasyan\Desktop\test.tif

                                                                           

                                                                          Then I run ActionFileToXML and opened the xml-file, where path key has value "~/Desktop/test.tif" -- the path is independent of current user name.

                                                                           

                                                                          Finally I run ActionFileFromXML and load the new set. The path is bound to the current user name again: C:\Documents and Settings\Kasyan\Desktop\test.tif

                                                                           

                                                                          Is it possible to modify the path in XML-file in a such way that it become 'user independent'? Or, is using ActiontoJavascript the only option in this case?

                                                                          • 36. Re: automate save with adding a number
                                                                            JoOosty Level 1

                                                                            Hi there,

                                                                             

                                                                            ive got a problem concerning the script:

                                                                             

                                                                            Somebody wants to use it, but faces a problem with the 'save as psd'-script. He got the following message:

                                                                             

                                                                            "

                                                                            Error 19: Bad argument add.

                                                                            Line: 12

                                                                            ->               var myGroup = myDialog.add('group');

                                                                            "

                                                                             

                                                                            At my sight (im using windows xp) the script is working perfectly. But at his sight (he uses vista) the script won't work and causes this error.

                                                                             

                                                                            Is it some kind of incompatibility with vista?? He's the first one complaining about this..

                                                                             

                                                                            Thanx in advance!

                                                                             

                                                                            JoOosty