I am trying to script the sorting of open Photoshop documents by name.
It errors out on the execute command.
Any insight greatly appreciated.
_________________________________________________
// Photoshop Script to sort open documents by name
if (documents.length > 0) {
// Create an array to hold document names and indices
var docArray = [];
// Populate the array with document names and their current indices
for (var i = 0; i < documents.length; i++) {
docArray.push({
name: documents.name.toLowerCase(), // Use lowercase for case-insensitive sort
index: i
});
}
// Sort the array by document name
docArray.sort(function(a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
// Create the action descriptor for reordering documents
var desc = new ActionDescriptor();
var list = new ActionList();
// Add document references in sorted order
for (var j = 0; j < docArray.length; j++) {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Dcmn"), docArray[j].index);
list.putReference(ref);
}
desc.putList(charIDToTypeID("null"), list);
executeAction(charIDToTypeID("Ordn"), desc, DialogModes.NO);
alert("Documents have been sorted by name.");
} else {
alert("No documents are open to sort.");
}