Skip to main content
Participant
November 6, 2017
Answered

looking for a save as Targa script

  • November 6, 2017
  • 1 reply
  • 2245 views

Anyone could help me script a SAVE AS .TGA  file in a folder?

Reason I can't use action is that the tag file needs a new suffix (ex: image_01.tga... image_02.tga... image_03.tga)

I'm saving dozens of Targas everyday. so please please the one who could help me would be my hero for the rest of the year.

in short:

PSD file with layers, ---> save as .tga file with suffix 32 bit.

Thanks a million!

/Dominique

This topic has been closed for replies.
Correct answer SuperMerlin

Please give this a try...

#target photoshop

main();

function main(){

if(!documents.length) return;

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

Name = Name.replace(/\d+$/,'');

Name = Name.replace(/_$/,'');

try{

var savePath = activeDocument.path;

}catch(e){

    alert("You must save this document first!");

    }

var fileList= savePath.getFiles(Name +"*.tga").sort().reverse();

var Suffix = 0;

if(fileList.length){

    Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));

}

Suffix= zeroPad(Suffix + 1, 3);

var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".tga");

saveTarga32(saveFile);

}

function zeroPad(n, s) {

   n = n.toString();

   while (n.length < s)  n = '0' + n;

   return n;

};

function saveTarga32(saveFile){

targaSaveOptions = new TargaSaveOptions();

targaSaveOptions.alphaChannels = true;

targaSaveOptions.resolution = TargaBitsPerPixels.THIRTYTWO;

activeDocument.saveAs(File(saveFile), targaSaveOptions, true, Extension.LOWERCASE);

};

1 reply

SuperMerlin
SuperMerlinCorrect answer
Inspiring
November 6, 2017

Please give this a try...

#target photoshop

main();

function main(){

if(!documents.length) return;

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');

Name = Name.replace(/\d+$/,'');

Name = Name.replace(/_$/,'');

try{

var savePath = activeDocument.path;

}catch(e){

    alert("You must save this document first!");

    }

var fileList= savePath.getFiles(Name +"*.tga").sort().reverse();

var Suffix = 0;

if(fileList.length){

    Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));

}

Suffix= zeroPad(Suffix + 1, 3);

var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".tga");

saveTarga32(saveFile);

}

function zeroPad(n, s) {

   n = n.toString();

   while (n.length < s)  n = '0' + n;

   return n;

};

function saveTarga32(saveFile){

targaSaveOptions = new TargaSaveOptions();

targaSaveOptions.alphaChannels = true;

targaSaveOptions.resolution = TargaBitsPerPixels.THIRTYTWO;

activeDocument.saveAs(File(saveFile), targaSaveOptions, true, Extension.LOWERCASE);

};

JJMack
Community Expert
Community Expert
November 6, 2017

You may want to add  an if to that script.

if (app.activeDocument.bitsPerChannel==BitsPerChannelType.SIXTEEN) {

  alert("TGA does not support 16 bit color!"); 

  return;

JJMack