Skip to main content
Inspiring
May 30, 2016
Answered

Per-layer metadata

  • May 30, 2016
  • 3 replies
  • 1535 views

I've searching around the web quite a lot regarding this but haven't been able to find any kind of official information about it:

Does this functionality exists in PS CC, and if so: where can I find code examples?

This topic has been closed for replies.
Correct answer SuperMerlin

Here is a script written by Paul Riggott, "Per Layer Guides"

#target photoshop

//Written by Paul Riggott

main();

function main(){

if(Number(app.version.match(/\d+/)) <12) return;

if(!documents.length) return;

var doc =activeDocument;

if(doc.activeLayer.isBackgroundLayer) return;

if(doc.activeLayer.kind != LayerKind.NORMAL) return;

if (ExternalObject.AdobeXMPScript == undefined)  ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");

var startRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

if(ScriptUI.environment.keyboardState.shiftKey ){

    setGuides();

    }else{

        displayGuides();

        }

app.preferences.rulerUnits = startRulerUnits;

function setGuides(){

try{

    xmp = doc.activeLayer.xmpMetadata.rawData;

    xmpObject = new XMPMeta(xmp);

} catch(e){

    xmpObject = new XMPMeta();

}

var psNamespace = "http://ns.layerguides/1.0/";

var psPrefix = "pmrGuides:";

XMPMeta.registerNamespace(psNamespace, psPrefix);

var guides = app.activeDocument.guides;

if(guides.length == 0){

    alert("No guides exist");

    return;

    }

var gH = '';

var gV = '';

for( var g = 0; g < guides.length; g++ ){

    if(guides.direction.toString() == 'Direction.HORIZONTAL'){

        gH+=(parseInt(guides.coordinate.value));

        gH+=',';

        }else{

            gV+=(parseInt(guides.coordinate.value));

            gV+=','

            }

}

gH=gH.replace(/,$/,'');

gV=gV.replace(/,$/,'');

currentGuides = 'Layer Guides' + "¬" + gH + "¬" + gV;

xmpObject.deleteProperty(psNamespace, "LayerGuides");

xmpObject.setProperty(psNamespace, "LayerGuides",currentGuides);

app.activeDocument.activeLayer.xmpMetadata.rawData = xmpObject.serialize();

}

function displayGuides(){

try{

    xmp = doc.activeLayer.xmpMetadata.rawData;

    xmpObject = new XMPMeta(xmp);

} catch(e){

    return;

}

var psNamespace = "http://ns.layerguides/1.0/";

var psPrefix = "pmrGuides:";

var layerGuides = xmpObject.getProperty(psNamespace, "LayerGuides");

clearGuides();

var ar1 = layerGuides.toString().split('¬');

var Hor = ar1[1].toString().split(',');

var Ver = ar1[2].toString().split(',');

for(var H in Hor){

    activeDocument.guides.add(Direction.HORIZONTAL,new UnitValue(Number(Hor),'px'));

    }

for(var V in Ver){

    activeDocument.guides.add(Direction.VERTICAL,new UnitValue(Number(Ver),'px'));

    }

}

}

function clearGuides() {

   var id556 = charIDToTypeID( "Dlt " );

       var desc102 = new ActionDescriptor();

       var id557 = charIDToTypeID( "null" );

           var ref70 = new ActionReference();

           var id558 = charIDToTypeID( "Gd  " );

           var id559 = charIDToTypeID( "Ordn" );

           var id560 = charIDToTypeID( "Al  " );

           ref70.putEnumerated( id558, id559, id560 );

       desc102.putReference( id557, ref70 );

   executeAction( id556, desc102, DialogModes.NO );

};

3 replies

HeimdaalAuthor
Inspiring
May 30, 2016

Thank you both.

I found the xmpMetadata function in the docs now and this is starting to make sense to me.

However, I find no information in the docs about the XMPMeta -class and it's functions. I see that setProperty, getProperty, deleteProperty are serialize are some of these, but where can I find more info on this class?

SuperMerlin
Inspiring
May 30, 2016

You will find the info in "The Javascript Tools Guide" that can be accesed from the help menu in Extendscript Toolkit

Participant
May 30, 2016

const ns= 'http://www.smartobjectlinks.com/1.0/';

var __xmp = new XMPMeta( activeDocument.activeLayer.xmpMetadata.rawData );

