save dialog with JPG save options specified

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

essejesse

save dialog with JPG save options specified

Post by essejesse »

I've gotten a lot of help here recently and first want to just say this place is awesome, and thanks.

Last week, I wrote a script that sized an image three different times and saved it in three different locations, but in this order:
size
save as
size
save as
size
save as

Everything was working great, but then I was modifying it for a different process and tested on an already flattened document. Instead of getting the save as dialog with my options specified, it just saved to the folder I had wanted to prompt the user to and moved on. That's when I realized I had tested my other script with a document that had layers the whole time.

Is there any way to force the save as dialog to show up so I can choose subfolders no matter what?

Here's the code
Code: Select all//saving original units and setting ruler units to pixels
var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.INCHES

//set active document
var myDoc = app.activeDocument

//JPG save options
jpgSaveOptions = new JPEGSaveOptions()
jpgSaveOptions.embedColorProfile = true
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE
jpgSaveOptions.matte = MatteType.WHITE
jpgSaveOptions.quality = 12

//initial resize
myDoc.resizeImage (undefined, undefined, 300, ResampleMethod.NONE)

do
{
//first save (300 DPI JPG) using variable to prompt proper folder selection
var myHiResPrompt = Folder ("my first network location")
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE)
}
//loop confirm
while(confirm ("Do you need to save to another folder at this resolution?"))

//second resize
if(myDoc.width > myDoc.height) {
    myDoc.resizeImage (5, undefined, 150, ResampleMethod.BICUBICSHARPER)
    }
else{
    myDoc.resizeImage (undefined, 5, 150, ResampleMethod.BICUBICSHARPER)
    }

//second save (150 DPI JPG) using variable to prompt proper folder selection
var myMidResPrompt = Folder ("my second network location")
myDoc.saveAs (new File (myMidResPrompt), jpgSaveOptions, false, Extension.LOWERCASE)

//third resize
if(myDoc.width > myDoc.height) {
    myDoc.resizeImage (4, undefined, 72, ResampleMethod.BICUBICSHARPER)
    }
    else {
    myDoc.resizeImage (undefined, 4, 72, ResampleMethod.BICUBICSHARPER)
    }

//third save (72 DPI PNG) using variable to prompt proper folder selection
var myLowResPrompt = Folder ("my third network location")
myDoc.saveAs (new File (myLowResPrompt), PNGSaveOptions, false, Extension.LOWERCASE)

//resetting original units
app.preferences.rulerUnits = originalUnit



I won't name names, but one of my coworkers flattens documents that we don't want flattened and she is going to run into this problem a lot.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

essejesse

save dialog with JPG save options specified

Post by essejesse »

I figured out that if I make sure the script has this in it, the dialog is triggered, and it doesn't really effect the document negatively.

Code: Select all//turn background into layer
myDoc.activeLayer = myDoc.layers[myDoc.layers.length-1]
myDoc.activeLayer.isBackgroundLayer = false
pfaffenbichler

save dialog with JPG save options specified

Post by pfaffenbichler »

Do you really scale the image three times in a row?
Image-integrity-wise it should make sense to undo the transformations before applying another one.
essejesse

save dialog with JPG save options specified

Post by essejesse »

Yes, it goes from native camera resolution (240 ppi) to 300 ppi without resampling, saves a JPG, reduces to 150 ppi resampling to 5 inches on the longer side, saves a JPG, resizes to 72 ppi resampling to 4 inches on the longer side, then saves as a PNG.

I haven't noticed any significant degradation of the image quality as it's always reducing the size, and resampling so much smaller than the previous resize. If they were closer in size, I could undo, but do you think it's still worth it considering the original is typically 5616 x 3744 @ 240, the 150 is 750 x 500, and the 72 is 288 x 192?
essejesse

save dialog with JPG save options specified

Post by essejesse »

An unintended consequence is that if the last layer is already not a background layer, the layer becomes visible no matter what. I need to address that because we often have one hidden white layer so we can compare the image we are working on to what it will look like on paper, or whatever.

The reason this is important is that on the final save, it's supposed to be a PNG with transparency, so if that white layer shows up, it ruins the final save.

I think if I can make the background layer part an if statement, it might work. I'll post any results I get.
essejesse

save dialog with JPG save options specified

Post by essejesse »

The end result was that I identified if the last layer is visible, and if so, ask if the background needs to be removed. This takes care of any random images that don't meet the exact criteria we have.

