Photoshop Script to Save a Web Copy of Image

  • 1
  • Question
  • Updated 11 months ago
Hiya, 
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)

thanks all!
Photo of Bill Larkin

Bill Larkin

  • 4 Posts
  • 2 Reply Likes

Posted 11 months ago

  • 1
Photo of Max Johnson

Max Johnson, Champion

  • 488 Posts
  • 235 Reply Likes
You'll want to use the File and Folder javascript objects... this is totally untested, but should point you in the right direction:

var docFolder = Folder( doc.path  );
var newFolder = Folder( docFolder.parent + "/web");
Photo of Max Johnson

Max Johnson, Champion

  • 488 Posts
  • 235 Reply Likes
Do you have a way to see what the filename variable is coming out as? Are you running from the ExtendScript Toolkit?

I had a minute to hack at this in ESTK... found
1. You duplicated the doc so active doc was in unsaved state and had no path
2. You didn't use the newFolder variable after making it
3. You need to create the new folder before you can save to it...

I hacked in a chunk of code from one of my other projects and commented out the duplicate command. Seems to work now:

 //Duplicate doc
var doc = app.activeDocument
var fname = doc.name.split(".")
var fname = fname[0]
//var newDoc = doc.duplicate(fname) // This caused active document to be in an unsaved state and have no document path
//save part
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 docFolder = Folder( doc.path  );
var newFolder = Folder( docFolder.parent + '/' + "02 - web");
    // create folder if missing
    if (!newFolder.exists) {
        $.writeln("Creating new folder: " + newFolder.fsName)
        try {
            if( newFolder.create() === false )
            {
                alert( "error:copyDir:error creating directory "+newFolder.fsName);
            }
        } catch (e) {
            alert( "error:copyDir:error creating directory "+newFolder.fsName+":"+ e.message);
        }
    }

  var jpegOptions = new JPEGSaveOptions();
  jpegOptions.quality = 10;
  jpegOptions.embedColorProfile = true;
  jpegOptions.matte = MatteType.NONE;
  var filename = newFolder + '/' + basename + '.jpg';
    $.writeln(filename);
  thedoc.saveAs((new File(filename)), jpegOptions, true);
};
Photo of Bill Larkin

Bill Larkin

  • 4 Posts
  • 2 Reply Likes
thank you so much for the help, I see... that is even closer now, as it does work! 
is it just not possible to get it done just like this is now, but with the new doc (trying to leave the .psd open) - as it is, if the button was accidentally hit, it would save the web jpg, but close the .psd without saving. 

My real intention is to have the .psd open,
duplicate the doc,
convert it to srgb,
run my web sharpen action on it,
save in the that path, one up tree then web folder,
then close the dupe and the original .psd would be sitting there untouched. 

is that even possible?
(Edited)
Photo of Max Johnson

Max Johnson, Champion

  • 488 Posts
  • 235 Reply Likes
Yeah, it should be possible. You just need to get the document path before you duplicate. But if you only want to get the exported jpeg you might look at just reverting in history back to when it first opened.
Photo of Max Johnson

Max Johnson, Champion

  • 488 Posts
  • 235 Reply Likes
Alternately, you might find this Image Processor Pro script (a better version of the built in Image Processor) to do what you want already and more:
https://photoshopscaresme.com/new-version-russell-browns-image-processor-pro/
Photo of Bill Larkin

Bill Larkin

  • 4 Posts
  • 2 Reply Likes
ok, I moved the docpath line up and it now works with the duplicate thank you so very much. - I do need to make some other tweaks, but I should be able to get it working now.... thanks !!!!