Layer to files script adding PNG support

Upload photoshop scripts, download photoshop scripts, discussion and support of photoshop scripts

Moderator: PS-Moderators

Layer to files script adding PNG support

Postby Mike Hale » Fri Nov 04, 2005 5:17 am

Someone at the Adobe forums ask how to add PNG support to the script that ships with Photoshop. Chris Cox replied in his normal fashion to just open the file and edit it. So I did to help that person out.

Here is that script with the requested changes. It now supports PNG file type with or without interlace.

Fixed PNG interlace option checkbox not hiding if you change from PNG to another format.

Someone at Adobe Exchange commented that this doesn't support transparent PNG files. It does now. I don't normally use PNG files, so didn't know that was a format feature.
Attachments
Export Layers To Files_with PNG.1.zip
now supports transparent png
(6.19 KiB) Downloaded 2438 times
Mike Hale
Site Admin
 
Posts: 4337
Joined: Fri Sep 30, 2005 10:52 pm
Location: USA

Postby mlk » Sun Dec 17, 2006 8:19 pm

The improved code supporting PNG didn't work with CS1 on my machine so I tweaked the older one and made it work (it replaces BMP and you can choose to trim the transparent pixels. I left the interlacing option out.)

The editing I did was quite sloppy, sorry =)

(save as .js)

Code: Select all
// Copyright 2003.  Adobe Systems, Incorporated.  All rights reserved.
// This script will export each layer in the document to a separate file.

// debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)
$.level = 0;
// debugger; // launch debugger on next line

//=================================================================
// Globals
//=================================================================

// UI strings to be localized
var uiTitleLayersToFiles = "Export Layers To Files";
var uiButtonRun = "Run";
var uiButtonCancel = "Cancel";
var uiHelpText = "Please specify the format and location for saving each layer as a file.";
var uiLabelDestination = "Destination:";
var uiButtonBrowse = "Browse...";
var uiLabelFileNamePrefix = "File Name Prefix:";
var uiCheckboxVisibleOnly = "Visible Layers Only";
var uiLabelFileType = "File Type:";
var uiCheckboxIncludeICCProfile = "Include ICC Profile";
var uiPanelJPEGOptions = "JPEG Options:";
var uiLabelQuality = "Quality:";
var uiPanelPSDOptions = "PSD Options:";
var uiCheckboxMaximizeCompatibility = "Maximize Compatibility";
var uiPanelTIFFOptions = "TIFF Options:";
var uiLabelImageCompression = "Image Compression:";
var uiRadiobuttonNone = "None";
var uiPanelPDFOptions = "PDF Options:";
var uiLabelEncoding = "Encoding:";
var uiPanelTargaOptions = "Targa Options:";
var uiLabelDepth = "Depth:";
var uiRadiobutton16bit = "16bit";
var uiRadiobutton24bit = "24bit";
var uiRadiobutton32bit = "32bit";
var uiPanelPNGOptions = "PNG Options:";
var uiAlertSpecifyDestination = "Please specify destination.";
var uiAlertDestinationNotExist = "Destination does not exist.";
var uiTitleSelectDestination = "Select Destination";
var uiAlertDocumentMustBeOpened = "You must have a document open to export!";
var uiAlertNeedMultipleLayers = "You need a document with multiple layers to export!";
var uiAlertWasSuccessful = " was successful.";
var uiAlertFaild = " failed.";

//=================================================================
// Functions
//=================================================================

