-
-
Save Inventsable/6c6df669f6357e17c87820b2a8f2c54a to your computer and use it in GitHub Desktop.
var deep = true, // Set to false if you don't want to recursively sort layers within layers too | |
isLowerFirst = true; // Set it to false if you want to place the layers with the first uppercase letter above | |
// Convert ILST collection into standard Array so we can use Array methods | |
function get(type, parent) { | |
if (arguments.length == 1 || !parent) parent = app.activeDocument; | |
var result = []; | |
if (!parent[type]) return result; | |
for (var i = 0; i < parent[type].length; i++) result.push(parent[type][i]); | |
return result; | |
} | |
// Polyfill for Array.forEach | |
Array.prototype.forEach = function (callback) { | |
for (var i = 0; i < this.length; i++) callback(this[i], i, this); | |
}; | |
function sortLayersInside(parent) { | |
function isUpperCase(text) { | |
return /[A-Z]/.test(text.charAt(0)) | |
} | |
function isLowerCase(text) { | |
return /[a-z]/.test(text.charAt(0)) | |
} | |
function hasMixedCase(a, b) { | |
if (a.charAt(0).toLowerCase() !== b.charAt(0).toLowerCase()) return false; | |
return isUpperCase(a) && isLowerCase(b) ? true : isLowerCase(a) && isUpperCase(b); | |
} | |
function handleMixedCase(a, b) { | |
var result = a === b ? 0 : isUpperCase(a) ? 1 : -1; | |
return isLowerFirst ? result : result * -1; | |
} | |
get("layers", parent) | |
.sort(function (a, b) { | |
return hasMixedCase(a.name, b.name) ? | |
handleMixedCase(a.name, b.name) : | |
a.name.toLowerCase().localeCompare(b.name.toLowerCase()) | |
}) | |
.forEach(function (layer) { | |
layer.zOrder(ZOrderMethod.SENDTOBACK); | |
if (layer.layers && layer.layers.length && deep) sortLayersInside(layer); | |
}); | |
} | |
sortLayersInside(); |
Hi, Tom. What if we add a flag to check the first letter case when sorting?
https://gist.github.com/creold/0d2e34b5fac9f9644ec51a77acfab76a
Revised with your additions and help! May as well keep this link updated in case someone stumbles across the forum link in the distant future.
By using /[a-z]/ and /[A-Z]/ you are missing layer names in other alphabets. For example, Cyrillic will still be randomly sorted. So I was thinking of a more primitive first letter comparison via .toUpperCase(). What do you think?
Good point. Are we certain that the ESTK String.localeCompare
accurately handles other locales given that it doesn't seem to support the option parameter? I wouldn't be against the primitive comparison at all but if it doesn't accurately handle locales to begin with, we may have more luck finding some kind of polyfill to address them.
Just a quick cursory check for polyfills doesn't turn out much, I've only found one but it only supports a very small list and looking at the issues, seems to not handle accent characters well.
...Though you can always look at the source code for prototype methods in V8 directly. I'm not sure that this necessarily helps though V8 code tends to have pretty detailed explanations and a walkthrough of what the code is doing via comments.
Hi, Tom. What if we add a flag to check the first letter case when sorting?
https://gist.github.com/creold/0d2e34b5fac9f9644ec51a77acfab76a