Copy link to clipboard
Copied
I'm looking for a script that (1) finds a layer that contains specific text, then (2) provides an error message if no layer can be found.
I've already found the first portion in a previous post but i'm not familiar with script writing to understand how to prompt an error message if else. See below for the first portion that's working but needs an if else prompt added. For context, the text i'm searching for is "Grain -"
s2t = stringIDToTypeID;
(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
ref.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(ref).getInteger(p);
for (var i = 1; i <= len; i++) {
(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
ref.putIndex(s2t('layer'), i);
if ((/Grain -/i).test(executeActionGet(ref).getString(p))) {
(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
ref.putIndex(s2t('layer'), i);
(r = new ActionReference()).putIdentifier(s2t("layer"), executeActionGet(ref).getInteger(p));
(d = new ActionDescriptor()).putReference(s2t("target"), r);
executeAction(s2t("select"), d, DialogModes.NO);
break;
}
}
Copy link to clipboard
Copied
This is a variant on a production script I use. I do have one that exports text layers to a file, that can be opened in Excel and searched.
This one does as you want and alerts if text isn't found. Just change the hard-coded variable.
#target photoshop
textChecker();
function textChecker(){
if(documents.length > 0){
var originalDialogMode = app.displayDialogs;
app.displayDialogs = DialogModes.ERROR;
try{
for(var j = 0; j < documents.length; j++){
var docRef = documents[j];
var sets = docRef.layerSets;
var searchTerm = 'test';
for(var l = 0; l < sets.length; l++){
for(var k = 0; k < sets[l].artLayers.length; k++){
var SetLayerRef = sets[l].artLayers[k];
if(SetLayerRef.kind == LayerKind.TEXT){
var SetlayerText = SetLayerRef.textItem.contents;
if(SetlayerText.search(searchTerm) == -1){
alert('Not found in ' + SetLayerRef.name);
}
}
}
}
for(var i = 0; i < docRef.artLayers.length; i++){
var LayerRef = docRef.artLayers[i];
if(LayerRef.kind == LayerKind.TEXT){
var layerText = LayerRef.textItem.contents;
if(layerText.search(searchTerm) == -1){
alert('Not found in ' + LayerRef.name);
}
}
}
}
}
catch(e){
alert(e + e.line);
}
app.displayDialogs = originalDialogMode;
}
}
Copy link to clipboard
Copied
I think that all you need to do is add an "else" to the "if" block...
s2t = stringIDToTypeID;
(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('numberOfLayers'));
ref.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var len = executeActionGet(ref).getInteger(p);
for (var i = 1; i <= len; i++) {
(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
ref.putIndex(s2t('layer'), i);
if ((/Grain -/i).test(executeActionGet(ref).getString(p))) {
(ref = new ActionReference()).putProperty(s2t('property'), p = s2t('layerID'));
ref.putIndex(s2t('layer'), i);
(r = new ActionReference()).putIdentifier(s2t("layer"), executeActionGet(ref).getInteger(p));
(d = new ActionDescriptor()).putReference(s2t("target"), r);
executeAction(s2t("select"), d, DialogModes.NO);
break;
} else {
app.beep();
alert("Match not found!");
}
}
Copy link to clipboard
Copied
This would select the Type Layers containing a certain text.
// findTextInTypeLayers;
// select type layers containing certain text;
// 2022, use it at your own risk;
if (app.documents.length > 0) {
selectTypeLayerWithText("tempor")
};
////////////////////////////////////
function selectTypeLayerWithText (theString) {
var theCheck = false;
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if not layer group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true && layerDesc.hasKey(stringIDToTypeID("textKey")) == true) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var theText = textDesc.getString(stringIDToTypeID('textKey'));
// compare text;
if (theText.match(theString) != null) {
selectLayerByID(theID,theCheck);
theCheck = true;
theLayers.push([theName, theID, theText])
};
};
}
catch (e) {};
};
if (theLayers.length == 0) {
alert ("no matching layer");
return
} else {return theLayers}
};
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};