Skip to main content
Known Participant
September 13, 2009
Answered

Export Groups to Files in Folders

  • September 13, 2009
  • 1 reply
  • 5396 views

I'm following up on the great script posted here:

http://forums.adobe.com/thread/488255?tstart=0

I'm trying to add 2 things

  • Save using ExportType.SAVEFORWEB
  • Display an options box in the dialog to choose whether to save the files in separate folders named after the group names

But I cannot get any of those to work.

Any ideas how to do it?

Thanks!

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Yep that works great Chris! Wunderbar

I'm testing it here with a PSD which at top level has graphical layers and language layer groups containing the translated texts

As things are now, to have it export the complete localized versions, I should duplicate the graphic layers for each language and move the copies inside the groups.

Would there be a way to have it to export the graphical layers and alternate the language layergroups?

It looks a bit like this:

- button

- image

--- [english]

--- [german]

--- [french]

- gradient

- background


I’ve added a clause to show only one of the selected layerSets at a copy and leave the rest of the layers’ visibility unchanged … maybe it works for Your purpose:

// creates save for web-jpg-copies of mutually hidden layersets with dialog to select layersets and suffix;

// be advised: existing files of identical names will be overwritten without warnings;

// 2009, pfaffenbichler, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

////////////////////////////////////

// get document-path and -title;

var myDocument = app.activeDocument;

var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];

var myPath = myDocument.path;

var theLayerSets = collectLayerSets(myDocument);

////// filter for checking if entry is numeric, thanks to xbytor //////

numberKeystrokeFilter = function() {

this.text = this.text.replace(",", "");

this.text = this.text.replace(".", "");

if (this.text.match(/[^\-\.\d]/)) {

this.text = this.text.replace(/[^\-\.\d]/g, "");

};

if (this.text == "") {

this.text = "0"

}

};

////////////////////////////////////

// create the dialog;

var dlg = new Window('dialog', "pdfs from layersets", [500,300,930,870]);

//create list for layer-selection;

dlg.layerRange = dlg.add('panel', [21,20,279,475], "select layersets to create pdfs of");

dlg.layerRange.layersList = dlg.layerRange.add('listbox', [11,20,240,435], '', {multiselect: true});

for (var q = 0; q < theLayerSets.length; q++) {

dlg.layerRange.layersList.add ("item", theLayerSets.name);

dlg.layerRange.layersList.items.selected = true

};

// entry for suffix;

dlg.suffix = dlg.add('panel', [290,20,410,85], "enter suffix");

dlg.suffix.suffixText = dlg.suffix.add('edittext', [11,20,99,40], "", {multiline:false});

// entry for number;

dlg.number = dlg.add('panel', [290,100,410,165], "start with #");

dlg.number.startNumber = dlg.number.add('edittext', [11,20,45,40], "1", {multiline:false});

dlg.number.addNumber = dlg.number.add('checkbox', [55,20,105,40], "add", {multiline:false});

dlg.number.startNumber.onChange = numberKeystrokeFilter;

// field to add layer-name;

dlg.layerName = dlg.add('panel', [290,180,410,270], "layer-name");

dlg.layerName.doAddName = dlg.layerName.add('radiobutton', [11,20,120,40], "add it");

dlg.layerName.dontAddName = dlg.layerName.add('radiobutton', [11,45,120,65], "don’t add it");

dlg.layerName.doAddName.value = true;

// field to select target-folder;

dlg.target = dlg.add('panel', [290,285,410,475], "target-folder");

dlg.target.folderPerSet = dlg.target.add('checkbox', [11,20,100,40], "folder each");

dlg.target.targetSel = dlg.target.add('button', [11,50,100,70], "select");

dlg.target.targetField = dlg.target.add('statictext', [11,80,100,185], String(myPath), {multiline:true});

dlg.target.targetSel.onClick = function () {

var target = Folder.selectDialog("select a target folder");

dlg.target.targetField.text = target.fsName

};

// ok- and cancel-buttons;

