Skip to main content
Inspiring
September 27, 2016
Answered

Yet Another Incremental Save Question....

  • September 27, 2016
  • 2 replies
  • 1290 views

Hi there.. I am trying to get incremental save working in a way that can be placed into a ACTION.

Basically I am scanning a large number of small items (over 3000), and I can fit 18 on the scanner at once. If I use the "Straiten and Crop" script the scanned file gets straitened and cropped nicely.

I made an Action that loads the scans from a folder and runs the straiten and crop script, then dose some editing to the cropped image, saves it and then closes it. This "edit/save/close" repeats until all the images are processed from the original scan.

The problem is that no matter what I do the save file in the batch is always the same, or askes for over write. What I would like is a very simple script that saves a increment number on each save file. There is a lot of threads on this, but the only one I could get working had this script in it...

// This script will save the active document to a folder with an incremental sufix 
// Change the options below to match your needs 
var saveFolder = new Folder( 'C:\\test' ); 
var saveExt = 'jpg'; 
var saveSufixStart = '_'; 
var saveSufixLength = 3; 
jpgSaveOptions = new JPEGSaveOptions(); 
jpgSaveOptions.embedColorProfile = true; 
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 
jpgSaveOptions.matte = MatteType.NONE; 
jpgSaveOptions.quality = 8; 
// End of user options 
//========================================== 
function zeroPad ( num, digit ){ 
   var tmp = num.toString(); 
   while (tmp.length < digit) { tmp = "0" + tmp;} 
   return tmp; 
var docName = decodeURI ( activeDocument.name ); 
docName = docName.match( /(.*)(\.[^\.]+)/ ) ? docName = docName.match( /(.*)(\.[^\.]+)/ ) : docName = [ docName, docName, undefined ]; 
var saveName = docName[ 1 ]; // activeDocument name with out ext 
var files = saveFolder.getFiles( saveName + '*.' + saveExt );// get an array of files matching doc name prefix 
var saveNumber = files.length + 1; 
//alert("New file number: " + zeroPad( saveNumber, saveSufixLength )); 
var saveFile = new File( saveFolder + '/' + saveName + '_' + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt ); 
activeDocument.saveAs( saveFile, jpgSaveOptions ,true ,Extension.LOWERCASE); 

This seams to work fine.. but there are some problems, and I am just a simple paint, scan manipulate type guy and no idea how to improve it...

1) - It only saves into "c:\test". I have tried pasting other paths in there.. like "d:\projects\project name\scans\processed\" but it doesn't help.

2) - The Straiten and Crop Script works fine called from an action. So I can make a single action to process the file. The script above works fine in an action along with the editing actions. The problem is if I call the editing / save action from the "open/straiten and crop" action, then the script dosn't error but I can not find the files it is suposed to make (probably got a billion jpgs lost on my drive somewhere now)

If anyone can help with this I would be appreciative.

Thanks!

This topic has been closed for replies.
Correct answer Tomas Sinkunas

I took a liberty of rewriting your code a bit. Code will check if folder "d:\projects\project name\scans\processed" exists. If it doesn't exist - it will prompt you to select different

Also it will save "img_003.jpg" if such file does not exist. For instance:

img_001.jpg,

img_002.jpg

img_004.jpg

saveIncrementally();

