5 Replies Latest reply on Dec 15, 2017 2:41 PM by cvpstudios

    Photoshop Script to Save File One Folder up and in Another.

    cvpstudios Level 1


      I'm working on piecing together a script for Photoshop that will take my current open photo (high res .psd) and duplicate it (without 'copy' being appended) - which I have done... and I have it currently able to save the .jpg in the folder next to the source .psd I've been working on.


      What I'd really like is for the script to be able to look at the path where the .psd is, go up the tree one, then into the "web" folder and save the .jpg there.


      example folder structure for each session:



      so while working the .psd I want to assign an action with F key to run the script... and then the script will duplicate the document and save is in the "02 - web" folder.


      Currently I have it working, but only to save next to the original .psd and here's the code I have (modified from pieces of script found around the web)



      //Duplicate doc

      var doc = app.activeDocument

      var fname = doc.name.split(".")

      var fname = fname[0]

      var newDoc = doc.duplicate(fname)


      //save the jpg

      if (app.documents.length > 0) {

        var thedoc = app.activeDocument;

        var docName = thedoc.name;

        if (docName.indexOf(".") != -1) {

          var basename = docName.match(/(.*)\.[^\.]+$/)[1];

        } else {

          var basename = docName;


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

        try {

          var docPath = doc.path;

        } catch (e) {

          var docPath = "~/Desktop";


        var jpegOptions = new JPEGSaveOptions();

        jpegOptions.quality = 10;

        jpegOptions.embedColorProfile = true;

        jpegOptions.matte = MatteType.NONE;

        var filename = docPath + '/' + basename + '.jpg';

        thedoc.saveAs((new File(filename)), jpegOptions, true);




      any help with how to get it to go up the tree and into that folder would be greatly appreciated.


      Then after that, phase 2 would be to see if I could get it to use the "JpegMini Pro" plugin to save the .jpg - or at least "save for web" and not the straight "save as" - but jpegmini pro is preferred (that one might be hard, I don't know)


        • 1. Re: Photoshop Script to Save File One Folder up and in Another.
          r-bin Level 5

          It's difficult for me to understand the main thing, but can it help?


          var folder = new Folder("C:\\111\\222\\333\\444");
          var folder_up1 = new Folder(folder.path)
          var folder_up2 = new Folder(folder_up1.path);
          var folder_up1_XYZ = new Folder(folder.path + "/" + "XYZ")
          alert(folder.fsName + "\n" + folder_up1.fsName + "\n" + folder_up2.fsName + "\n" + folder_up1_XYZ.fsName)
          • 2. Re: Photoshop Script to Save File One Folder up and in Another.
            JJMack Most Valuable Participant

            To me it sounds like you actually do not want to duplicate you layered document.  You want to save a jpeg for your current document in a web folder where the web folder is in the same folder the has the folder your psd file is in.


            You should be able to script that.  You would first test to see if your layered document has been saved into a folder not the root on a drive.   If the layered document has not been saved you would abort the script with a message that  the document has not be saved yet.


            If there is a backing file you may also want to test if the Document has been changed since you opened it, and if it has, save the current changes into your PSD file.   Otherwise the jpeg you save may not match what is in your PSD file.   Once you find the PSD Path you can find that folder paren. Then test if there is a web folder in it and, if not create the web folder.  You can then use save a jpeg to save  the current document into your web folder.


            You layered document will still be open on Photoshop.    The Script could of course close it our of Photoshop.



            Save AS and Save for Web can save into you Web folder.  You can test the file size saved and resave lowering the jpeg image quality to get a smaller file size or scale down the image.    You would not need to use a plug-in the get the file size you need.


            What you posted seems to be more a service or desktop application not a Photoshop plug-in.

            JPEGmini | Technology


            Reading more on their web sit there now seems to be a plug-in for Lr and Ps.  Not free

            Fits Into Your Workflow

            JPEGmini Pro now includes a plug-in for Adobe® Lightroom® and a Photoshop® extension! When you export your photos directly from Lightroom or Photoshop, they will be automatically optimized by JPEGmini to the lowest file size possible without ever sacrificing quality.

            So your script could use the Plug-in if its use is scriptable

            • 3. Re: Photoshop Script to Save File One Folder up and in Another.
              Kukurykus Adobe Community Professional
              (jpg = new JPEGSaveOptions()).quality = 12, PTH = (pth = decodeURI((aD = activeDocument).fullName))
              .replace(/(\d - ).{3}/, function(_, v1, v2) {return ++pth[v2] + v1.slice(1) + 'web'}), aD.saveAs(File(PTH), jpg)


              Read this topic if your document wasn't saved yet: https://www.ps-scripts.com/viewtopic.php?f=66&t=24379

              • 4. Re: Photoshop Script to Save File One Folder up and in Another.
                Chuck Uebele Adobe Community Professional & MVP

                To get the upper level folder, you can use:


                #target photoshop
                var doc = activeDocument
                var parentF = new Folder(doc.path.parent)//one level up
                var parentF2 = new Folder(doc.path.parent.parent)//two levels up
                • 5. Re: Photoshop Script to Save File One Folder up and in Another.
                  cvpstudios Level 1

                  Thanks for the thoughts, I have since been able to get it working as I needed.


                  The duplicate doc was/is a very important part for me, as I want to be working on the psd, hit the hotkey and have a web jpg saved, but I may or may not be done with the psd yet. - and it was important for that web jpg to be the same filename (no "copy" appended)


                  I do know jpegmini is a program and it's also a PS extension, the terminology I was trying to use was, is jpegmini scriptable. which they (the developer) said it isn't currently.


                  I currently now have the script working to:


                  1. Get docpath
                  2. Duplicate original image
                  3. Flatten the new doc
                  4. Convert to SRGB 2.1
                  5. Resize the new doc (fit image style)
                  6. Apply my specific web sharpen settings for that size
                  7. Save up the tree one folder, then in the web folder. SO that it corresponds correctly with each client
                  8. Close the new (dupe)


                  Then I assign an action with a F key to run the script.


                  End result:

                  Retouching a .psd, hit one key and the web jpg is saved and I'm right back where I was without accidentally resizing or flattening my psd


                  Thanks all for the replies I had figured it out just after I wrote the original post.