Skip to main content
Participant
August 10, 2023
Answered

Scripts for Saving Targa (TGA) files to a known folder location.

  • August 10, 2023
  • 1 reply
  • 2169 views

I'm looking for a script that allows me to save a psd file as a TGA file.    The psd file will have the filename format of car_12345.psd and I'd like to be able to save it as car_12345.tga or in case 2 as car_spec_12345.tga. 

 

All tga files need to be saved to a C:\application\paint\<carmodel> folder where <carmodel> depends on the parent folder of car_12345.psd.      The parent folder could have a matching folder name but at the moment its just a design folder that holds resources used in the psd and back up files. 

 

I'd like to have the filesave dialog open in C:\application\paint\ or even better C:\application\paint\parent_folder.

 

I have found two helpful answers by @Kukurykus and @SuperMerlin (links below) but neither quite meet the brief.  

 

Looking for a script to save as targa file saves the file into the parent folder without any option for user interaction.

 

looking for a save as Targa script this adds a suffix and is closer in someways but how would I change the save as location?

 

Thanks Max.

This topic has been closed for replies.
Correct answer Stephen Marsh

@Max Pickle 

 

Do you really need the Save dialog window to open?

 

The following script will do what you request without the Save dialog window, "silently" saving to the required static output directory and creating the sub-directory based on the PSD parent directory name. You will be prompted to replace a file of the same name if it exists.

 

The naming is only for Case #1:

car_12345.tga

 

It isn't clear to me how Case #2 should be triggered:

car_spec_12345.tga

 

/*
Save TGA to Paint Folder.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/scripts-for-saving-targa-tga-files-to-a-known-folder-location/td-p/13998437
v1.1 - 11th August 2023, Stephen Marsh
*/

#target photoshop

(function () {

    if (app.documents.length > 0) {

        // Set the source doc parent folder name
        var sourceDirName = activeDocument.path.displayName;
        // Set the base save folder
        var thePath = "C:/application/paint";
        // Remove the file extension
        var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
        // Base folder sub-folder from PSD folder name
        var destinationDir = new Folder(thePath + "/" + sourceDirName)
        if (!destinationDir.exists) {
            destinationDir.create()
        }
        var theFile = File(destinationDir + "/" + docName + ".tga");
        if (theFile.exists) {
            // true = 'No' as default active button
            if (!confirm("'" + theFile.name + "' exists, overwrite: Yes or No?", true))
                return;
        }

        SaveTARGA(theFile);


        function SaveTARGA(saveFile) {
            SaveOptions = new TargaSaveOptions();
            TargaSaveOptions.alphaChannels = true; // Include alpha channels, change to false for none
            TargaSaveOptions.resolution = TargaBitsPerPixels.TWENTYFOUR; // Options of SIXTEEN or THIRTYTWO
            TargaSaveOptions.rleCompression = true; // False for no compression
            activeDocument.saveAs(saveFile, TargaSaveOptions, true, Extension.LOWERCASE);
        }

    } else {
        alert("A document must be open to save a TGA file!");
    }

})();

 

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
August 11, 2023

@Max Pickle 

 

Do you really need the Save dialog window to open?

 

The following script will do what you request without the Save dialog window, "silently" saving to the required static output directory and creating the sub-directory based on the PSD parent directory name. You will be prompted to replace a file of the same name if it exists.

 

The naming is only for Case #1:

car_12345.tga

 

It isn't clear to me how Case #2 should be triggered:

car_spec_12345.tga

 

/*
Save TGA to Paint Folder.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/scripts-for-saving-targa-tga-files-to-a-known-folder-location/td-p/13998437
v1.1 - 11th August 2023, Stephen Marsh
*/

#target photoshop

(function () {

    if (app.documents.length > 0) {

        // Set the source doc parent folder name
        var sourceDirName = activeDocument.path.displayName;
        // Set the base save folder
        var thePath = "C:/application/paint";
        // Remove the file extension
        var docName = activeDocument.name.replace(/\.[^\.]+$/, '');
        // Base folder sub-folder from PSD folder name
        var destinationDir = new Folder(thePath + "/" + sourceDirName)
        if (!destinationDir.exists) {
            destinationDir.create()
        }
        var theFile = File(destinationDir + "/" + docName + ".tga");
        if (theFile.exists) {
            // true = 'No' as default active button
            if (!confirm("'" + theFile.name + "' exists, overwrite: Yes or No?", true))
                return;
        }

        SaveTARGA(theFile);


        function SaveTARGA(saveFile) {
            SaveOptions = new TargaSaveOptions();
            TargaSaveOptions.alphaChannels = true; // Include alpha channels, change to false for none
            TargaSaveOptions.resolution = TargaBitsPerPixels.TWENTYFOUR; // Options of SIXTEEN or THIRTYTWO
            TargaSaveOptions.rleCompression = true; // False for no compression
            activeDocument.saveAs(saveFile, TargaSaveOptions, true, Extension.LOWERCASE);
        }

    } else {
        alert("A document must be open to save a TGA file!");
    }

})();

 

Participant
August 11, 2023

Hi Stephen,  Thanks again, super helpful and saved me hours of trawling the web 🙂

 

It worked almost perfectly out the box and I was able to create the spec version from what you gave me.    I had to comment out throw null as it threw an error.   For the spec version I just added this line and swapped the File Destination accordingly. 

 

var newDocName = docName.replace("_", "_spec_");

 

Note for anyone else copying and pasting the folder path from explorer into this script.  You need to get the slashes going in the right direction!

 

Is it written in pure javascript or an Adobe variant?   

 

Thanks again,

Max

Stephen Marsh
Community Expert
Community Expert
August 11, 2023

@Max Pickle – I have updated the code to a 1.1 version which should hopefully remove the error if no is pressed to avoid overwriting an existing file. Would you be able to test and confirm?