• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to suppress "save changes" dialog in jsx script

New Here ,
Jul 03, 2020 Jul 03, 2020

Copy link to clipboard

Copied

I have a photoshop jsx script that will work through multiple folders of images to be focus-stacked and output a final stacked image for each.  I want it to run non-interactively after telling it where the images are, as I may need it to crunch away for hours without me babysitting it.  The script is pasted below. 

 

The problem is that at the end of each stacking operation, a dialog box still appears, asking, Save changes to Adobe Photoshop document "Untitled-1" before closing?  Yes / No / Cancel.   I have included two lines of code that I thought would prevent this, but they don't do so:

 

app.displayDialogs = DialogModes.NO;
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

 

The last of these two lines is where the prompt arises, and execution halts until I click No. How do I suppress it, have the close() function not save changes as it closes the document, and have the script continue?

 

I am running Adobe Photoshop 2020 21.2.0 release in Windows 10 Home 10.0.18363.

 

// auto_focus_stack.jsx
// Adapted from a script by SuperMerlin at 
// https://community.adobe.com/t5/photoshop/automate-focus-stacking-script-action-help-needed/td-p/10483237?page=1
//
// Place this script in the Adobe Photoshop Presets Scripts directory.
//
// To run:  
// * Create a top folder with subfolders inside. 
// * Each subfolder should contain a series of images to stack into a single image and no other files.
// * Name each subfolder by the desired filename of the output stacked image, but omit the file extension.
//   E.g., if the stacked image filename should be "NorthSide.jpg", then name the subfolder "NorthSide".
//   The script will place the stacked images into a new subfolder called "processed" and will add the
//   file extension in the file names.
// * Open Photoshop and start the script with File > Scripts > auto_focus_stack. 
// * Select the top folder. Click Select Folder.
//
// The script will then create a "processed" subfolder, stack the images in each of the other subfolders,
// and save the stacked images in the "processed" subfolder.  

#target photoshop;
app.bringToFront();
app.displayDialogs = DialogModes.NO;
main();

function main(){
  // Asks user to select top directory.
  var selectedFolder = Folder.selectDialog("Please select the top folder to process.");    
  if(selectedFolder == null) return;

  // Gets a list of subfolders and files in the top directory.
  var contents = selectedFolder.getFiles();
  if(contents == null) return;

  // Creates an output subfolder.
  var outFolder = Folder(selectedFolder + "/processed");
  if(!outFolder.exists) outFolder.create();

  // Works on each subfolder, skipping other items in the top folder.
  for(var dir_ind = 0; dir_ind < contents.length; dir_ind++) {
    if(contents[dir_ind] instanceof Folder && contents[dir_ind].name != 'processed') {
	var stackFolder = contents[dir_ind];
        // Gets a list of photo files in the subfolder.
        var pictureFiles = stackFolder.getFiles(/\.(jpg)$/i);
        
        // Does the focus stacking.
        stackFiles(pictureFiles);
        selectAllLayers();
        autoAlign();
        autoBlendLayers();
	
        // Saves the stacked photo after reducing its size.
        resizeLayer(0.5);
        var saveFile = new File(outFolder + '/' + stackFolder.name + '.jpg');
        SaveJPEG(saveFile);

        // Closes the photos that were stacked.
        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);	
    }
  }

  alert("Script completed.");
};

function resizeLayer(factor) {
  new_width = app.activeDocument.width.value*factor
  new_height = app.activeDocument.height.value*factor
  app.activeDocument.resizeImage(new_width, new_height);
}

function autoBlendLayers(){
var d=new ActionDescriptor();
d.putEnumerated(stringIDToTypeID("apply"), stringIDToTypeID("autoBlendType"), stringIDToTypeID("maxDOF"));
d.putBoolean(stringIDToTypeID("colorCorrection"), true);
d.putBoolean(stringIDToTypeID("autoTransparencyFill"), true);
executeAction(stringIDToTypeID("mergeAlignedLayers"), d, DialogModes.NO);
};

function SaveJPEG(saveFile){
  SaveOptions = new JPEGSaveOptions(); 
  SaveOptions.embedColorProfile = true; 
  SaveOptions.formatOptions = FormatOptions.STANDARDBASELINE
  SaveOptions.quality = 12;
  activeDocument.saveAs(saveFile, SaveOptions, true, Extension.LOWERCASE); 
};

function SaveTIFF(saveFile){
tiffSaveOptions = new TiffSaveOptions(); 
tiffSaveOptions.embedColorProfile = true; 
tiffSaveOptions.alphaChannels = true; 
tiffSaveOptions.layers = true;
tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW; 
activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE); 
};

function selectAllLayers() {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
executeAction( stringIDToTypeID('selectAllLayers'), desc, DialogModes.NO );
};

function stackFiles(sFiles){  
var loadLayersFromScript = true;  
var SCRIPTS_FOLDER =  decodeURI(app.path + '/' + localize('$$$/ScriptingSupport/InstalledScripts=Presets/Scripts')); 
$.evalFile( new File(SCRIPTS_FOLDER +  '/Load Files into Stack.jsx'));   
loadLayers.intoStack(sFiles);  
};

function autoAlign() {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
desc.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('ADSt'), stringIDToTypeID('ADSContent') );
desc.putEnumerated( charIDToTypeID('Aply'), stringIDToTypeID('projection'), charIDToTypeID('Auto') );
desc.putBoolean( stringIDToTypeID('vignette'), false );
desc.putBoolean( stringIDToTypeID('radialDistort'), false );
executeAction( charIDToTypeID('Algn'), desc, DialogModes.NO );
};

function autoBlend() {
var desc = new ActionDescriptor();
desc.putEnumerated( charIDToTypeID('Aply'), stringIDToTypeID('autoBlendType'), stringIDToTypeID('maxDOF') );
desc.putBoolean( charIDToTypeID('ClrC'), true );
executeAction( stringIDToTypeID('mergeAlignedLayers'), desc, DialogModes.NO );
};

 

TOPICS
Actions and scripting

Views

701

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Beginner ,
Nov 08, 2020 Nov 08, 2020

Copy link to clipboard

Copied

Hi there, I have a similar issue, just wondering if you resolved this? Let us know.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 24, 2023 Sep 24, 2023

Copy link to clipboard

Copied

LATEST

Hello, i have ussing your script. I see problem with output JPG fomat. Can you change PSD is working

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines