janowhatever
janowhatever

Reputation: 13

Photoshop Javascript script: Rename file and saveAs

Im new to using JS with photoshop and have some trouble. What I want to do is to get rid of the word "Online" in the file name of the current document and then save a JPG with new file name in a different folder.

With the help of the adobe reference I came up with the following script:

//Path where the final jpg should be saved
var JPGquality = 12;
var docPath="C:\Users\user\Desktop\test";
var docName='';

docName = activeDocument.name;

//Set new file name by replacing "_Online_" with "_"

var NewName = docName.replace("_Online_", "_");



var saveFile = new File(docPath+'/'+NewName+ '.jpg');


//Save JPG 

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);
}

The Script runs through but therewithout errors but nothing happens. It would be very helpful if someone could tell me what Ive done wrong. Hope someone helps me figuring out how to fix this ;)

Upvotes: 1

Views: 9140

Answers (3)

BQTP
BQTP

Reputation: 21

I use this:

function saveAsJPG() {

    jpgFile = new File(outputFolder + "/" + _CardFileName + ".jpg");
    jpgSaveOptions = new JPEGSaveOptions();
    jpgSaveOptions.embedColorProfile = true;
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
    jpgSaveOptions.matte = MatteType.NONE;
    jpgSaveOptions.quality = 12;
    docRef.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);

}

Upvotes: 2

Anna Forrest
Anna Forrest

Reputation: 1741

The best way to write paths in javascript for photoshop automation is '/c/users/user/' That works on both mac and windows and you don't need to escape the backslashes

Upvotes: 0

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179119

Try to use forward slashes in your docPath:

var docPath="C:/Users/user/Desktop/test";

\t (tab) and \u (beginning of a Unicode sequence) have special meanings in JS strings.

Or you can escape them, of course:

var docPath="C:\\Users\\user\\Desktop\\test";

Upvotes: 0

Related Questions