Photoshop CS6: How to I extract Layer TimeStamps via scripting?

  • 1
  • Question
  • Updated 2 years ago
  • (Edited)
I am trying to write a script to extract the layer.timestamp from each
layer in a group of Photoshop files using Photoshop CS6. Is there
layer-level timestamping?
Photo of Michael Mckenna-Mattiaccio

Michael Mckenna-Mattiaccio

  • 3 Posts
  • 0 Reply Likes
  • anxious

Posted 2 years ago

  • 1
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 141 Reply Likes
No there is no metadata for a layer unless you put it there.
Photo of Michael Mckenna-Mattiaccio

Michael Mckenna-Mattiaccio

  • 3 Posts
  • 0 Reply Likes
According to John Nack in 2008 it's possible, he says it is automagically created.  https://blogs.adobe.com/jnack/2008/10/per-layer_metadata_comes_to_photoshop.html 
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 141 Reply Likes
Here is some code you can run from ExtendScript Toolkit that will print all the layers metadata if there is any to the console.

checkLayerMetadata();
function checkLayerMetadata(){
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
 try{
xmp = new XMPMeta( app.activeDocument.activeLayer.xmpMetadata.rawData );
} catch( e ) {
alert("There is no metadata for this layer");
return;
}
$.writeln(xmp.dumpObject());
};
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 141 Reply Likes
Ah, I was thinking about per layer metadata, but the layer also has it's own keys one called metadata that holds the layers modified time.
This should get you the modified time of a layer...

$.writeln(layerTime());
function layerTime(){
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"),stringIDToTypeID("metadata"));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var lt = executeActionGet(ref).getObjectValue(stringIDToTypeID("metadata")).getDouble(stringIDToTypeID("layerTime"));
return new Date(lt*1000.0).toLocaleString();
};
Photo of Michael Mckenna-Mattiaccio

Michael Mckenna-Mattiaccio

  • 3 Posts
  • 0 Reply Likes
Hi Paul, 

Thanks for your help! I am getting a singular result instead of a loop of all the layers in the file. How do I loop? 

Here is a script that loops through the layers but uses a different method of getting the metadata and I'm not sure how to use it to get the create or save or modification date/time.


var layerNum = app.activeDocument.layers.length

var layerSetNum = app.activeDocument.layerSets.length
$.writeln(layerNum)
$.writeln(layerSetNum)

//function layerMetadata(currLayer)
showBounds(app.activeDocument.layerSets);



var allLayers = new Array();
var theLayers = collectAllLayers(app.activeDocument, 0);


function collectAllLayers (theParent, level)
{
    for (var m = 0; m < theParent.layers.length; m++)
    {
    var theLayer = theParent.layers[m];      
    allLayers.push(level + theLayer.name);
    $.writeln(theLayer.name);
    $.writeln(theLayer.typeName);
    var xmpData =  theLayer.xmpMetadata.parent;
    var strXMPData = xmpData.rawData;
    $.writeln(strXMPData);
    //alert(theLayer.xmpMetadata.rawData);
    //collectAllLayers(theLayer, level + 1)
  }

}

function showBounds(layerNode) {    
    for (var i=0; i<layerNode.length; i++) {
        $.writeln(layerNode.length);
        showBounds(layerNode[i].layerSets);

        for(var layerIndex=0; layerIndex < layerNode[i].artLayers.length; layerIndex++) {
            var layer=layerNode[i].artLayers[layerIndex];
            a$.writeln(layer.name);
            }
        }
    }
Photo of Paul Riggott

Paul Riggott

  • 359 Posts
  • 141 Reply Likes
I think the layerTime is read only, the time is updated if you make any change to the layer.

To access per layer metadata you will need to use AdobeXMPScript, information can be found under:-
Accessing the XMP scripting API in the JavaScript Tools Guide.