Skip to main content
Participating Frequently
May 1, 2020
Question

Script and extract layer to file

  • May 1, 2020
  • 3 replies
  • 3572 views

Hi 🙂

 

I have made a script to export current layers to PNG / JPG BLACK and PNG WHITE

ie: layer1 is extracted to layer1-black.jpg, layer1-black.png, layer1-white.png

most of time i have 3 layers like this

 

At the end, my folder contain my working PSD and many JPG and PNG

 

The script works fine but photoshop export always in the same folder and I don't want it to do that

I want photoshop export files in the folder where my current PSD file is

 

Is there a way to ask photoshop to export in current folder? like a command in the destinaton field? (capture attached)

 

I hope I'm clear...

Thank's

Joachim

This topic has been closed for replies.

3 replies

Stephen Marsh
Community Expert
Community Expert
May 8, 2020

Perhaps posting the code would be a good idea?

JJMack
Community Expert
Community Expert
May 1, 2020

If the current document  has been saved there is a backing PSD files in some folder on your system. Your script should have no Problem saving your PNG and Jpeg files into that folder. 

 

You can instead use Adobe Script menu File>Export>Layers to Files... and use its interactive dialog to choose the output folder.

JJMack
Participating Frequently
May 1, 2020

Hi, yes when i make export layer to file with the dialog box, i can select destination but when you make this included in a script, you can't choose the folder, and photoshop saved the folder of the current PSD when I created the script...

That's why I'm looking for how to save in the current folder when using this script and not always in the same folder...

I hope I'm clear 😛 It is hard to explain

JJMack
Community Expert
Community Expert
May 1, 2020

You stated you want the script to save the output files in the same folder the PSD is in.  You need to use the export Layers to Files functions to bypass its dialog.  Its a Plug-in script you can see that in the script code there an // BEGIN__HARVEST_EXCEPTION_ZSTRING section.  So if you record it in an action the action will hard code the output folder into the action step and when the action is played the action will pass the output folders and all the other dialog settings to the script and the script will use these settings and not display its dialog so the cation can be used is a batch process,  This is exactly what you want to do in your Script but you want to pass a different output folder for each PSD processed. So that is what you do in your script.    Here is a script the will pass the same folder all the time.  However, I made the export function have an output parameter.  All the other export layer to files dialog setting are hard coded the way I wanted. The function should give you some food for thought.  It saves gpg files.

 

var exportFolder = "~/Desktop/exportedlayers";
var makeFolder = new Folder(exportFolder);
makeFolder.create();

exjpg(exportFolder, activeDocument.name);

function exjpg(saveTo, preFix) {
  // Export Layers to Files
    var desc1 = new ActionDescriptor();
    desc1.putString(app.charIDToTypeID('Msge'), "Export Layers To Files action settings");
    desc1.putString(app.stringIDToTypeID("destination"), saveTo);
    desc1.putString(app.stringIDToTypeID("fileNamePrefix"), preFix);
    desc1.putBoolean(app.stringIDToTypeID("visibleOnly"), false);
    desc1.putDouble(app.charIDToTypeID('FlTy'), 1);
    desc1.putBoolean(app.stringIDToTypeID("icc"), true);
    desc1.putString(app.stringIDToTypeID("jpegQuality"), "10");
    desc1.putBoolean(app.stringIDToTypeID("psdMaxComp"), true);
    desc1.putString(app.stringIDToTypeID("tiffCompression"), "TIFFEncoding.NONE");
    desc1.putString(app.stringIDToTypeID("tiffJpegQuality"), "8");
    desc1.putString(app.stringIDToTypeID("pdfEncoding"), "PDFEncoding.JPEG");
    desc1.putString(app.stringIDToTypeID("pdfJpegQuality"), "8");
    desc1.putString(app.stringIDToTypeID("targaDepth"), "TargaBitsPerPixels.TWENTYFOUR");
    desc1.putString(app.stringIDToTypeID("bmpDepth"), "BMPDepthType.TWENTYFOUR");
    desc1.putBoolean(app.stringIDToTypeID("png24Transparency"), true);
    desc1.putBoolean(app.stringIDToTypeID("png24Interlaced"), false);
    desc1.putBoolean(app.stringIDToTypeID("png24Trim"), false);
    desc1.putBoolean(app.stringIDToTypeID("png8Transparency"), true);
    desc1.putBoolean(app.stringIDToTypeID("png8Interlaced"), false);
    desc1.putBoolean(app.stringIDToTypeID("png8Trim"), true);
    executeAction(app.stringIDToTypeID('6f1c2cf5-4a97-4e32-8f59-f5d7a087adef'), desc1, DialogModes.NO);
};

 

 

 

JJMack
Charu Rajput
Community Expert
Community Expert
May 1, 2020

Hi,

You can fetch of the path of the document as

 

 

var psdPath = app.activeDocument.path;

 

 

Above statement will give you the apth of the document which is current open.

 

 

if (psdPath) {
    var folder = Folder(psdPath);
    var folderPath = folder.selectDlg("Select a folder to save the files");
}else{
    var folderPath = Folder.selectDialog("Select a folder to save the files");
}

 

 

selectDlg will open the path where psd document is saved.

 

I hope this helps you.

 

Thanks

Charu

Best regards
Participating Frequently
May 1, 2020

Hi , thank s for your answer, what can I do with this? sorry i don't understand.. 😕😕

Charu Rajput
Community Expert
Community Expert
May 1, 2020

Hi,

Since you have said you have written a script to export layers. So in your script you should show dialog to select the path for teh exported images. And you want that path must be the path of current document. So, you need to create a simple dialog, using Adobe ScriptUI

 

Please try below sample code how it will work. This below code show you dialog and allow you to choose the folder on clcik of "Select" buton and if the photoshop document is saved, by default it will have path for that document.

 

#target photoshop
function showDialog() {

    var dlg = new Window("dialog", "Sample Dialog");
    try {
        var psdPath = app.activeDocument.path;
    } catch (e) {

    }

    row = dlg.add('group', undefined, '');
    row.add('statictext', undefined, "Path: ");
    var pathInput = row.add('edittext', undefined, "");
    pathInput.enabled = false;
    pathInput.preferredSize = [350, 22];
    if (psdPath)
        pathInput.text = decodeURI(psdPath);

    //Icon button
    var selectButton = row.add('button', undefined, 'Select', {
        name: 'Select'
    });

    selectButton.onClick = function () {
        if (psdPath) {
            var folder = Folder(psdPath);
            var folderPath = folder.selectDlg("Select a folder to save the files");
        } else {
            var folderPath = Folder.selectDialog("Select a folder to save the files");
        }
        if (folderPath) {
            pathInput.text = decodeURI(folderPath.fsName);
            goButton.enabled = true;
        }
    };

    var buttonGroup = dlg.add('group', undefined, '');
    buttonGroup.alignment = 'right';
    var closeButton = buttonGroup.add('button', undefined, 'Close', {
        name: 'Close'
    });

    closeButton.onClick = function () {
        dlg.close();
    };

    dlg.show();

}

showDialog();

Before running the above snippet, open your current photoshop document so that it amkes you clear what exactly needs to be done.

 

Let me know if this helps you to understand.

Thanks

Charu 

Best regards