Skip to main content
Participant
November 10, 2013
Question

Quick Save as Versions with Script?

  • November 10, 2013
  • 2 replies
  • 960 views

I'm looking to modify a script I found online but unfortunately I don't know enough Javascript to figure it out.

Here's the original code from this blog post:

#target photoshop;

if (app.documents.length > 0) {

var thedoc = app.activeDocument;

  var docName = thedoc.name;

if (docName.indexOf(".") != -1) {

  var basename = docName.match(/(.*)\.[^\.]+$/)[1]

} else {

  var basename = docName

}

  //getting the location, if unsaved save to desktop;

try {

  var docPath = thedoc.path

} catch (e) {

  var docPath = "~/Desktop"

}

var jpegOptions = new JPEGSaveOptions();

jpegOptions.quality = 9;

jpegOptions.embedColorProfile = true;

jpegOptions.matte = MatteType.NONE;

var filename = docPath + '/' + basename + "-" + getTime() + '.jpg';

  thedoc.saveAs((new File(filename)), jpegOptions, true);

};

function getTime(){

var currentTime = new Date();

  //Make single-digit mins show up as 6:01 and not 6:1

var minutes = currentTime.getMinutes();

if (minutes < 10) {

  minutes = "0" + minutes;

}

  var timeStamp = currentTime.getFullYear() + "-"

     + (currentTime.getMonth() + 1) + "-"

     + currentTime.getDate() + "-"

     + currentTime.getHours() + "."

     + minutes + "."

     + currentTime.getSeconds() + "."

     + currentTime.getMilliseconds();

return timeStamp;

}

What I'm trying to do is instead of saving with a timestamp, I'd rather have the script automatically detect the most recent version (eg: MyPicture_04.jpg) and save as the next logical increment (eg: MyPicture_05.jpg)

Anyone know how I'd be able to go about scripting that?

Thanks!

This topic has been closed for replies.

2 replies

Participant
December 13, 2016

learn quick functions in Photoshop http://skillonpage.com/quick-functions-photoshop/

Chuck Uebele
Community Expert
Community Expert
November 10, 2013

try this:

#target photoshop

if (app.documents.length > 0) {

var thedoc = app.activeDocument;

   

  var docName = thedoc.name;

if (docName.indexOf(".") != -1) {

  var basename = docName.match(/(.*)\.[^\.]+$/)[1]

} else {

  var basename = docName

}

  var baseArray = basename.split("_")

  var imgNumStr = baseArray[baseArray.length-1]

  var replaceNum = Number (imgNumStr) + 1000000001

  var replaceStr = replaceNum.toString().substr (replaceNum.toString().length-imgNumStr.toString().length, imgNumStr.toString().length)

 

  var nameBeg = basename.substr(0,basename.length-replaceStr.length)

 

  if(Number (replaceStr)==0)

  {replaceStr = replaceNum.toString().substr (replaceNum.toString().length-imgNumStr.toString().length-1, imgNumStr.toString().length+1)}

  //getting the location, if unsaved save to desktop;

try {

  var docPath = thedoc.path

} catch (e) {

  var docPath = "~/Desktop"

}

var jpegOptions = new JPEGSaveOptions();

jpegOptions.quality = 9;

jpegOptions.embedColorProfile = true;

jpegOptions.matte = MatteType.NONE;

var filename = docPath + '/' + nameBeg + replaceStr +  '.jpg';

  thedoc.saveAs((new File(filename)), jpegOptions, true);

};

Participant
November 10, 2013

Hey, thanks for your help.

When I loaded your script it would revoke some of the naming of my original file, but with some tinkering I got it to work, but there are still some problems. One is it only saves one copy and then it'll just overwrite it...and the other is that when I'm creating the first save, my file comes out as 3LINE_V2_02a_NaN.jpg (bold is the part the script creates, the rest is my filename) and then after that it'll just overwrite it. Here's the script I have atm:

--------------------------------------------------------------------------

#target photoshop;

if (app.documents.length > 0) {

var thedoc = app.activeDocument;

  var docName = thedoc.name;

if (docName.indexOf(".") != -1) {

  var basename = docName.match(/(.*)\.[^\.]+$/)[1]

} else {

  var basename = docName

}

  if (docName.indexOf(".") != -1) {

   var basename = docName.match(/(.*)\.[^\.]+$/)[1]

  } else {

   var basename = docName

  }

  var baseArray = basename.split("_")

  var imgNumStr = baseArray[baseArray.length-1]

  var replaceNum = Number (imgNumStr) + 1000000001

  var replaceStr = replaceNum.toString().substr (replaceNum.toString().length-imgNumStr.toString().length, imgNumStr.toString().length)

  var nameBeg = basename.substr(0,basename.length-replaceStr.length)

  if(Number (replaceStr)==0)

  {replaceStr = replaceNum.toString().substr (replaceNum.toString().length-imgNumStr.toString().length-1, imgNumStr.toString().length+1)}

  //getting the location, if unsaved save to desktop;

try {

  var docPath = thedoc.path

} catch (e) {

  var docPath = "~/Desktop"

}

var jpegOptions = new JPEGSaveOptions();

jpegOptions.quality = 10;

jpegOptions.embedColorProfile = true;

jpegOptions.matte = MatteType.NONE;

var filename = docPath + '/' + basename + "_" + replaceStr + '.jpg';

  thedoc.saveAs((new File(filename)), jpegOptions, true);

};

function getTime(){

var currentTime = new Date();

  //Make single-digit mins show up as 6:01 and not 6:1

var minutes = currentTime.getMinutes();

if (minutes < 10) {

  minutes = "0" + minutes;

}

  //Make single-digit hours show up as 06:01 and not 6:1

var hours = currentTime.getHours();

if (hours < 10) {

  hours = "0" + hours;

}

  var timeStamp = currentTime.getFullYear() + "_"

     + (currentTime.getMonth() + 1) + "_"

     + currentTime.getDate() + "_"

     + currentTime.getHours() + "_"

     + minutes + "_"

     + currentTime.getSeconds()

return timeStamp;

}