// main dialog
function settingDialog(exportInfo)
{
    var dlgW = 390; // dialog width
    var dlgH = 405; // dialog height
    var btnW = 80;  // botton width
    var btnH = 20;  // botton height
    var txtW = 270; // text width
    var txtH = 20;  // text height
    var mgn  = 10;  // margin
    var pnlW = txtW;// panel width
    var pnlH = 190; // panel height
    var rb1W = 70;  // radio botton width level1
    var rb2W = 55;  // radio botton width level2
    var rbH = 20;   // radio botton height
    var labelW = 60;    // JPEG quality label width
    var editW = 25; // JPEG quality edit width

    var bounds;
    var yLayout = mgn+txtH; // scanning y layout

    bounds = {x:0, y:0, width:dlgW, height:dlgH};
    var w = new Window("dialog", uiTitleLayersToFiles, bounds );

    bounds = {x:dlgW-btnW-mgn, y:yLayout, width:btnW, height:btnH};
    w.btnRun = w.add("button", bounds, uiButtonRun);
    w.btnRun.onClick = btnRunOnClick;

    yLayout += btnH + mgn;
    bounds = {x:dlgW-btnW-mgn, y:yLayout, width:btnW, height:btnH};
    w.btnCancel = w.add("button", bounds, uiButtonCancel);
    w.btnCancel.onClick = function() { this.parent.close(0); };

    yLayout = mgn;  // reset

    bounds = {x:mgn, y:yLayout, width:180, height:txtH}; // if the bounds is too wide, "browse" button does not work.
    w.add("statictext", bounds, uiLabelDestination);

    yLayout += txtH;
    bounds = {x:mgn, y:yLayout, width:txtW-btnW-mgn-5, height:txtH}; // 5px smaller
    w.etDestination   = w.add("edittext", bounds, exportInfo.destination.toString());

    bounds = {x:mgn+txtW-btnW-5, y:yLayout, width:btnW+5, height:btnH}; // 5px wider
    w.btnBrowse= w.add("button", bounds, uiButtonBrowse);
    w.btnBrowse.onClick = btnBrowseOnClick;

    yLayout += txtH + mgn;
    bounds = {x:mgn, y:yLayout, width:txtW, height:txtH};
    w.add("statictext", bounds, uiLabelFileNamePrefix);

    yLayout += txtH;
    bounds = {x:mgn, y:yLayout, width:txtW, height:txtH};
    w.etFileNamePrefix = w.add("edittext", bounds, exportInfo.fileNamePrefix.toString());

    yLayout += txtH + mgn;
    bounds = {x:mgn, y:yLayout, width:txtW, height:txtH};
    w.cbVisible = w.add( "checkbox", bounds, uiCheckboxVisibleOnly);
    exportInfo.visibleOnly;
    w.cbVisible.value = 1;

    yLayout += txtH + mgn;
    bounds = {x:mgn, y:yLayout, width:pnlW, height:pnlH};
    w.pnlFT = w.add( "panel", bounds, uiLabelFileType );

    bounds = {x:mgn, y:mgn*2, width:rb1W, height:rbH};
    w.pnlFT.rbJpg = w.pnlFT.add( "radiobutton", bounds, "JPEG" );
    w.pnlFT.rbJpg.onClick = rbJpgOnClick;

    bounds = {x:mgn*2+rb1W, y:mgn*2, width:rb1W, height:rbH};
    w.pnlFT.rbPsd = w.pnlFT.add( "radiobutton", bounds, "PSD" );
    w.pnlFT.rbPsd.onClick = rbPsdOnClick;

    bounds = {x:mgn*3+rb1W*2, y:mgn*2, width:rb1W, height:rbH};
    w.pnlFT.rbTif = w.pnlFT.add( "radiobutton", bounds, "TIFF" );
    w.pnlFT.rbTif.onClick = rbTifOnClick;

    bounds = {x:mgn, y:mgn*2+rbH, width:rb1W, height:rbH};
    w.pnlFT.rbPdf = w.pnlFT.add( "radiobutton", bounds, "PDF" );
    w.pnlFT.rbPdf.onClick = rbPdfOnClick;

    bounds = {x:mgn*2+rb1W, y:mgn*2+rbH, width:rb1W, height:rbH};
    w.pnlFT.rbTrg = w.pnlFT.add( "radiobutton", bounds, "Targa" );
    w.pnlFT.rbTrg.onClick = rbTrgOnClick;

    bounds = {x:mgn*3+rb1W*2, y:mgn*2+rbH, width:rb1W, height:rbH};
    w.pnlFT.rbPng = w.pnlFT.add( "radiobutton", bounds, "PNG" );
    w.pnlFT.rbPng.onClick = rbPngOnClick;

    bounds = {x:mgn, y:mgn*2+rbH*2, width:180, height:txtH};
    w.pnlFT.cbIcc = w.pnlFT.add( "checkbox", bounds, uiCheckboxIncludeICCProfile);
    w.pnlFT.cbIcc.value = exportInfo.icc;

    var boundsPanelOptions = {x:mgn, y:mgn*2+rbH*2+txtH, width:pnlW-mgn*2, height:pnlH-(mgn*3+rbH*2+txtH)};
    w.pnlFT.pnlJpg = w.pnlFT.add( "panel", boundsPanelOptions, uiPanelJPEGOptions );
    bounds = {x:mgn, y:mgn*2+3, width:labelW, height:txtH};
    w.pnlFT.pnlJpg.add("statictext", bounds, uiLabelQuality);
    bounds = {x:mgn+labelW, y:mgn*2, width:editW, height:txtH};
    w.pnlFT.pnlJpg.etQuality = w.pnlFT.pnlJpg.add("edittext", bounds, exportInfo.jpegQuality.toString());

    w.pnlFT.pnlPsd = w.pnlFT.add( "panel", boundsPanelOptions, uiPanelPSDOptions );
    bounds = {x:mgn, y:mgn*2, width:180, height:txtH};
    w.pnlFT.pnlPsd.cbMax = w.pnlFT.pnlPsd.add( "checkbox", bounds, uiCheckboxMaximizeCompatibility);
    w.pnlFT.pnlPsd.cbMax.value = exportInfo.psdMaxComp;
   
    w.pnlFT.pnlTif = w.pnlFT.add( "panel", boundsPanelOptions, uiPanelTIFFOptions );
    bounds = {x:mgn, y:mgn*2, width:180, height:txtH};
    w.pnlFT.pnlTif.add("statictext", bounds, uiLabelImageCompression);
    bounds = {x:mgn, y:mgn*2+txtH, width:rb2W, height:rbH};
    w.pnlFT.pnlTif.rbNone = w.pnlFT.pnlTif.add( "radiobutton", bounds, uiRadiobuttonNone);
    w.pnlFT.pnlTif.rbNone.onClick = rbDiableJpegQuality;
    bounds = {x:mgn*2+rb2W, y:mgn*2+txtH, width:rb2W, height:rbH};
    w.pnlFT.pnlTif.rbLzw = w.pnlFT.pnlTif.add( "radiobutton", bounds, "LZW");
    w.pnlFT.pnlTif.rbLzw.onClick = rbDiableJpegQuality;
    bounds = {x:mgn*2+rb2W*2, y:mgn*2+txtH, width:rb2W, height:rbH};
    w.pnlFT.pnlTif.rbZip = w.pnlFT.pnlTif.add( "radiobutton", bounds, "ZIP");
    w.pnlFT.pnlTif.rbZip.onClick = rbDiableJpegQuality;
    bounds = {x:mgn*2+rb2W*3, y:mgn*2+txtH, width:rb2W, height:rbH};
    w.pnlFT.pnlTif.rbJpeg = w.pnlFT.pnlTif.add( "radiobutton", bounds, "JPEG");
    w.pnlFT.pnlTif.rbJpeg.onClick = rbEnableJpegQuality;
    bounds = {x:mgn, y:mgn*3+txtH*2, width:labelW, height:txtH};
    w.pnlFT.pnlTif.stQuality = w.pnlFT.pnlTif.add("statictext", bounds, uiLabelQuality);
    bounds = {x:mgn+labelW, y:mgn*3+txtH*2, width:editW, height:txtH};
    w.pnlFT.pnlTif.etQuality = w.pnlFT.pnlTif.add("edittext", bounds, exportInfo.tiffJpegQuality.toString());

    switch (exportInfo.tiffCompression) {
        case TIFFEncoding.NONE:     w.pnlFT.pnlTif.rbNone.value = true; break;
        case TIFFEncoding.TIFFLZW:  w.pnlFT.pnlTif.rbLzw.value  = true; break;
        case TIFFEncoding.TIFFZIP:  w.pnlFT.pnlTif.rbZip.value  = true; break;
        case TIFFEncoding.JPEG:     w.pnlFT.pnlTif.rbJpeg.value = true; break;
        default: w.pnlFT.pnlTif.rbNone.value = true;    break;
    }
    if (TIFFEncoding.JPEG != exportInfo.tiffCompression) { // if not JPEG
        w.pnlFT.pnlTif.stQuality.enabled = false;
        w.pnlFT.pnlTif.etQuality.enabled = false;
    }

    w.pnlFT.pnlPdf = w.pnlFT.add( "panel", boundsPanelOptions, uiPanelPDFOptions );
    bounds = {x:mgn, y:mgn*2, width:100, height:txtH};
    w.pnlFT.pnlPdf.add("statictext", bounds, uiLabelEncoding);
    bounds = {x:mgn, y:mgn*2+txtH, width:rb2W, height:rbH};
    w.pnlFT.pnlPdf.rbZip = w.pnlFT.pnlPdf.add( "radiobutton", bounds, "ZIP");
    w.pnlFT.pnlPdf.rbZip.onClick = rbDiableJpegQuality;
    bounds = {x:mgn*2+rb2W, y:mgn*2+txtH, width:rb2W, height:rbH};
    w.pnlFT.pnlPdf.rbJpeg = w.pnlFT.pnlPdf.add( "radiobutton", bounds, "JPEG");
    w.pnlFT.pnlPdf.rbJpeg.onClick = rbEnableJpegQuality;
    bounds = {x:mgn, y:mgn*3+txtH+rbH, width:labelW, height:rbH};
    w.pnlFT.pnlPdf.stQuality = w.pnlFT.pnlPdf.add("statictext", bounds, uiLabelQuality);
    bounds = {x:mgn+labelW, y:mgn*3+txtH+rbH, width:editW, height:txtH};
    w.pnlFT.pnlPdf.etQuality = w.pnlFT.pnlPdf.add("edittext", bounds, exportInfo.pdfJpegQuality.toString());//

    switch (exportInfo.pdfEncoding) {
        case PDFEncoding.PDFZIP: w.pnlFT.pnlPdf.rbZip.value  = true;    break;
        case PDFEncoding.JPEG:   w.pnlFT.pnlPdf.rbJpeg.value = true;    break;
        default: w.pnlFT.pnlPdf.rbJpeg.value = true;    break;
    }
    if (PDFEncoding.JPEG != exportInfo.pdfEncoding) { // if not JPEG
        w.pnlFT.pnlPdf.stQuality.enabled = false;
        w.pnlFT.pnlPdf.etQuality.enabled = false;
    }

    w.pnlFT.pnlTrg = w.pnlFT.add( "panel", boundsPanelOptions, uiPanelTargaOptions );
    bounds = {x:mgn, y:mgn*2, width:180, height:txtH};
    w.pnlFT.pnlTrg.add("statictext", bounds, uiLabelDepth);
    bounds = {x:mgn, y:mgn*2+txtH, width:rb2W, height:rbH};
    w.pnlFT.pnlTrg.rb16bit = w.pnlFT.pnlTrg.add( "radiobutton", bounds, uiRadiobutton16bit);
    bounds = {x:mgn*2+rb2W, y:mgn*2+txtH, width:rb2W, height:rbH};
    w.pnlFT.pnlTrg.rb24bit = w.pnlFT.pnlTrg.add( "radiobutton", bounds, uiRadiobutton24bit);
    bounds = {x:mgn*3+rb2W*2, y:mgn*2+txtH, width:rb2W, height:rbH};
    w.pnlFT.pnlTrg.rb32bit = w.pnlFT.pnlTrg.add( "radiobutton", bounds, uiRadiobutton32bit);

    switch (exportInfo.targaDepth) {
        case TargaBitsPerPixels.SIXTEEN:     w.pnlFT.pnlTrg.rb16bit.value = true;   break;
        case TargaBitsPerPixels.TWENTYFOUR:  w.pnlFT.pnlTrg.rb24bit.value = true;   break;
        case TargaBitsPerPixels.THIRTYTWO:   w.pnlFT.pnlTrg.rb32bit.value = true;   break;
        default: w.pnlFT.pnlTrg.rb24bit.value = true;   break;
    }

    w.pnlFT.pnlPng = w.pnlFT.add( "panel", boundsPanelOptions, uiPanelPNGOptions );
    bounds = {x:mgn, y:mgn*2, width:180, height:txtH};
    w.pnlFT.pnlPng.cbMax = w.pnlFT.pnlPng.add( "checkbox", bounds, "Trim transparency");
    w.pnlFT.pnlPng.cbMax.value = 1;
    bounds = {left:mgn, top:dlgH-mgn-50, right:dlgW-mgn, bottom:dlgH-mgn};
    w.pnlHelp = w.add( "panel", bounds, "" );
    bounds = {x:mgn, y:mgn, width:dlgW-mgn*4, height:30};
    w.pnlHelp.add("statictext", bounds, uiHelpText, {multiline:true});

    hideAllFileTypePanel(w.pnlFT);
    switch (exportInfo.fileType) {  // default setting
        case 1:  w.pnlFT.rbJpg.value = true;    w.pnlFT.pnlJpg.visible = true;  break;
        case 2:  w.pnlFT.rbPsd.value = true;    w.pnlFT.pnlPsd.visible = true;  break;
        case 3:  w.pnlFT.rbTif.value = true;    w.pnlFT.pnlTif.visible = true;  break;
        case 4:  w.pnlFT.rbPdf.value = true;    w.pnlFT.pnlPdf.visible = true;  break;
        case 5:  w.pnlFT.rbTrg.value = true;    w.pnlFT.pnlTrg.visible = true;  break;
        case 6:  w.pnlFT.rbPng.value = true;    w.pnlFT.pnlPng.visible = true;  break;
        default: w.pnlFT.rbPsd.value = true;    w.pnlFT.pnlPsd.visible = true;  break;
    }
   
    w.center(); // centering dialog
    var result = w.show();
    if ( 0 == result)   return result;  // close to quit
   
    // get setting from dialog
    exportInfo.destination = w.etDestination.text;
    exportInfo.fileNamePrefix = w.etFileNamePrefix.text;
    exportInfo.visibleOnly = w.cbVisible.value;
    exportInfo.pngtrim = w.pnlFT.pnlPng.cbMax.value;
    if (w.pnlFT.rbJpg.value) exportInfo.fileType = 1;
    if (w.pnlFT.rbPsd.value) exportInfo.fileType = 2;
    if (w.pnlFT.rbTif.value) exportInfo.fileType = 3;
    if (w.pnlFT.rbPdf.value) exportInfo.fileType = 4;
    if (w.pnlFT.rbTrg.value) exportInfo.fileType = 5;
    if (w.pnlFT.rbPng.value) exportInfo.fileType = 6;
    exportInfo.icc = w.pnlFT.cbIcc.value;
    exportInfo.jpegQuality = w.pnlFT.pnlJpg.etQuality.text;
    exportInfo.psdMaxComp = w.pnlFT.pnlPsd.cbMax.value;
    if (w.pnlFT.pnlTif.rbNone.value) exportInfo.tiffCompression = TIFFEncoding.NONE;
    if (w.pnlFT.pnlTif.rbLzw.value)  exportInfo.tiffCompression = TIFFEncoding.TIFFLZW;
    if (w.pnlFT.pnlTif.rbZip.value)  exportInfo.tiffCompression = TIFFEncoding.TIFFZIP;
    if (w.pnlFT.pnlTif.rbJpeg.value) exportInfo.tiffCompression = TIFFEncoding.JPEG;
    exportInfo.tiffJpegQuality = w.pnlFT.pnlTif.etQuality.text;
    if (w.pnlFT.pnlPdf.rbZip.value)  exportInfo.pdfEncoding = PDFEncoding.PDFZIP;
    if (w.pnlFT.pnlPdf.rbJpeg.value) exportInfo.pdfEncoding = PDFEncoding.JPEG;
    exportInfo.pdfJpegQuality = w.pnlFT.pnlPdf.etQuality.text;
    if (w.pnlFT.pnlTrg.rb16bit.value)  exportInfo.targaDepth = TargaBitsPerPixels.SIXTEEN;
    if (w.pnlFT.pnlTrg.rb24bit.value)  exportInfo.targaDepth = TargaBitsPerPixels.TWENTYFOUR;
    if (w.pnlFT.pnlTrg.rb32bit.value)  exportInfo.targaDepth = TargaBitsPerPixels.THIRTYTWO;

    return result;
}


