PHOTOSHOP Script - Please help me to save optimized file in another location.

  • 1
  • Question
  • Updated 3 years ago
  • (Edited)
I have JavaScript script for Photoshop, which based on some kind of optimization algorithm from tinyPNG.
1) It receives image.
2) performs optimization
3) saves it by doing over running the existing (original file).

I would like to save optimized image to another folder.
the main part of the script as following:
function compressFile(file, type) {

try{
// Open the file without dialogs like Adobe Camera Raw
var opener = new ActionDescriptor();
opener.putPath(charIDToTypeID("null"), file);
executeAction(charIDToTypeID("Opn "), opener, DialogModes.NO);

// Select the opened document
var document = app.activeDocument;

var fileInfo = getFileDetails (file, " ", "before");
var fileSizeBefore = fileInfo[5];

// Change the color space to RGB if needed
if (document.mode == DocumentMode.INDEXEDCOLOR) {
document.changeMode(ChangeMode.RGB);
}

// Switch to 8 bit RGB if the image is 16 bit
if (document.bitsPerChannel == BitsPerChannelType.SIXTEEN) {
convertBitDepth(document, 8);
}

var _type = charIDToTypeID(type); /* tyJP for JPEG */
percentage = 100;

// Compress the document
var tinify = new ActionDescriptor();

tinify.putPath(charIDToTypeID("In "), file); /* Overwrite original! */
tinify.putEnumerated(charIDToTypeID("FlTy"), charIDToTypeID("tyFT"), _type);
tinify.putUnitDouble(charIDToTypeID("Scl "), charIDToTypeID("#Prc"), percentage );

var compress = new ActionDescriptor();
compress.putObject(charIDToTypeID("Usng"), charIDToTypeID("tinY"), tinify);
document.fullName = getAssetSavePath(file.path, file.name);
executeAction(charIDToTypeID("Expr"), compress, DialogModes.NO);
//Close the temp document without saving the changes
document.close(SaveOptions.DONOTSAVECHANGES);
var fileInfo = getFileDetails (file, "Successful;", "after");
}
catch(error){
//Write the Log to the file
var fileInfo = getFileDetails (file, "Failure;", "before");
var fileSizeBefore = fileInfo[5];
internalLogFile.writeIntoFile ("Error: "+error+ "\r\n");
internalLogFile.writeIntoFile (fileInfo[0] +fileInfo[1] + fileInfo[2]+fileInfo[3]+ fileInfo[4]+fileSizeBefore+fileInfo[6]+ "\r\n");
}
rawReportFile.writeIntoFile (fileInfo[0] +fileInfo[1] + fileInfo[2]+fileInfo[3]+ fileInfo[4]+fileSizeBefore+fileInfo[6]+ "\r\n");
}
function getAssetSavePath(savePath, saveFileName){
splitPath = savePath.toString().split("/");
splitPath[2] = "bingoDimaNowTest"
var newSavePath = splitPath.join ("/");
var assetSavePath = new Folder (newSavePath);
if(! assetSavePath.exists) {
assetSavePath.create();
}
var assetSaveFile = new File(assetSavePath+"/"+saveFileName);
return (assetSaveFile);
}

Please help me to save optimized file in another location.
Photo of D F

D F

  • 4 Posts
  • 0 Reply Likes
  • frustrated

Posted 3 years ago

  • 1

Be the first to post a reply!