Highlighted

PS Script : Incremental save

Community Beginner ,
Sep 09, 2020

Copy link to clipboard

Copied

Hello,

I have this script which saves my open document as the file name, + an increment.

 

Works great but..it currently saves in a specified directory.
Is it possible to modify this to save the document in its current directory?
Thanks for any help!
Jeff

    
#target photoshop

// 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:/Storyboards/Projects01' );
    var saveExt = 'psd';
    var saveSufixStart = '_';
    var saveSufixLength = 3;
    psdOpts = new PhotoshopSaveOptions();
    psdOpts.embedColorProfile = true;
    psdOpts.alphaChannels = true;
    psdOpts.layers = true;
    // 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

    if( files.length == 0 ) {  // no file with that name so start at one
       var saveNumber = 1;
    }
    if( files.length == 1 ) { // one file found, see if it has a sufix
       var fileName = decodeURI ( files[ 0 ].name );
       fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
       if( fileName[1].match( /_(\d{3})$/ ) == null ){
          var saveNumber = 1;// does not have sufix so set to one
       } else{// has sufix
          var saveNumber = parseInt( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1
       }
    }
    if( files.length > 1 ){
       files.sort();
       var fileName = decodeURI ( files[ files.length -1 ].name );
       fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
       var saveNumber = Number( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1
    }
    var saveFile = new File( saveFolder + '/' + saveName + saveSufixStart + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt );
    activeDocument.saveAs( saveFile, psdOpts ,true ,Extension.LOWERCASE);

 

 

TOPICS
Actions and scripting

Views

54

Likes

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

PS Script : Incremental save

Community Beginner ,
Sep 09, 2020

Copy link to clipboard

Copied

Hello,

I have this script which saves my open document as the file name, + an increment.

 

Works great but..it currently saves in a specified directory.
Is it possible to modify this to save the document in its current directory?
Thanks for any help!
Jeff

    
#target photoshop

// 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:/Storyboards/Projects01' );
    var saveExt = 'psd';
    var saveSufixStart = '_';
    var saveSufixLength = 3;
    psdOpts = new PhotoshopSaveOptions();
    psdOpts.embedColorProfile = true;
    psdOpts.alphaChannels = true;
    psdOpts.layers = true;
    // 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

    if( files.length == 0 ) {  // no file with that name so start at one
       var saveNumber = 1;
    }
    if( files.length == 1 ) { // one file found, see if it has a sufix
       var fileName = decodeURI ( files[ 0 ].name );
       fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
       if( fileName[1].match( /_(\d{3})$/ ) == null ){
          var saveNumber = 1;// does not have sufix so set to one
       } else{// has sufix
          var saveNumber = parseInt( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1
       }
    }
    if( files.length > 1 ){
       files.sort();
       var fileName = decodeURI ( files[ files.length -1 ].name );
       fileName = fileName.match( /(.*)(\.[^\.]+)/ ) ? fileName = fileName.match( /(.*)(\.[^\.]+)/ ) : fileName = [ fileName, fileName, undefined ];
       var saveNumber = Number( fileName[ 1 ].match( /_(\d{3})$/ )[1] ) + 1; // strip the ext and get the sufix , convert to number and add 1
    }
    var saveFile = new File( saveFolder + '/' + saveName + saveSufixStart + zeroPad( saveNumber, saveSufixLength ) + '.' + saveExt );
    activeDocument.saveAs( saveFile, psdOpts ,true ,Extension.LOWERCASE);

 

 

TOPICS
Actions and scripting

Views

55

Likes

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
Sep 09, 2020 0
Adobe Community Professional ,
Sep 09, 2020

Copy link to clipboard

Copied

Change the following line

 

var saveFolder = new Folder( 'C:/Storyboards/Projects01' );

 

To

 

if(!app.documents[0].saved)
{
  CAlert("Please save the file before execution")
  exit()
}
var saveFolder = new Folder(app.activeDocument.fullName.parent)

 

-Manan

Likes

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
Reply
Loading...
Sep 09, 2020 1
Community Beginner ,
Sep 10, 2020

Copy link to clipboard

Copied

Works Great!

 

Many thanks,  Manan

Likes

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
Reply
Loading...
Sep 10, 2020 0