place image inside a specific Layerset

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

Posts: 45
Joined: Fri Nov 04, 2016 8:29 am

place image inside a specific Layerset

Post by wasfiwasfi »

Hi guys

can anyone help me on this please !

here is whats required:
i have 5 folders in which there are few images, i need a script that will create 5 LayerSets with the folders names and import/place all the images inside their respective LayerSet !

for example if i have a first folder named "Productx", the script will create a LayerSet called "Productx" then import all the images from this folder and place them inside "Productx" LayerSet !

Does this make sense !
is this possible !!?
Thank you very much,

Posts: 29
Joined: Sat Jul 30, 2016 3:52 am
Location: San Francisco

Re: place image inside a specific Layerset

Post by JavierAroche »

This should do it.

You have to select the parent folder that contains the folders you want to process since you can't select multiple folders (as far as I know). I added comments so it's easier to understand.

Code: Select all

// Import folders with images into Photoshop
// Javier Aroche

// Get parent folder that contains folders to process
var parentFolder = Folder.selectDialog("Select parent folder");
// Validate input
if(!parentFolder) {
alert('No folder selected');
throw new Error('No folder selected');

// Get folder's files
var folders = parentFolder.getFiles();
// Make new document (Modify width and height if source images are larger)
var doc = app.documents.add('10000 px', '10000 px', 72, "Untitled-1", NewDocumentMode.RGB, DocumentFill.TRANSPARENT, 1);
// Store new document's empty layer
var emptyLayer = doc.layers[0];

// Iterate through folders starting from the last to avoid reordering
for(var i = folders.length - 1; i > 0; i--) {
// Only work on folders
if(folders[i] instanceof Folder) {
// Get filtered files
var folderFiles = folders[i].getFiles(/.+\.(?:gif|jpe?g|[ew]mf|eps|tiff?|psd|pdf|bmp|png)$/i);
var folderGroup = doc.layerSets.add(); = folders[i].name;

// Iterate through folder's files
for(var ii = 0; ii < folderFiles.length; ii++) {
doc.activeLayer.move(folderGroup, ElementPlacement.PLACEATEND);

// Remove initial empty layer

// Trim document

// Collapse all groups (Optional)

// Helpers
function placeFile(filePath) {
try {
var desc = new ActionDescriptor();
desc.putPath( charIDToTypeID( "null" ), new File( filePath ) );
desc.putEnumerated( charIDToTypeID( "FTcs" ), charIDToTypeID( "QCSt" ), charIDToTypeID( "Qcsa" ) );
executeAction( charIDToTypeID( "Plc " ), desc, DialogModes.NO );
} catch(err) {
alert('Skipped file: ' + filePath);

function collapseAllGroups() {
var desc = new ActionDescriptor();
executeAction( stringIDToTypeID( "collapseAllGroupsEvent" ), desc, DialogModes.NO );

Posts: 45
Joined: Fri Nov 04, 2016 8:29 am

Re: place image inside a specific Layerset

Post by wasfiwasfi »

OMG !! Thank you so much its working ! it have skipped the last folder but by adding an additional empty folder the process is PERFECT !!
if it is not too much ! is there any way to make photoshop "create" all possible combinations of the imported layers by hiding and showing 1 layer each time from each layer !!?
i mean for example:
if have clothing imported by this script it will result to create 3 layersets
inside each one there are some different clothing with different colors
the object is make photoshop do/create all possible combinations and save as png !?

Thank you again !!

Posts: 45
Joined: Fri Nov 04, 2016 8:29 am

Re: place image inside a specific Layerset

Post by wasfiwasfi »

i just have noticed that someone is already asking the same question/request !

i hope you can help us !!