dlg.buildBtn = dlg.add('button', [220,490,410,505], 'OK', {name:'ok'});

dlg.cancelBtn = dlg.add('button', [21,490,210,505], 'Cancel', {name:'cancel'});

dlg.warning = dlg.add('statictext', [21,520,410,560], "be advised: existing files of the same name will be replaced without prompting", {multiline: true});

dlg.center();

////////////////////////////////////

var myReturn = dlg.show ();

// in case of OK;

if (myReturn == true && dlg.layerRange.layersList.selection.length > 0) {

// get the number instead of the name;

var theLayerSelection = new Array;

var theColl = dlg.layerRange.layersList.items;

for (var p = 0; p < dlg.layerRange.layersList.items.length; p++) {

if (dlg.layerRange.layersList.items

.selected == true) {

theLayerSelection = theLayerSelection.concat(p);

}

};

// collect the rest of the variables,

var theSuffix = dlg.suffix.suffixText.text;

var theNumber = Number (dlg.number.startNumber.text) - 1;

var theLayerNameAdd = dlg.layerName.doAddName.value;

var theDestination = dlg.target.targetField.text;

var theNumbering = dlg.number.addNumber.value;

var folderEach = dlg.target.folderPerSet.value;

// save for web options;

var webOptions = new ExportOptionsSaveForWeb();

webOptions.format = SaveDocumentType.JPEG;

webOptions.includeProfile = false;

webOptions.interlaced = 0;

webOptions.optimized = true;

webOptions.quality = 80;

// history state;

var historyState = myDocument.activeHistoryState;

// create the pdf-name;

if (theSuffix.length > 0) {

var aSuffix = "_" + theSuffix

}

else {

var aSuffix = ""

};

// do the operation;

for (var m = theLayerSelection.length - 1; m >= 0; m--) {

var theLayer = theLayerSets[theLayerSelection];

theLayer.visible = true;

// numbers;

if (theNumbering == true) {

theNumber = bufferNumberWithZeros((Number (theNumber) + 1), 2);

theNumberString =  "_" + theNumber

};

else {

theNumberString = ""

};

// get the layername for the pdf-name;

if (theLayerNameAdd == true) {

var aLayerName = "_" + theLayer.name.replace("/", "_")

}

else {

var aLayerName = ""

}

// hide other layers;

for (var n = theLayerSelection.length - 1; n >= 0; n--) {

var aLayer = theLayerSets[theLayerSelection];

if (aLayer != theLayer && aLayer.visible == true) {

aLayer.visible = false

}

};

// create the individual folders;

if (folderEach == true) {

var aFolder = new Folder (theDestination+"/"+(theLayer.name.replace("/", "_"))).create();

var theDestination2 = theDestination+"/"+(theLayer.name.replace("/", "_"))

}

else {theDestination2 = theDestination};

// hide the llast added layer;

myDocument.exportDocument(new File(theDestination2+"/"+myDocName+aSuffix+aLayerName+theNumberString+".jpg"), ExportType.SAVEFORWEB, webOptions);

};

myDocument.activeHistoryState = historyState;

}

};

else {alert ("no document open")};

////////////////////////////////////

////// buffer number with zeros //////

function bufferNumberWithZeros (number, places) {

var theNumberString = String(number);

for (var o = 0; o < (places - String(number).length); o++) {

theNumberString = String("0" + theNumberString)

};

return theNumberString

};

////// function collect all layersets //////

function collectLayerSets (theParent) {

if (!allLayerSets) {

var allLayerSets = new Array}

else {};

for (var m = theParent.layers.length - 1; m >= 0;m--) {

var theLayer = theParent.layers;

// apply the function to layersets;

if (theLayer.typename == "ArtLayer") {

// allLayerSets = allLayerSets.concat(theLayer)

}

else {

// this line includes the layer groups;

allLayerSets = allLayerSets.concat(theLayer);

allLayerSets = allLayerSets.concat(collectLayerSets(theLayer))

}

}

return allLayerSets

};