Here's the full script
Code: Select all//saving original units and setting ruler units to pixels
var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.INCHES

//set active document
var myDoc = app.activeDocument

/////////////////////////////////////////////////////

//RGB
doAction("RGB – With Merge","Jesse's Actions")//Just a quick check to make sure the image is in RGB, as some of the oldest ones we have are in CMYK

/////////////////////////////////////////////////////

//set myLayer variable to last layer
var myLayer = myDoc.layers[myDoc.layers.length-1]
    if (myLayer.visible) {
    //turn background into layer, if necessary
    var whiteBackground = confirm ("Do you need to remove the white background for PNG transparency?")
    //if so, ask which path to use
        if (whiteBackground) {
        myDoc.activeLayer = myLayer
        myLayer.isBackgroundLayer = false
        var dlg = new Window('dialog', "Which path will remove the white background?", [500,300,840,525]);
        dlg.pathList = dlg.add('listbox', [15,15,325,137], 'field', {multiselect: false});
        // populate the list with the open files’ names;
        for (var o = 0; o < myDoc.pathItems.length; o++) {
        dlg.pathList.add ("item", myDoc.pathItems[o].name);
        };
        dlg.pathList.items[0].selected = true;
        // buttons for ok, and cancel;
        dlg.buttons = dlg.add('panel', [15,152,325,210], '');
        dlg.buttons.buildBtn = dlg.buttons.add('button', [160,14,291,39], 'OK', {name:'ok'});
        dlg.buttons.cancelBtn = dlg.buttons.add('button', [14,14,145,39], 'Cancel', {name:'cancel'});
        // show the dialog;
        dlg.center();
        var myReturn = dlg.show ();
            if (myReturn == true) {
            myDoc.pathItems.getByName(dlg.pathList.selection).makeSelection ()
            myDoc.selection.invert()
            myDoc.selection.clear()
            }   
        }
    }

//JPG save options
jpgSaveOptions = new JPEGSaveOptions()
jpgSaveOptions.embedColorProfile = true
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE
jpgSaveOptions.matte = MatteType.WHITE
jpgSaveOptions.quality = 12

/////////////////////////////////////////////////////

//initial resize
myDoc.resizeImage (undefined, undefined, 300, ResampleMethod.NONE)

//first save (300 DPI JPG) using variable to prompt proper folder selection
var myHiResPrompt = Folder("/Volumes/network save location")
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE)

/////////////////////////////////////////////////////

//second resize
if(myDoc.width > myDoc.height) {
    myDoc.resizeImage (5, undefined, 150, ResampleMethod.BICUBICSHARPER)
    }
else{
    myDoc.resizeImage (undefined, 5, 150, ResampleMethod.BICUBICSHARPER)
    }

//second save (150 DPI JPG) using variable to prompt proper folder selection
var myMidResPrompt = Folder ("/Volumes/network save location")
myDoc.saveAs (new File (myMidResPrompt), jpgSaveOptions, false, Extension.LOWERCASE)

/////////////////////////////////////////////////////

//third resize
if(myDoc.width > myDoc.height) {
    myDoc.resizeImage (4, undefined, 72, ResampleMethod.BICUBICSHARPER)
    }
    else {
    myDoc.resizeImage (undefined, 4, 72, ResampleMethod.BICUBICSHARPER)
    }

//third save (72 DPI PNG) using variable to prompt proper folder selection
var myLowResPrompt = Folder ("/Volumes/network save location")
myDoc.saveAs (new File (myLowResPrompt), PNGSaveOptions, false, Extension.LOWERCASE)

//resetting original units
app.preferences.rulerUnits = originalUnit



After some more testing, I'll be distributing it among my coworkers and will probably throw in a close w/o saving command at the end.
Owl

save dialog with JPG save options specified

Post by Owl »

essejesse wrote:The end result was that I identified if the last layer is visible, and if so, ask if the background needs to be removed. This takes care of any random images that don't meet the exact criteria we have.

Here's the full script
Code: Select all//saving original units and setting ruler units to pixels
var originalUnit = preferences.rulerUnits
preferences.rulerUnits = Units.INCHES

//set active document
var myDoc = app.activeDocument

/////////////////////////////////////////////////////

//RGB
doAction("RGB – With Merge","Jesse's Actions")//Just a quick check to make sure the image is in RGB, as some of the oldest ones we have are in CMYK