function hideAllFileTypePanel(panel)    // call back function
{
    panel.pnlJpg.visible = false;
    panel.pnlPsd.visible = false;
    panel.pnlTif.visible = false;
    panel.pnlPdf.visible = false;
    panel.pnlTrg.visible = false;
    panel.pnlPng.visible = false;
}

function rbJpgOnClick(exportInfo)   // call back function
{
    hideAllFileTypePanel(this.parent);
    this.parent.pnlJpg.visible = true;
    this.parent.cbIcc.enabled = true;
}

function rbPsdOnClick() // call back function
{
    hideAllFileTypePanel(this.parent);
    this.parent.pnlPsd.visible = true;
    this.parent.cbIcc.enabled = true;
}

function rbTifOnClick() // call back function
{
    hideAllFileTypePanel(this.parent);
    this.parent.pnlTif.visible = true;
    this.parent.cbIcc.enabled = true;
}

function rbPdfOnClick() // call back function
{
    hideAllFileTypePanel(this.parent);
    this.parent.pnlPdf.visible = true;
    this.parent.cbIcc.enabled = true;
}

function rbTrgOnClick() // call back function
{
    hideAllFileTypePanel(this.parent);
    this.parent.pnlTrg.visible = true;
    this.parent.cbIcc.enabled = false;
}