__xmp.setProperty( __ns, "layerName", activeDocument.activeLayer.name );

SuperMerlin
SuperMerlinCorrect answer
Inspiring
May 30, 2016

Here is a script written by Paul Riggott, "Per Layer Guides"

#target photoshop

//Written by Paul Riggott

main();

function main(){

if(Number(app.version.match(/\d+/)) <12) return;

if(!documents.length) return;

var doc =activeDocument;

if(doc.activeLayer.isBackgroundLayer) return;

if(doc.activeLayer.kind != LayerKind.NORMAL) return;

if (ExternalObject.AdobeXMPScript == undefined)  ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");

var startRulerUnits = app.preferences.rulerUnits;

app.preferences.rulerUnits = Units.PIXELS;

if(ScriptUI.environment.keyboardState.shiftKey ){

    setGuides();

    }else{

        displayGuides();

        }

app.preferences.rulerUnits = startRulerUnits;

function setGuides(){

try{

    xmp = doc.activeLayer.xmpMetadata.rawData;

    xmpObject = new XMPMeta(xmp);

} catch(e){

    xmpObject = new XMPMeta();

}

var psNamespace = "http://ns.layerguides/1.0/";

var psPrefix = "pmrGuides:";

XMPMeta.registerNamespace(psNamespace, psPrefix);

var guides = app.activeDocument.guides;

if(guides.length == 0){

    alert("No guides exist");

    return;

    }

var gH = '';

var gV = '';

for( var g = 0; g < guides.length; g++ ){

    if(guides.direction.toString() == 'Direction.HORIZONTAL'){

        gH+=(parseInt(guides.coordinate.value));

        gH+=',';

        }else{

            gV+=(parseInt(guides.coordinate.value));

            gV+=','

            }

}

gH=gH.replace(/,$/,'');

gV=gV.replace(/,$/,'');

currentGuides = 'Layer Guides' + "¬" + gH + "¬" + gV;

xmpObject.deleteProperty(psNamespace, "LayerGuides");

xmpObject.setProperty(psNamespace, "LayerGuides",currentGuides);

app.activeDocument.activeLayer.xmpMetadata.rawData = xmpObject.serialize();

}

function displayGuides(){

try{

    xmp = doc.activeLayer.xmpMetadata.rawData;

    xmpObject = new XMPMeta(xmp);

} catch(e){

    return;

}

var psNamespace = "http://ns.layerguides/1.0/";

var psPrefix = "pmrGuides:";

var layerGuides = xmpObject.getProperty(psNamespace, "LayerGuides");

clearGuides();

var ar1 = layerGuides.toString().split('¬');

var Hor = ar1[1].toString().split(',');

var Ver = ar1[2].toString().split(',');

for(var H in Hor){

    activeDocument.guides.add(Direction.HORIZONTAL,new UnitValue(Number(Hor),'px'));

    }

for(var V in Ver){

    activeDocument.guides.add(Direction.VERTICAL,new UnitValue(Number(Ver),'px'));

    }

}

}

function clearGuides() {

   var id556 = charIDToTypeID( "Dlt " );

       var desc102 = new ActionDescriptor();

       var id557 = charIDToTypeID( "null" );

           var ref70 = new ActionReference();

           var id558 = charIDToTypeID( "Gd  " );

           var id559 = charIDToTypeID( "Ordn" );

           var id560 = charIDToTypeID( "Al  " );

           ref70.putEnumerated( id558, id559, id560 );

       desc102.putReference( id557, ref70 );

   executeAction( id556, desc102, DialogModes.NO );

};

HeimdaalAuthor
Inspiring
June 2, 2016

For some reason, the jsx-script comes to a halt because it catches an exception:

  1. try
  2.     xmp = doc.activeLayer.xmpMetadata.rawData; 
  3.     xmpObject = new XMPMeta(xmp); 
  4. } catch(e){ 
  5.     xmpObject = new XMPMeta(); 
  6. }

No XMP object is created. Why?

SuperMerlin
Inspiring
June 2, 2016

if (ExternalObject.AdobeXMPScript == undefined)  ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");

    try{

        xmp = activeDocument.activeLayer.xmpMetadata.rawData;

        xmpObject = new XMPMeta(xmp);

    } catch(e){

        xmpObject = new XMPMeta();

    }