1 reply

c.pfaffenbichler
Community Expert
Community Expert
September 14, 2009

Good day!

It’s monday morning here and work has begun, so I’m not able to look into those things right away, but I’ll probably be able to check it out in the course of the week.

As regards the optionsbox You could add a checkbox-item (in any panel really), set up an if-clause referring to its value and depending on that create a new Folder in the proper location and modify the filepath for saving etc.

Regards,

Pfaffenbichler

c.pfaffenbichler
Community Expert
Community Expert
September 14, 2009

Turns out I’ve exaggerated …

You might have to edit the saveForWeb-settings to Your requirements, but You could give this a try:

// creates save for web-jpg-copies of layersets with dialog to select layersets and suffix;

// be advised: existing files of identical names will be overwritten without warnings;

// 2009, pfaffenbichler, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

////////////////////////////////////

// get document-path and -title;

var myDocument = app.activeDocument;

var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];

var myPath = myDocument.path;

var theLayerSets = collectLayerSets(myDocument);

////// filter for checking if entry is numeric, thanks to xbytor //////

numberKeystrokeFilter = function() {

this.text = this.text.replace(",", "");

this.text = this.text.replace(".", "");

if (this.text.match(/[^\-\.\d]/)) {

this.text = this.text.replace(/[^\-\.\d]/g, "");

};

if (this.text == "") {

this.text = "0"

}

};

////////////////////////////////////

// create the dialog;

var dlg = new Window('dialog', "pdfs from layersets", [500,300,930,870]);

//create list for layer-selection;

dlg.layerRange = dlg.add('panel', [21,20,279,475], "select layersets to create pdfs of");

dlg.layerRange.layersList = dlg.layerRange.add('listbox', [11,20,240,435], '', {multiselect: true});

for (var q = 0; q < theLayerSets.length; q++) {

dlg.layerRange.layersList.add ("item", theLayerSets.name);

dlg.layerRange.layersList.items.selected = true

};

// entry for suffix;

dlg.suffix = dlg.add('panel', [290,20,410,85], "enter suffix");

dlg.suffix.suffixText = dlg.suffix.add('edittext', [11,20,99,40], "", {multiline:false});

// entry for number;

dlg.number = dlg.add('panel', [290,100,410,165], "start with #");

dlg.number.startNumber = dlg.number.add('edittext', [11,20,45,40], "1", {multiline:false});

dlg.number.addNumber = dlg.number.add('checkbox', [55,20,105,40], "add", {multiline:false});

dlg.number.startNumber.onChange = numberKeystrokeFilter;

// field to add layer-name;

dlg.layerName = dlg.add('panel', [290,180,410,270], "layer-name");

dlg.layerName.doAddName = dlg.layerName.add('radiobutton', [11,20,120,40], "add it");

dlg.layerName.dontAddName = dlg.layerName.add('radiobutton', [11,45,120,65], "don’t add it");

dlg.layerName.doAddName.value = true;

// field to select target-folder;

dlg.target = dlg.add('panel', [290,285,410,475], "target-folder");

dlg.target.folderPerSet = dlg.target.add('checkbox', [11,20,100,40], "folder each");

dlg.target.targetSel = dlg.target.add('button', [11,50,100,70], "select");

dlg.target.targetField = dlg.target.add('statictext', [11,80,100,185], String(myPath), {multiline:true});

dlg.target.targetSel.onClick = function () {

var target = Folder.selectDialog("select a target folder");

dlg.target.targetField.text = target.fsName

};

// ok- and cancel-buttons;

dlg.buildBtn = dlg.add('button', [220,490,410,505], 'OK', {name:'ok'});

dlg.cancelBtn = dlg.add('button', [21,490,210,505], 'Cancel', {name:'cancel'});

dlg.warning = dlg.add('statictext', [21,520,410,560], "be advised: existing files of the same name will be replaced without prompting", {multiline: true});

dlg.center();

////////////////////////////////////

var myReturn = dlg.show ();

// in case of OK;