function rbPngOnClick() // call back function
{
    hideAllFileTypePanel(this.parent);
    this.parent.pnlPng.visible = true;
    this.parent.cbIcc.enabled = false;
}

function rbDiableJpegQuality()  // call back function
{
    this.parent.etQuality.enabled = false;
    this.parent.stQuality.enabled = false;
}

function rbEnableJpegQuality()  // call back function
{
    this.parent.etQuality.enabled = true;
    this.parent.stQuality.enabled = true;
}

function btnRunOnClick()    // call back function
{
    // check if the setting is properly
    var destination = this.parent.etDestination.text;
    if (destination.length == 0) {
        alert(uiAlertSpecifyDestination);
        return;
    }
    var testFolder = new Folder(destination);
    if (!testFolder.exists) {
        alert(uiAlertDestinationNotExist);
        return;
    }
   
    this.parent.close(1);
}

function btnBrowseOnClick() // call back function
{
    var defaultFolder = this.parent.etDestination.text;
    var testFolder = new Folder(this.parent.etDestination.text);
    if (!testFolder.exists) defaultFolder = "~";
    var selFolder = Folder.selectDialog(uiTitleSelectDestination, defaultFolder);
    if ( selFolder != null ) {
        this.parent.etDestination.text = selFolder.toString();
    }
    return;
}