function saveIncrementally() {

     try {

          var saveFolder = "d:\projects\project name\scans\processed";

          if (!Folder(saveFolder).exists) {

               saveFolder = Folder.selectDialog("Select output folder");

          if (!saveFolder)

               return;

     }

    

     var saveExt = 'jpg';

     var saveSufixStart = '_';

     var saveSufixLength = 3;

     // End of user options

     //==========================================

     var docName = decodeURI ( activeDocument.name );

     var dot = docName.lastIndexOf(".");

     var saveName = docName.slice(0, dot);

     var suffix = zeroPad(0, saveSufixLength);

     var myFile = saveFolder + "/" + saveName + "_" + suffix + ".jpg";

     while (File(myFile).exists) {

          var suffixInteger = parseInt(suffix, 10);

               suffixInteger += 1;

          suffix = zeroPad(suffixInteger, 3);

          myFile = saveFolder + "/" + saveName + "_" + suffix + ".jpg";

     }

     saveJPEG(myFile, 8)

     function saveJPEG(saveFile, qty ) {

          var saveOptions = new JPEGSaveOptions();

          saveOptions.embedColorProfile = true;     

          saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

          saveOptions.matte = MatteType.NONE;

          saveOptions.quality = qty;

          app.activeDocument.saveAs( File(saveFile), saveOptions, true );

     }

     function zeroPad ( num, digit ){

          var tmp = num.toString();

          while (tmp.length < digit) { tmp = "0" + tmp;}

          return tmp;

     }

     } catch(e) {

          alert(e.toString() + "\nLine: " + e.line.toString())

     }

}

2 replies

abUSER23Author
Inspiring
October 5, 2016

Thanks Tom...

Dose that have a way of adjusting the save settings? Say I want it to save a PNG or a high 10 quality JPG?

JJMack
Community Expert
October 6, 2016

change 8 to 10

saveJPEG(myFile, 8);

JJMack
Tomas Sinkunas
Tomas SinkunasCorrect answer
Brainiac
September 27, 2016

I took a liberty of rewriting your code a bit. Code will check if folder "d:\projects\project name\scans\processed" exists. If it doesn't exist - it will prompt you to select different

Also it will save "img_003.jpg" if such file does not exist. For instance:

img_001.jpg,

img_002.jpg

img_004.jpg

saveIncrementally();

function saveIncrementally() {

     try {

          var saveFolder = "d:\projects\project name\scans\processed";

          if (!Folder(saveFolder).exists) {

               saveFolder = Folder.selectDialog("Select output folder");

          if (!saveFolder)

               return;

     }

    

     var saveExt = 'jpg';

     var saveSufixStart = '_';

     var saveSufixLength = 3;

     // End of user options

     //==========================================

     var docName = decodeURI ( activeDocument.name );

     var dot = docName.lastIndexOf(".");

     var saveName = docName.slice(0, dot);

     var suffix = zeroPad(0, saveSufixLength);

     var myFile = saveFolder + "/" + saveName + "_" + suffix + ".jpg";

     while (File(myFile).exists) {

          var suffixInteger = parseInt(suffix, 10);

               suffixInteger += 1;

          suffix = zeroPad(suffixInteger, 3);

          myFile = saveFolder + "/" + saveName + "_" + suffix + ".jpg";

     }

     saveJPEG(myFile, 8)

     function saveJPEG(saveFile, qty ) {

          var saveOptions = new JPEGSaveOptions();

          saveOptions.embedColorProfile = true;     

          saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

          saveOptions.matte = MatteType.NONE;

          saveOptions.quality = qty;

          app.activeDocument.saveAs( File(saveFile), saveOptions, true );

     }

     function zeroPad ( num, digit ){

          var tmp = num.toString();

          while (tmp.length < digit) { tmp = "0" + tmp;}

          return tmp;

     }

     } catch(e) {

          alert(e.toString() + "\nLine: " + e.line.toString())

     }

}

abUSER23Author
Inspiring
December 5, 2016

I'm still having some trouble with this script... I can not change the save folder... . no matter what I do it always opens the "save as" dialogue looking for a location.....

I have tried changing it to...

var saveFolder = "d:\Projects\Test\Raw-Scans\_Cut";

or

var saveFolder = "d:\Projects\Test\Raw-Scans\_Cut\";

but no luck?

How can I make this script require no interaction.. so I can just run it and it save and so I can edit the file as needed to change the folder if I need?

Thanks!

abUSER23Author
Inspiring
December 5, 2016

Never mind... I worked it out about 2 seconds after I posted!! (I did spend AGES googleing trying to work it out" but after I came here I found another script on the site and noticed it used // instead of /...

var saveFolder = "d:\\Projects\\Test\\Raw-Scans\\_Cut";

This works!