Skip to main content
Inspiring
September 5, 2022
Question

Quick way to determine if PSD has text layers

  • September 5, 2022
  • 4 replies
  • 423 views

In the interest of efficiency, without looping over all layers (whould can get slow), is it possible to determine if a .PSD countains or does not contain text layers? 

 

    Sort of like this?

var hasText = app.activeDocument.textLayers;

 - But obviously that won't work.

This topic has been closed for replies.

4 replies

Stephen Marsh
Community Expert
Community Expert
September 5, 2022

This Photoshop script checks the metadata of an open document. I adapted the code from another script, source unknown ( I'm presuming @SuperMerlin )

 

#target photoshop

if (documents.length) {
   if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
   var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
   var textLayer = getArrayItems(XMPConst.NS_PHOTOSHOP, 'photoshop:TextLayers');
   for (var a in textLayer) {
      if (textLayer) {
         alert('This file has one or more text layers...');
         break;
      }
   }
}

function getArrayItems(ns, prop) {
   var arrItem = [];
   var items = xmp.countArrayItems(ns, prop);
   for (var i = 1; i <= items; i++) {
      arrItem.push(xmp.getArrayItem(ns, prop, i));
   }
   return arrItem;
}

 

Stephen Marsh
Community Expert
Community Expert
September 5, 2022

And this Bridge script will add tif tiff psd psb files that contain text layers in the current folder to a collection:

 

/*
Files with Text Layers to Collection.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/quick-way-to-determine-if-psd-has-text-layers/td-p/13179161
Based on a script from SuperMerlin
https://community.adobe.com/t5/bridge/how-to-check-if-image-includes-layers/m-p/11111182
How to check if image includes layers?
*/

#target bridge

if (BridgeTalk.appName == "bridge") {
    layeredFiles = MenuElement.create("command", "Files with Text Layers to Collection", "at the end of Tools");
}
layeredFiles.onSelect = function () {
    var fileList = Folder(app.document.presentationPath).getFiles(/\.(tif|tiff|psd|psb)$/i);
    withLayers = new Array();
    for (var x in fileList) {
        var file = fileList[x];
        file.open("r");
        file.encoding = 'BINARY';
        var dat = file.read();
        file.close();
        var result;
        var pos = [];
        var rex = /TySh/g;
        while ((result = rex.exec(dat)) != null) {
            pos.push(result.index + (result[0].length));
        }
        if (pos.length > 0) withLayers.push(new Thumbnail(fileList[x]));
        dat = null;
    }
    if (withLayers.length > 0) {
        var foundFiles = app.createCollection("Files with Text Layers");
        app.addCollectionMember(foundFiles, withLayers);
    }
};  

 

 

Stephen Marsh
Community Expert
Community Expert
September 5, 2022

It will take me a while to see if I can hack some existing code to search for the text layer metadata.

 

Much faster to post some ExifTool code:

 

exiftool -if '$XMP-photoshop:TextLayerName' -XMP-photoshop:TextLayerName -r 'TOP LEVEL ROOT FOLDER'

 

On Windows, one would use straight " double quotes around a file path that contains word spaces.

 

____________________

 

Alternatively, here is a Bridge Find command:

 

 

Inspiring
September 5, 2022

Cheers, Stephen! Got it!

 

For my future self:

var srcDoc = app.activeDocument;
var rawData = srcDoc.xmpMetadata.rawData;
alert(find_text_layers(rawData));


function find_text_layers(str)
{
  var regEx = new RegExp("\<photoshop:TextLayers\>", "gim");
  var results = str.match(regEx);
  if (results) return true;
  else return false;
}
Stephen Marsh
Community Expert
Community Expert
September 5, 2022

@Ghoul Fool – XMP metadata:

 

<photoshop:TextLayers>