// It carries dialog setting.
function initExportInfo(exportInfo)
{
    exportInfo.destination = new String("");
    exportInfo.fileNamePrefix = new String("untitled_");
    exportInfo.visibleOnly = false;
    exportInfo.fileType = 2; // JPEG=1, PSD=2, TIFF=3, PDF=4, Targa=5, BMP=6
    exportInfo.icc = true;
    exportInfo.jpegQuality = 8;
    exportInfo.psdMaxComp = true;
    exportInfo.tiffCompression = TIFFEncoding.NONE;
    exportInfo.tiffJpegQuality = 8;
    exportInfo.pdfEncoding = PDFEncoding.JPEG;
    exportInfo.pdfJpegQuality = 8;
    exportInfo.targaDepth = TargaBitsPerPixels.TWENTYFOUR;
//    exportInfo.bmpDepth = BMPDepthType.TWENTYFOUR;

    try {
        exportInfo.destination = app.activeDocument.fullName.parent; // destination folder
        var tmp = app.activeDocument.fullName.name;
        exportInfo.fileNamePrefix = decodeURI(tmp.substring(0, tmp.indexOf("."))); // filename body part
    } catch(someError) {
        exportInfo.destination = new String("");
        exportInfo.fileNamePrefix = app.activeDocument.name; // filename body part
    }
}


