Back to Programming Articles

Photoshop Script to Change Matte Color
by drews 06 Jan 2007

I created a Photoshop script that will open all the PSDs in a folder and Save To Web as .gif using a matte color you specify in hex out to a folder of your choice.

You need to specify the hex color in the code around line 52, just put in the value without the preceding "#" sign. Copy and paste the code to into a new text document and save it as <somename>.jsx .

Unfortunately, it doesn't do folder traversing so if you have multiple folders it will be necessary to do each folder individually. I created this script especially for use with the vBulletin PSDs for easy matting. It has been tested in Adobe Photoshop CS2.

Code below... let me know how you like it.

HTML Code:
// enable double clicking from the Macintosh Finder or the Windows Explorer
#target photoshop
///////////////////////////
//       SET-UP          //
///////////////////////////
// A list of file extensions to skip, keep them lower case
    gFilesToSkip = Array( "db", "xmp", "thm", "txt", "doc", "md0", "tb0", "adobebridgedb", "adobebridgedbt", "bc", "bct" );
// Pops open a dialog for the user to choose the folder of documents to process
    var inputFolder = Folder.selectDialog("Select a folder of documents to process");
// Pops open a dialog for the user to set the output folder
    var outputFolder = Folder.selectDialog("Select a folder for the output files");
///////////////////////////
//         MAIN          //
///////////////////////////
// Open Folder of Images    
    var numFiles = OpenFolder();
///////////////////////////
//       FUNCTIONS       //
///////////////////////////
main();
function main() {
    if (app.documents.length <= 0)
    {
        alert("You must have a document open to Save For Web");
        return;
    }
    try
    {
        for(var i = 0; i < numFiles; i++)
        {
            //alert("Save To Web");
            RunSaveForWeb();
            //alert("Closing");
            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }
    }
    catch (error)
    {
        if (error.number != 8007) // Don't report user cancelled errors.
            alert (error); //  + ":" + error.line);
    }
}
function RunSaveForWeb()
{
    //Define SaveForWeb Options
    var matteColorColor = new RGBColor();
    matteColorColor.hexValue = "000000";
    var sfwOptions = new ExportOptionsSaveForWeb();
    sfwOptions.format = SaveDocumentType.COMPUSERVEGIF;
    sfwOptions.includeProfile = false;
    sfwOptions.colors = 256;
    sfwOptions.matteColor = matteColorColor;
    sfwOptions.interlaced = 0;
    //Save for Web
    docPath = outputFolder;
    docName = activeDocument.name;
    var temp = new Array();
    temp = docName.split('.');
    sfwJpgFile = new File(docPath + "\\" + temp[0] + ".gif");
    activeDocument.exportDocument(sfwJpgFile, ExportType.SAVEFORWEB, sfwOptions);
}
// Given the a Folder of files, open them
function OpenFolder() 
{
    var filesOpened = 0;
    var fileList = inputFolder.getFiles();
    for (var i = 0; i < fileList.length; i++) 
    {
        // Make sure all the files in the folder are compatible with PS
        if (fileList[i] instanceof File && ! fileList[i].hidden && ! IsFileOneOfThese(fileList[i], gFilesToSkip))
        {
            open(fileList[i]);
            filesOpened++;
      }
    }
   return filesOpened;
}
// given a file name and a list of extensions
// determine if this file is in the list of extensions
function IsFileOneOfThese( inFileName, inArrayOfFileExtensions )
{
    var lastDot = inFileName.toString().lastIndexOf(".");
    if (lastDot == -1)
    {
        return false;
    }
    var strLength = inFileName.toString().length;
    var extension = inFileName.toString().substr(lastDot + 1, strLength - lastDot);
    extension = extension.toLowerCase();
    for (var i = 0; i < inArrayOfFileExtensions.length; i++ )
    {
        if ( extension == inArrayOfFileExtensions[i] )
        {
            return true;
        }
    }
    return false;
}

vblts.ru supports vBulletin®, 2022-2024