if (myReturn == true && dlg.layerRange.layersList.selection.length > 0) {

// get the number instead of the name;

var theLayerSelection = new Array;

var theColl = dlg.layerRange.layersList.items;

for (var p = 0; p < dlg.layerRange.layersList.items.length; p++) {

if (dlg.layerRange.layersList.items

.selected == true) {

theLayerSelection = theLayerSelection.concat(p);

}

};

// collect the rest of the variables,

var theSuffix = dlg.suffix.suffixText.text;

var theNumber = Number (dlg.number.startNumber.text) - 1;

var theLayerNameAdd = dlg.layerName.doAddName.value;

var theDestination = dlg.target.targetField.text;

var theNumbering = dlg.number.addNumber.value;

var folderEach = dlg.target.folderPerSet.value;

// save for web options;

var webOptions = new ExportOptionsSaveForWeb();

webOptions.format = SaveDocumentType.JPEG;

webOptions.includeProfile = false;

webOptions.interlaced = 0;

webOptions.optimized = true;

webOptions.quality = 80;

// create the pdf-name;

if (theSuffix.length > 0) {

var aSuffix = "_" + theSuffix

}

else {

var aSuffix = ""

};

// create a flattened copy;

var theCopy = myDocument.duplicate("thecopy", true);

// do the operation;

for (var m = theLayerSelection.length - 1; m >= 0; m--) {

app.activeDocument = myDocument;

var theLayer = theLayerSets[theLayerSelection];

if (theNumbering == true) {

theNumber = bufferNumberWithZeros((Number (theNumber) + 1), 2);

theNumberString =  "_" + theNumber

};

else {

theNumberString = ""

};

// get the layername for the pdf-name;

if (theLayerNameAdd == true) {

var aLayerName = "_" + theLayer.name.replace("/", "_")

}

else {

var aLayerName = ""

}

// transfer layerset over to the copy;

theLayer.duplicate (theCopy, ElementPlacement.PLACEATBEGINNING);

app.activeDocument = theCopy;

// create the individual folders;

if (folderEach == true) {

var aFolder = new Folder (theDestination+"/"+(theLayer.name.replace("/", "_"))).create();

var theDestination2 = theDestination+"/"+(theLayer.name.replace("/", "_"))

}

else {theDestination2 = theDestination};

// hide the llast added layer;

theCopy.layers[1].visible = false;

theCopy.exportDocument(new File(theDestination2+"/"+myDocName+aSuffix+aLayerName+theNumberString+".jpg"), ExportType.SAVEFORWEB, webOptions);

};

theCopy.close(SaveOptions.DONOTSAVECHANGES);

}

};

else {alert ("no document open")};

////////////////////////////////////

////// buffer number with zeros //////

function bufferNumberWithZeros (number, places) {

var theNumberString = String(number);

for (var o = 0; o < (places - String(number).length); o++) {

theNumberString = String("0" + theNumberString)

};

return theNumberString

};

////// function collect all layersets //////

function collectLayerSets (theParent) {

if (!allLayerSets) {

var allLayerSets = new Array}

else {};

for (var m = theParent.layers.length - 1; m >= 0;m--) {

var theLayer = theParent.layers;

// apply the function to layersets;

if (theLayer.typename == "ArtLayer") {

// allLayerSets = allLayerSets.concat(theLayer)

}

else {

// this line includes the layer groups;

allLayerSets = allLayerSets.concat(theLayer);

allLayerSets = allLayerSets.concat(collectLayerSets(theLayer))

}

}

return allLayerSets

};

c.pfaffenbichler
Community Expert
Community Expert
September 14, 2009

Hmmm I would only need text layers to be updated with localised content, not images. What help file do you mean?


Either the menu Help – Photoshop Help or (available somewhere on the Adobe-homepage) »photoshop_cs4_help.pdf«, a search should turn that up quickly.

When You have a text-layer selected Variables offers Text Replacement, too.

I have no real experience with that feature; but I suspect it might be easier than writing a Script for this.