function saveFile( docRef, fileNameBody, exportInfo)
{
    switch (exportInfo.fileType) {
        case 1: // JPEG
            var saveFile = new File(exportInfo.destination + "/" + fileNameBody + ".jpg");
            jpgSaveOptions = new JPEGSaveOptions();
            jpgSaveOptions.embedColorProfile = exportInfo.icc;
            jpgSaveOptions.quality = exportInfo.jpegQuality;
            docRef.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
            break;
        case 2: // PSD
            var saveFile = new File(exportInfo.destination + "/" + fileNameBody + ".psd");
            psdSaveOptions = new PhotoshopSaveOptions();
            psdSaveOptions.embedColorProfile = exportInfo.icc;
            psdSaveOptions.maximizeCompatibility = exportInfo.psdMaxComp;
            docRef.saveAs(saveFile, psdSaveOptions, true, Extension.LOWERCASE);
            break;
        case 3: // TIFF
            var saveFile = new File(exportInfo.destination + "/" + fileNameBody + ".tif");
            tiffSaveOptions = new TiffSaveOptions();
            tiffSaveOptions.embedColorProfile = exportInfo.icc;
            tiffSaveOptions.imageCompression = exportInfo.tiffCompression;
            if (TIFFEncoding.JPEG == exportInfo.tiffCompression)    tiffSaveOptions.jpegQuality = exportInfo.tiffJpegQuality;
            docRef.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);
            break;
        case 4: // PDF
            var saveFile = new File(exportInfo.destination + "/" + fileNameBody + ".pdf");
            pdfSaveOptions = new PDFSaveOptions();
            pdfSaveOptions.embedColorProfile = exportInfo.icc;
            pdfSaveOptions.encoding = exportInfo.pdfEncoding;
            if (PDFEncoding.JPEG == exportInfo.pdfEncoding) pdfSaveOptions.jpegQuality = exportInfo.pdfJpegQuality;
            docRef.saveAs(saveFile, pdfSaveOptions, true, Extension.LOWERCASE);
            break;
        case 5: // Targa
            var saveFile = new File(exportInfo.destination + "/" + fileNameBody + ".tga");
            targaSaveOptions = new TargaSaveOptions();
            targaSaveOptions.resolution = exportInfo.targaDepth;
            docRef.saveAs(saveFile, targaSaveOptions, true, Extension.LOWERCASE);
            break;
        case 6: // PNG
            var saveFile = new File(exportInfo.destination + "/" + fileNameBody + ".png");
            pngSaveOptions = new PNGSaveOptions();
      if(exportInfo.pngtrim){
         var startDisplayDialogs = displayDialogs;
         displayDialogs = DialogModes.NO;
         docRef.trim(TrimType.TRANSPARENT,true,true,true,true);
         displayDialogs = startDisplayDialogs;
      }
            docRef.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);


            break;
        default:
            alert("Unexpected error");
            break;
    }
}


