How to resave file in another location and keep its original type
Hi All,
I am not much experienced in PS scripting. Now I encountered a problem. I thought it would be a simple task: just open an image and resave it in a different location.
I tried this code, but it always results in a PSD-file, no matter which type I open: tif, jpg, etc.
function ResaveFile() {
try {
var path = "/D/Test/Egret.tif";
var file = new File(path);
if (file.exists) {
var doc = app.open(file);
var newFolderPath = "~/Desktop/";
var newFile = new File(newFolderPath + doc.name);
doc.saveAs(newFile);
doc.close(SaveOptions.DONOTSAVECHANGES);
}
else {
$.writeln("File doesn't exist");
}
}
catch(err) {
$.writeln(err.message + ", line: " + err.line);
}
}
ResaveFile();
I tried to set asCopy to true/false and it made no difference:
doc.saveAs(newFile, undefined, true, Extension.LOWERCASE);
doc.saveAs(newFile, undefined, false, Extension.LOWERCASE);
I came to conclusion that the only way is to set the options parameter depending on the original image type, like so:
function ResaveFile() {
try {
var path = "/D/Test/Egret.tif";
var file = new File(path);
if (file.exists) {
var doc = app.open(file);
var newFolderPath = "~/Desktop/";
var newFile = new File(newFolderPath + doc.name);
if (doc.name.match(/\.tif$/) != null) {
var options = new TiffSaveOptions();
}
else if (doc.name.match(/\.psd$/) != null) {
var options = new PhotoshopSaveOptions();
}
else if (doc.name.match(/\.jpg$/) != null) {
var options = new JPEGSaveOptions();
}
doc.saveAs(newFile, options, true, Extension.LOWERCASE);
doc.close(SaveOptions.DONOTSAVECHANGES);
}
else {
$.writeln("File doesn't exist");
}
}
catch(err) {
$.writeln(err.message + ", line: " + err.line);
}
}
ResaveFile();
But I am afraid that some default app settings will be used in this case instead of the image being opened so some parameters may change in the resaved image.
Could someone clarify the situation?
I am trying to rework my Resize images script for InDesign to make it work on copies instead of original images.
Maybe I should use another approach: first copy the image with File's copy() method and work with the copy.
Thanks in advance!
— Kas
