Copy link to clipboard
Copied
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?
1 Correct answer
Replace the line
var docPath =Folder("~/Desktop");
with
var docPath = decodeURI(activeDocument.path);
Explore related tutorials & articles
Copy link to clipboard
Copied
This would be a good start:
http://ps-scripts.com/bb/viewtopic.php?t=2772
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
Hm, it almost gets through it, but hangs on the last line. The error states:
Error 1224: Numeric value expected
Line: 23
-> activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
Copy link to clipboard
Copied
In the JPEGSaveOptions you need to set the quality to a value between 1 and 12
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Replace the line
var docPath =Folder("~/Desktop");
with
var docPath = decodeURI(activeDocument.path);
