Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
If the file hasn't already been saved as a PSD, the script will prompt you to do that because it relies on that path for all the exports.
#target photoshop
var savePSD = false; // If set true the psd document is saved every time the script runs...
var folderName = ' (Frames)';
try {
var doc = app.activeDocument;
var fileExists = false;
try { if ( savePSD ) doc.save(); } catch(e){}
try {
fileExists = doc.path;
}
catch(e){
var idsave = charIDToTypeID( "save" );
var desc50 = new ActionDescriptor();
var idIn = charIDToTypeID( "In " );
desc50.putPath( idIn, new File( Folder.desktop.fullName ) );
var idDocI = charIDToTypeID( "DocI" );
desc50.putInteger( idDocI, 196 );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveSucceeded = stringIDToTypeID( "saveSucceeded" );
desc50.putEnumerated( idsaveStage, idsaveStageType, idsaveSucceeded );
executeAction( idsave, desc50, DialogModes.ALL );
fileExists = doc.path;
}
if ( fileExists ) {
saveNewVersion( doc );
}
}
catch( e ) {
// remove comments below to see error for debugging
// alert( e );
}
function saveNewVersion( doc, docName ) {
var extension = '.png';
var docName = doc.name.substring(0, doc.name.lastIndexOf('.'));
var docPath = doc.path;
var outputPath = docPath + '/' + doc.name + folderName;
var outputFolder = new Folder( outputPath );
if( !outputFolder.exists ) { outputFolder.create(); }
var verisonsList = outputFolder.getFiles( '*' + extension );
var versionsLength1 = verisonsList.length;
var number = getVersionNumber( docName, verisonsList );
var filename = number + extension;
doc.exportDocument( File( outputPath + '/' + filename ), ExportType.SAVEFORWEB, exportOptions() );
app.beep();
}
function getVersionNumber( docName, verisonsList ) {
var number = 0;
for ( var i = 0; i < verisonsList.length; i++ ) {
var file = verisonsList[i];
var fileName = File(file).displayName;
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
// Start looking for the version number if PSD filename contains doc name...
var versionNumber = fileName.match(/^[0-9]*/);
if ( versionNumber ) {
versionNumber = parseInt( versionNumber ,10);
if ( number < versionNumber ) {
number = versionNumber;
}
}
}
return number + 1;
}
function exportOptions() {
var options = new ExportOptionsSaveForWeb();
options.format = SaveDocumentType.PNG;
options.quality = 100;
return options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.