/////////////////////////////////////////////////////

//set myLayer variable to last layer
var myLayer = myDoc.layers[myDoc.layers.length-1]
    if (myLayer.visible) {
    //turn background into layer, if necessary
    var whiteBackground = confirm ("Do you need to remove the white background for PNG transparency?")
    //if so, ask which path to use
        if (whiteBackground) {
        myDoc.activeLayer = myLayer
        myLayer.isBackgroundLayer = false
        var dlg = new Window('dialog', "Which path will remove the white background?", [500,300,840,525]);
        dlg.pathList = dlg.add('listbox', [15,15,325,137], 'field', {multiselect: false});
        // populate the list with the open files’ names;
        for (var o = 0; o < myDoc.pathItems.length; o++) {
        dlg.pathList.add ("item", myDoc.pathItems[o].name);
        };
        dlg.pathList.items[0].selected = true;
        // buttons for ok, and cancel;
        dlg.buttons = dlg.add('panel', [15,152,325,210], '');
        dlg.buttons.buildBtn = dlg.buttons.add('button', [160,14,291,39], 'OK', {name:'ok'});
        dlg.buttons.cancelBtn = dlg.buttons.add('button', [14,14,145,39], 'Cancel', {name:'cancel'});
        // show the dialog;
        dlg.center();
        var myReturn = dlg.show ();
            if (myReturn == true) {
            myDoc.pathItems.getByName(dlg.pathList.selection).makeSelection ()
            myDoc.selection.invert()
            myDoc.selection.clear()
            }   
        }
    }

//JPG save options
jpgSaveOptions = new JPEGSaveOptions()
jpgSaveOptions.embedColorProfile = true
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE
jpgSaveOptions.matte = MatteType.WHITE
jpgSaveOptions.quality = 12

/////////////////////////////////////////////////////

//initial resize
myDoc.resizeImage (undefined, undefined, 300, ResampleMethod.NONE)

//first save (300 DPI JPG) using variable to prompt proper folder selection
var myHiResPrompt = Folder("/Volumes/network save location")
myDoc.saveAs (new File (myHiResPrompt),jpgSaveOptions,false, Extension.LOWERCASE)

/////////////////////////////////////////////////////

//second resize
if(myDoc.width > myDoc.height) {
    myDoc.resizeImage (5, undefined, 150, ResampleMethod.BICUBICSHARPER)
    }
else{
    myDoc.resizeImage (undefined, 5, 150, ResampleMethod.BICUBICSHARPER)
    }

//second save (150 DPI JPG) using variable to prompt proper folder selection
var myMidResPrompt = Folder ("/Volumes/network save location")
myDoc.saveAs (new File (myMidResPrompt), jpgSaveOptions, false, Extension.LOWERCASE)

/////////////////////////////////////////////////////

//third resize
if(myDoc.width > myDoc.height) {
    myDoc.resizeImage (4, undefined, 72, ResampleMethod.BICUBICSHARPER)
    }
    else {
    myDoc.resizeImage (undefined, 4, 72, ResampleMethod.BICUBICSHARPER)
    }

//third save (72 DPI PNG) using variable to prompt proper folder selection
var myLowResPrompt = Folder ("/Volumes/network save location")
myDoc.saveAs (new File (myLowResPrompt), PNGSaveOptions, false, Extension.LOWERCASE)

//resetting original units
app.preferences.rulerUnits = originalUnit



After some more testing, I'll be distributing it among my coworkers and will probably throw in a close w/o saving command at the end.

The following is the error I get running this script.
essejesse

save dialog with JPG save options specified

Post by essejesse »

If you just remove this part, it will continue without the error:

Code: Select all//RGB
doAction("RGB – With Merge","Jesse's Actions")//Just a quick check to make sure the image is in RGB, as some of the oldest ones we have are in CMYK

That's calling on an action I made within photoshop. You can do it other ways, but I wanted to make sure the image was in RGB color mode before continuing, and already had this action created.
Owl

save dialog with JPG save options specified

Post by Owl »

The script fires a new error ?
essejesse

save dialog with JPG save options specified

Post by essejesse »

I specified a location on my network here. If you haven't modified the script to use a new location on your own drive, or network drive, then you will get that error.

In the code I uploaded last, it was written like this:
"/Volumes/network save location"
That needs to be where you want the file saved. It happens three times, once each for 300 DPI, 150 DPI and 72 DPI, so be sure to change it that many times.