Photoshop脚本开发:合并脚本提取红色标记图层名至文本层
Merged Photoshop Script: Collect Red-Tagged Layer Names into a Text Layer
Hey there! Let's combine your two scripts into one that does exactly what you're asking for—scans all layers in your active document, grabs the names of those marked red, and dumps them into a single text layer called "Comp" (with each name on a new line). Here's the full working script, plus a breakdown of how it works:
Full Merged Script
#target photoshop if (app.documents.length > 0) { main(); } else { alert("Please open a Photoshop document first!"); } function main() { var myDocument = app.activeDocument; var redLayerNames = []; // Step 1: Traverse all layers and collect red-tagged ones try { var ref = new ActionReference(); ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); var applicationDesc = executeActionGet(ref); var layerCount = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers")); for (var m = 0; m <= layerCount; m++) { try { var layerRef = new ActionReference(); layerRef.putIndex(charIDToTypeID("Lyr "), m); var layerDesc = executeActionGet(layerRef); // Skip layer groups and background layer var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection"))); var isBackground = layerDesc.getBoolean(stringIDToTypeID("background")); if (layerSet === "layerSectionEnd" || isBackground) continue; // Check if layer is tagged red var layerColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color"))); if (layerColor === "red") { var layerName = layerDesc.getString(stringIDToTypeID('name')); redLayerNames.push(layerName); } } catch (e) { // Skip any layers we can't access (rare, but safe to ignore) continue; } } } catch (e) { alert("Error scanning layers: " + e); return; } // Step 2: Handle the text layer if (redLayerNames.length === 0) { alert("No red-tagged layers found!"); return; } // Combine names into a single string with line breaks var combinedText = redLayerNames.join("\n"); // Check if "Comp" text layer already exists var compLayer = null; try { compLayer = myDocument.artLayers.getByName("Comp"); // Make sure it's a text layer if (compLayer.kind !== LayerKind.TEXT) { alert("A layer named 'Comp' exists but isn't a text layer. Please rename or delete it first!"); return; } } catch (e) { // Create new text layer if it doesn't exist compLayer = myDocument.artLayers.add(); compLayer.name = "Comp"; compLayer.kind = LayerKind.TEXT; // Set default text properties compLayer.textItem.size = 10; compLayer.textItem.font = "Arial"; } // Update the text content compLayer.textItem.contents = combinedText; alert("Success! Collected " + redLayerNames.length + " red layer names into 'Comp' text layer."); }
Key Improvements & Explanations
- Layer Filtering: We keep the logic from your "Select by red" script to skip background layers and layer groups, focusing only on regular layers tagged red.
- Name Collection: Instead of just counting red layers, we store their names in an array for later use.
- Text Layer Handling:
- Checks if a "Comp" text layer already exists (and throws a warning if there's a non-text layer with that name to avoid conflicts).
- Creates the layer from scratch if it doesn't exist, with your default font/size settings.
- Error Handling: Added checks for empty documents, missing red layers, and invalid existing layers to make the script more robust.
- Clean Output: Uses
join("\n")to put each layer name on a new line in the text layer, making the list easy to read.
Just paste this into ExtendScript Toolkit (or Photoshop's built-in script editor) and run it on your document—you're good to go!
内容的提问来源于stack exchange,提问作者Alexey_Berezov
相关产品推荐
相关产品推荐

