Hey guys, I'm very new to photoshop scripting but I have a fair bit of experience with OOP in javascript (client side and nodejs) also I have a bit of experience with maxscript (weep those who know it). So recently where i work someone asked for an incremental save script so here it is in its blazing beta glory! Some things might not work, I haven't been able to spend too much time on it, hope you can use / learn from it
Code: Select allfunction zeroPad ( num, digit ){
if( num < 10 ) return "00"+num;
if( num < 100 ) return "0"+num;
return num;
}
function basename( str ){
var regex = /(.*)\.[^\.]+/
var match = str.match( regex ) ? str.match( regex ) : [ str, str, undefined ];
return match[1];
}
function SaveIncrement()
{
try {
activeDocument;
}catch( err ){
alert( "You must haved saved the document to save a version." );
return;
}
// Properties
this.file = new File( decodeURI( activeDocument.name ) );
this.folder = new Folder( activeDocument.path );
this.basename = basename( this.file.name );
// Basic options
this.defaultExt = 'psd';
this.suffixStart = '_';
this.suffixLenght = 3;
// Save options
this.opts = new PhotoshopSaveOptions();
this.opts.embedColorProfile = true;
this.opts.alphaChannels = true;
this.opts.layers = true;
// Get the next unused increment
this.nextIncrement = function( basename ){
var regex = '.+?'+this.suffixStart+'(\\d+).*';
var increments = this.folder.getFiles( basename+this.suffixStart+'*.'+this.defaultExt );
var nextIncrement = 0;
for( var i=0; i < increments.length; i++ )
{
var increment;
var match = increments.name.match( regex );
if( !match || isNaN( Number( match[1] ) ) )
continue;
increment = Number( match[1] );
if( !isNaN( increment ) )
nextIncrement = increment > nextIncrement ? increment : nextIncrement;
}
nextIncrement++;
return nextIncrement;
}
// Save the file as the next increment
this.saveIncrement = function( increment ){
if( increment == undefined )
increment = this.nextIncrement( this.basename );
var saveFile = new File( this.folder+'/'+this.basename+this.suffixStart+zeroPad( increment, this.suffixLength )+'.'+this.defaultExt );
activeDocument.saveAs( saveFile, this.opts, true, Extension.LOWERCASE );
if( saveFile.exists )
return true;
return false;
}
// Create the ui and show it
this.show = function(){
var t = this;
dlg = new Window( 'dialog', 'Save Increment' );
// Set dialog color
var brush = dlg.graphics.newBrush( dlg.graphics.BrushType.THEME_COLOR, 'appDialogBackground' );
dlg.graphics.backgroundColor = brush;
dlg.graphics.diabledBackgroundColor = brush;
dlg.orientation = 'column';
dlg.alignChildren = 'left';
// Folder picker label
dlg.add( 'statictext', undefined, 'Increment Destination' );
// Top left group
dlg.grpTop = dlg.add( 'group' );
dlg.grpTop.orientation = 'column';
dlg.grpTop.alignChildren = 'top';
dlg.grpTop.alignment = 'fill';
// Dest line
dlg.uiSecondGRP = dlg.grpTop.add( 'group' );
dlg.uiSecondGRP.orientation = 'row';
dlg.uiSecondGRP.alignChildren = 'center';
dlg.uiDestinationTXT = dlg.uiSecondGRP.add( 'edittext', undefined, this.folder.fsName );
dlg.uiDestinationTXT.preferredSize.width = 300;
dlg.uiBrowseBTN = dlg.uiSecondGRP.add( 'button', undefined, 'Browse...' );
dlg.uiBrowseBTN.onClick = function(){
var selFolder = Folder.selectDialog( 'Select Destination' );
if( selFolder != null ){
dlg.uiDestinationTXT.text = selFolder.fsName;
t.folder = new Folder( selFolder );
}
dlg.defaultElement.active = true;
}
// Preview increment filename
dlg.uiPreviewGRP = dlg.grpTop.add( 'group' );
dlg.uiPreviewGRP.orientation = 'column';
dlg.uiPreviewGRP.alignChildren = 'left';
dlg.uiPreviewGRP.alignment = 'fill';
dlg.uiPreviewLBL = dlg.uiPreviewGRP.add( 'statictext', undefined, 'Increment Filename' );
var increment = this.nextIncrement( this.basename );
dlg.uiPreviewTXT = dlg.uiPreviewGRP.add( 'edittext', undefined, this.basename+this.suffixStart+zeroPad( increment, this.suffixLength )+'.'+this.defaultExt )
dlg.uiPreviewTXT.preferredSize.width = 300;
dlg.uiBottomGRP = dlg.add( 'group' );
dlg.uiBottomGRP.orientation = 'row';
dlg.uiBottomGRP.alignChildren = 'center';
dlg.uiBottomGRP.alignment = 'fill';
dlg.uiSaveBTN = dlg.uiBottomGRP.add( 'button', undefined, 'Save' );
dlg.uiSaveBTN.onClick = function(){
if( t.saveIncrement( increment ) ){
dlg.close();
} else {
alert( 'Failed to save iteration! :0' );
}
}
dlg.uiCancelBTN = dlg.uiBottomGRP.add( 'button', undefined, 'Cancel' );
dlg.uiCancelBTN.onClick = dlg.close;
dlg.center();
dlg.show();
}
return this;
}
si = new SaveIncrement();
si.show();