Skip to main content
Kasyan Servetsky
Legend
February 8, 2019
Answered

How to resave file in another location and keep its original type

  • February 8, 2019
  • 3 replies
  • 743 views

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

This topic has been closed for replies.
Correct answer Chuck Uebele

Yes, you need to create a save option for whatever type of file you want to save. You can add options, if you want. If you don't, it will save the file with the default save options.

3 replies

Kasyan Servetsky
Legend
February 10, 2019

Thank you for your quick reply, Chuck!

Legend
February 9, 2019

Using the copy() function is a good idea.
I do not know how your script works and what its purpose is, but you can use a duplicate document obtained using the doc.duplicate() method.

Kasyan Servetsky
Legend
February 10, 2019

Yes, this turned out to be the best approach in my case: I added a few lines in the InDesign part of the script, like so

if (saveAsCopy) {

    newLinkFile = new File(newLinksFolderPath + myLink.name);

    var copyResult = oldLinkFile.copy(newLinkFile);

    myImagePath = newLinkFile.fsName;

}

leaving the Photoshop part as it is.

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
February 9, 2019

Yes, you need to create a save option for whatever type of file you want to save. You can add options, if you want. If you don't, it will save the file with the default save options.