this script errors out on line 33

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: Tom, Kukurykus

mangurian
Posts: 1
Joined: Wed Mar 26, 2025 5:56 pm

this script errors out on line 33

Post by mangurian »

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.");
}