function zeroSuppress (num, digit)
{
    var tmp = num.toString();
    while(tmp.length < digit)   tmp = "0" + tmp;
    return tmp
}


function setInvisibleAllArtLayers(obj)  // obj can be DocRef or LayerSet
{
    for( var i = 0; i < obj.artLayers.length; i++) {
        obj.artLayers[i].allLocked = false; // unlock first
        obj.artLayers[i].visible = false;
    }
    for( var i = 0; i < obj.layerSets.length; i++) {
        setInvisibleAllArtLayers(obj.layerSets[i]); // recursive call
    }
}


function removeAllInvisibleArtLayers(obj)   // obj can be DocRef or LayerSet
{
    for( var i = obj.artLayers.length-1; 0 <= i; i--) {
        try {  // best effort
            if(!obj.artLayers[i].visible)   obj.artLayers[i].remove();
        } catch (e) {}
    }
    for( var i = obj.layerSets.length-1; 0 <= i; i--) {
        removeAllInvisibleArtLayers(obj.layerSets[i]);  // recursive call
    }
}


function removeAllEmptyLayerSets(obj)   // obj can be DocRef or LayerSet
{
    var foundEmpty = true;
    for( var i = obj.layerSets.length-1; 0 <= i; i--) {
        if( removeAllEmptyLayerSets(obj.layerSets[i])) {    // if empty
            obj.layerSets[i].remove();
        } else {
            foundEmpty = false; // it is not empty
        }
    }
    if (obj.artLayers.length > 0)   foundEmpty = false;
    return foundEmpty;  // return boolean / if empty
}


function removeAllInvisible(docRef)
{
    removeAllInvisibleArtLayers(docRef);
    removeAllEmptyLayerSets(docRef);
}


