.jsx script extremely slow
Hi,
I've tried to work a little with photoshop scripting, and I tried to make a thing that removes groups if it fits some criteria.
The problem I am facing is - it's VERY VERY slow. Let's say we have a file that contains 10 groups, each group contains 10 sub-groups, and each of these groups contains 10 layers.
I made a recursion that checks if the first child layer is a group, if it is a group, it moves in (recursion), if it's not, then skip. At the end, remove group. (This is simplified, but it is basically the thing).
From my previous programming experience, I'd say this should run under a second without any issues. But what I've found out is, that it takes several seconds to remove one group.
This is just straight up unacceptable. Can anyone point me in the right direction on how to solve this? Or explain to me, why is such a popular app so so so badly made? (In this specific case)
These results show, that any photoshop call takes quite a long time. Any suggestions on how to improve this? I'd need it to improve dramatically in speed (at least 10x).
Second issue:
I've now noticed a second problem - the first group is marked visible immediatelly, so it doens't get removed. Any idea why this happens?
Intel core i7-7700, 32 GB RAM, Adobe Photoshop 2021
Recreate demo:
Photoshop file:
create new, printing, A4, remove layer, add empty layer, dulicate so you have 10 empty layers. Now group these 10 layers into one group, and duplicate 9 more times. Group these groups together and duplicate 9 more times.
So there are 10 top groups, each contains 10 sub groups, each sub group contains 10 empty groups.
My code:
var start_time = new Date().getTime();
var doc = activeDocument;
var objectString;
var textFile = File(doc.path + '/time.txt');
textFile.encoding = 'UTF-8';
textFile.open('a');
textFile.write("Initialization: ", new Date().getTime() - start_time, "\n");
loopTopGroups();
textFile.write("Total time: ", new Date().getTime() - start_time, "\n");
function loopTopGroups() {
var time_before = new Date().getTime();
var len = doc.layers.length;
var layers = doc.layers;
textFile.write("Get layers: ", new Date().getTime() - time_before, "\n");
for (var i = len - 1; i >= 0; i--) {
var currentLayer = layers[i];
if (!currentLayer.visible) {
loopGroups(currentLayer);
}
if (currentLayer.layers.length == 0) {
var time_before = new Date().getTime();
currentLayer.remove();
textFile.write("Out remove: ", new Date().getTime() - time_before, "\n");
}
}
}
function loopGroups(parentLayer) {
var time_before = new Date().getTime();
var len = parentLayer.layers.length;
var layers = parentLayer.layers;
textFile.write("Get inner layers: ", new Date().getTime() - time_before, "\n");
for (var i = len - 1; i >= 0; i--) {
var currentLayer = layers[i];
if (currentLayer.typename == 'LayerSet') {
if (currentLayer.layers[0].typename == 'LayerSet') {
loopGroups(currentLayer);
} else {
var time_before = new Date().getTime();
currentLayer.remove();
textFile.write("Remove: ", new Date().getTime() - time_before, "\n");
}
}
}
}
Output:
