Skip to content

Instantly share code, notes, and snippets.

@keithcurtis1
Last active October 15, 2025 21:35
Show Gist options
  • Select an option

  • Save keithcurtis1/f7db3c5644e9c277dbaefd5d0689857d to your computer and use it in GitHub Desktop.

Select an option

Save keithcurtis1/f7db3c5644e9c277dbaefd5d0689857d to your computer and use it in GitHub Desktop.
Layer Comp Browserfor Photoshop
#target photoshop
(function () {
if (!app.documents.length) {
alert("No document is open.");
return;
}
var doc = app.activeDocument;
// Ask for search term
var searchTerm = prompt("Search term:", "");
if (!searchTerm) return;
var searchLower = searchTerm.toLowerCase();
// Collect matching layer comps (case-insensitive)
var comps = doc.layerComps;
var found = [];
for (var i = 0; i < comps.length; i++) {
if (comps[i].name.toLowerCase().indexOf(searchLower) !== -1) {
found.push(comps[i]); // store the actual LayerComp object
}
}
if (found.length === 0) {
alert("No Layer Comps match that string.");
return;
}
// Apply the first matching comp
var currentComp = found[0];
currentComp.apply();
app.refresh(); // Force Photoshop to visually update when run via Action
// --- Build ScriptUI dialog ---
var dlg = new Window("dialog", "Layer Comp Browser");
dlg.orientation = "column";
dlg.alignChildren = ["fill", "top"];
dlg.spacing = 10;
dlg.margins = 16;
var info = dlg.add(
"statictext",
undefined,
'"' + currentComp.name + '" is currently shown. Select another to preview or click Apply to finish:'
);
info.alignment = "fill";
var listbox = dlg.add("listbox", undefined, [], { multiselect: false });
listbox.preferredSize = [300, 200];
// populate listbox and attach the exact LayerComp object to each item
for (var j = 0; j < found.length; j++) {
var it = listbox.add("item", found[j].name);
it._compRef = found[j]; // attach reference to the actual LayerComp
}
listbox.selection = 0;
var btnGroup = dlg.add("group");
btnGroup.alignment = "center";
var previewBtn = btnGroup.add("button", undefined, "Preview");
var applyBtn = btnGroup.add("button", undefined, "Apply");
// --- Handlers ---
function applyCompByItem(item) {
if (!item) return;
if (item._compRef && typeof item._compRef.apply === "function") {
try {
item._compRef.apply();
app.refresh(); // <── Force canvas refresh after preview
currentComp = item._compRef;
info.text =
'"' + currentComp.name + '" is currently shown. Select another to preview or click Apply to finish:';
} catch (e) {
alert("Failed to apply comp: " + e.message);
}
} else {
// fallback: if somehow reference missing, try name-based lookup (rare)
for (var k = 0; k < comps.length; k++) {
if (comps[k].name === item.text) {
comps[k].apply();
app.refresh(); // <── Refresh here too
currentComp = comps[k];
info.text =
'"' + currentComp.name + '" is currently shown. Select another to preview or click Apply to finish:';
break;
}
}
}
}
listbox.onDoubleClick = function () {
if (listbox.selection) {
applyCompByItem(listbox.selection);
}
};
previewBtn.onClick = function () {
if (listbox.selection) {
applyCompByItem(listbox.selection);
}
};
applyBtn.onClick = function () {
dlg.close();
};
dlg.center();
dlg.show();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment