Photoshop-Scripts
Photoshop-Scripts copied to clipboard
Update to Import Folder As Layers.jsx
Since all the google results points to pages containing this code, I'm posting here an updated version that:
-
Check both folders and subfolders.
-
Uses Place from Action Commands, instead of doing repeated ctr+c / ctrl+v commands
-
Condenses all of the actions in a single History task.
//based on https://github.com/ES-Collection/Photoshop-Scripts/blob/master/Import%20Folder%20As%20Layers.jsx
var obj = {};
var extRE = /\.(?:png|gif|jpg|bmp|tif|psd)$/i;
cTID = function(s) {
return app.charIDToTypeID(s);
};
sTID = function(s) {
return app.stringIDToTypeID(s);
};
function listFolders(folder, obj) {
var files = folder.getFiles();
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file instanceof Folder) {
obj[file.name] = {};
listFolders(file, obj[file.name]);
} else {
obj[file.name] = file;
}
}
obj = sortObject(obj);
}
function sortObject(obj) {
var sorted = {};
var keys = [];
for (var key in obj) {
keys.push(key);
}
keys.sort(function(a, b) {
var intA = parseInt(a.replace(/\D/g, ""));
var intB = parseInt(b.replace(/\D/g, ""));
if (isNaN(intA) || isNaN(intB)) {
return a.localeCompare(b);
} else {
return intA - intB;
}
});
for (var i = 0; i < keys.length; i++) {
if (obj[keys[i]] instanceof Object) {
sorted[keys[i]] = sortObject(obj[keys[i]]);
} else {
sorted[keys[i]] = obj[keys[i]];
}
}
return sorted;
}
function createLayerSets(obj, parent) {
for (var key in obj) {
if (obj[key] instanceof Object) {
if (key.match(extRE)) {
placeFile(obj[key], parent.itemIndex -1);
} else {
var group = parent.layerSets.add();
group.name = key;
createLayerSets(obj[key], group);
}
} else {
var layer = parent.artLayers.add();
layer.name = key;
}
}
}
function main(){
var folder = Folder.selectDialog("Select a folder to list");
if (folder != null) {
listFolders(folder, obj);
createLayerSets(obj, app.activeDocument);
}
}
app.activeDocument.suspendHistory ("Import Folders and Sub Folders", "main()");
function placeFile(filePath, index) {
(function(enabled, withDialog) {
if (void 0 == enabled || enabled) {
enabled = withDialog ? DialogModes.ALL : DialogModes.NO;
withDialog = new ActionDescriptor();
withDialog.putInteger(cTID("Idnt"), 19);
withDialog.putPath(cTID("null"), new File(filePath));
withDialog.putEnumerated(cTID("FTcs"), cTID("QCSt"), sTID("QCSAverage"));
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(cTID("Hrzn"), cTID("#Pxl"), 0);
desc2.putUnitDouble(cTID("Vrtc"), cTID("#Pxl"), 0);
withDialog.putObject(cTID("Ofst"), cTID("Ofst"), desc2);
executeAction(cTID("Plc "), withDialog, enabled);
}
})();
(function(enabled, withDialog) {
if (void 0 == enabled || enabled) {
enabled = withDialog ? DialogModes.ALL : DialogModes.NO;
withDialog = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated(cTID("Lyr "), cTID("Ordn"), cTID("Trgt"));
withDialog.putReference(cTID("null"), ref1);
ref1 = new ActionReference();
ref1.putIndex(cTID("Lyr "), index);
withDialog.putReference(cTID("T "), ref1);
withDialog.putBoolean(cTID("Adjs"), !1);
withDialog.putInteger(cTID("Vrsn"), 5);
ref1 = new ActionList();
ref1.putInteger(428);
withDialog.putList(cTID("LyrI"), ref1);
executeAction(cTID("move"), withDialog, enabled);
}
})();
}
It was a lot of work, but it turned out better than anything else I have tried.