Skip to main content
Known Participant
July 17, 2009
Answered

adding suffix to file name and re-saving

  • July 17, 2009
  • 1 reply
  • 3945 views

I need a simple script that saves the open file as a jpeg at 80% compression to the same directory as the original, while adding a suffix to the end of the file name.  So for instance, if the file open is "image.png", I'd like it saved as "image_thumb.jpg".  I've tried to create my own, but I just don't have the chops for scripting.  Can anyone help?

This topic has been closed for replies.
Correct answer Michael_L_Hale

Ah, works great!  I'm sure you can see now how little experience I have with scripting.  I've marked this answered, but what would I have to change to have the file saved to the same folder as the original?

I found this, but I'm not sure how to use it.


Replace the line

var docPath =Folder("~/Desktop");

with

var docPath = decodeURI(activeDocument.path);

1 reply

Inspiring
July 17, 2009

This would be a good start:

http://ps-scripts.com/bb/viewtopic.php?t=2772

Known Participant
July 20, 2009

Thanks xbytor2, but unfortunately I'm still having problems with it. I amended the script to set the "docPath" just to "~/Desktop" instead of "~/Desktop/cropped" and I then I get the following script alert:

Unknown filetype please use manual save.

Not exactly sure why it's doing that.  Any help would be appreciated.

Inspiring
July 21, 2009

I've taken the script mentioned above and modified it to the best of my abilities.  Still getting the same error, unfortunately:

function main(){
/////////////////////////////////////////////////////////////////////////////////////////
var suffix ="cropped";
var docPath =Folder("~/Desktop");
//////////////////////////////////////////////////////////////////////////////////////////
if(!docPath.exists){
   alert(docPath + " does not exist!\n Please create it!");
   return;
   }
var fileName =activeDocument.name.match(/(.*)\.[^\.]+$/)[1];
var fileExt =activeDocument.name.toLowerCase().match(/[^\.]+$/).toString();
var saveFile = new File(docPath +"/"+fileName+suffix+'.'+fileExt);

switch (fileExt){
   case 'jpeg' : SaveJPEG(saveFile); break;
   default : alert("Unknown filetype please use manual save!"); break;
   }
}
if(documents.length) main();

function SaveJPEG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality; //1-12
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}


My guess is that your document does not have the ext of 'jpeg'. The script below will save the doc as a jpg no matter what the ext.

function main(){
//////////////////////////////////////////////////////////////////////////////// /////////
var suffix ="_thumb";
var docPath =Folder("~/Desktop");
//////////////////////////////////////////////////////////////////////////////// //////////
if(!docPath.exists){
   alert(docPath + " does not exist!\n Please create it!");
   return;
   }
var fileName =activeDocument.name.match(/(.*)\.[^\.]+$/)[1];
var saveFile = new File(docPath +"/"+fileName+suffix+'.jpg');
SaveJPEG(saveFile);

}
if(documents.length) main();

function SaveJPEG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality; //1-12
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}