function exportChildren(dupObj, orgObj, exportInfo, dupDocRef, fileNamePrefix)  // obj can be DocRef or LayerSet
{
    for( var i = 0; i < dupObj.artLayers.length; i++) {
        if (exportInfo.visibleOnly) { // visible layer only
            if (!orgObj.artLayers[i].visible) continue;
        }
        dupObj.artLayers[i].visible = true;

        var layerName = dupObj.artLayers[i].name;  // store layer name before change doc
        var duppedDocumentTmp = dupDocRef.duplicate();
        if (2 == exportInfo.fileType || 6 == exportInfo.fileType) { // PSD: Keep transparency
            removeAllInvisible(duppedDocumentTmp);
        } else { // just flatten
            duppedDocumentTmp.flatten();
        }
        var fileNameBody = fileNamePrefix;
        fileNameBody += "_" + zeroSuppress(i, 4);
        fileNameBody += "_" + layerName;
        fileNameBody  = fileNameBody.replace(/\//g, "_");  // '/' -> '_'
        if (fileNameBody.length > 120) fileNameBody = fileNameBody.subString(0,120);
        saveFile(duppedDocumentTmp, fileNameBody, exportInfo);
        duppedDocumentTmp.close(SaveOptions.DONOTSAVECHANGES);

        dupObj.artLayers[i].visible = false;
    }
    for( var i = 0; i < dupObj.layerSets.length; i++) {
        if (exportInfo.visibleOnly) { // visible layer only
            if (!orgObj.layerSets[i].visible) continue;
        }
        var fileNameBody = fileNamePrefix;
        fileNameBody += "_" + zeroSuppress(i, 4) + "s";
        exportChildren(dupObj.layerSets[i], orgObj.layerSets[i], exportInfo, dupDocRef, fileNameBody);  // recursive call
    }
}


function main()
{
    if ( app.documents.length <= 0 ) {
        alert( uiAlertDocumentMustBeOpened );
        return;
    }

    var exportInfo = new Object();
    initExportInfo(exportInfo);
    if (0 == settingDialog(exportInfo)) return; // quit

    try {
        var docName = app.activeDocument.name;  // save the app.activeDocument name before duplicate.

        var layerCount = app.documents[docName].layers.length;
        var layerSetsCount = app.documents[docName].layerSets.length;

        if ((layerCount <= 1)&&(layerSetsCount <= 0)) {
            alert ( uiAlertNeedMultipleLayers );
        } else {
            app.activeDocument = app.documents[docName];
            var duppedDocument = app.activeDocument.duplicate();
            duppedDocument.activeLayer = duppedDocument.layers[duppedDocument.layers.length-1]; // for removing
            setInvisibleAllArtLayers(duppedDocument);
            exportChildren(duppedDocument, app.documents[docName], exportInfo, duppedDocument, exportInfo.fileNamePrefix);
            duppedDocument.close( SaveOptions.DONOTSAVECHANGES );
            alert(uiTitleLayersToFiles + uiAlertWasSuccessful);
        }
    } catch (e) {
        alert(e);
    }
}


//===============================================================
// Dispatch
//===============================================================

main();



mlk
 
Posts: 18
Joined: Tue May 24, 2005 4:42 pm
Location: London, United Kingdom

Postby number-six » Wed Jan 24, 2007 9:29 pm

There's a typo in mlk's version on line 536:

Code: Select all
if (fileNameBody.length > 120) fileNameBody = fileNameBody.subString(0,120);

// Should be:

if (fileNameBody.length > 120) fileNameBody = fileNameBody.substring(0,120);
number-six
 
Posts: 5
Joined: Mon Jan 22, 2007 9:33 pm

Re: Layer to files script adding PNG support

Postby areimer » Sun Jan 11, 2009 5:03 am

Mike Hale wrote:Someone at the Adobe forums ask how to add PNG support to the script that ships with Photoshop. Chris Cox replied in his normal fashion to just open the file and edit it. So I did to help that person out.

Here is that script with the requested changes. It now supports PNG file type with or without interlace.

Fixed PNG interlace option checkbox not hiding if you change from PNG to another format.

Someone at Adobe Exchange commented that this doesn't support transparent PNG files. It does now. I don't normally use PNG files, so didn't know that was a format feature.


Thank you SO much for creating this and sharing it. It is just what I need, especially with the transparencies.
areimer
 
Posts: 1
Joined: Sat Jan 10, 2009 10:27 pm

Export Layers To Files - (Fast PNG version)

Postby rpoland » Wed Aug 19, 2009 12:41 am

Photoshop CS3, Mac OS 10.5.8.

This works great but for my application I wish that in addition to the no prefix there was an option to not have leading numbers.
rpoland
 
Posts: 1
Joined: Tue Aug 18, 2009 11:58 pm
Location: Fort Collins, CO

Postby Mike Hale » Wed Aug 19, 2009 1:27 am

This is an old script. The CS4 version that ships with Photoshop has png8, png24 support and an option to trim transparent.

I think that the reason for the numbers is to avoiding dealing with more than one layer with the same name. If you are sure that you docs always have unique layer names you can hardcode the removal of the numbers by commenting out // this line.

Code: Select all
fileNameBody += "_" + zeroSuppress(i, 4);
Mike Hale
Site Admin
 
Posts: 4337
Joined: Fri Sep 30, 2005 10:52 pm
Location: USA


Return to Photoshop Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

cron