Skip to main content
edvardkrupke
Known Participant
June 13, 2019
Answered

Create clipping path from current layer's vector- or layermask?

  • June 13, 2019
  • 1 reply
  • 1407 views

Hi!

I am having a bit of trouble with finding good sources of how to convert the current layer's vector- or layermask into a clipping path via Javascript.

I only found one reference in the scripting reference (apart from the index) - page 139;

MethodParameter type
Returns
What it does

makeClippingPath

([flatness])

number [0.2..100]Makes this the clipping path for this document. flatness tells the PostScript printer how to approximate curves in the path.

I have tried this to select the Vector Mask;

makeActiveByName("Canvas");

activeDocument.pathItems.getByName('Canvas Vector Mask').select(); 

But when I add this;

activeDocument.pathItems.getByName('Canvas Vector Mask').makeClippingPath(0.2);

I get this;

I am aware that I should probably convert the Vector mask to a path before creating the clippingpath, but am unable to find relevant info on that issue as well.

TL;DR: How do I create a Clipping path from a Vector- or Layermask through Javascript?

If you can help me closer to the solution of the problem; Thank you!

This topic has been closed for replies.
Correct answer Stephen Marsh

Not sure if this helps or not... the following cleaned SL code will duplicate the selected (shape?) path and set it to a clipping path with a 0.2 flatness value:

// =======================================================

make();

function make() {

  var c2t = function (s) {

  return app.charIDToTypeID(s);

  };

  var s2t = function (s) {

  return app.stringIDToTypeID(s);

  };

  var descriptor = new ActionDescriptor();

  var reference = new ActionReference();

  var reference2 = new ActionReference();

  reference.putClass( s2t( "path" ));

  descriptor.putReference( c2t( "null" ), reference );

  reference2.putEnumerated( s2t( "path" ), s2t( "path" ), s2t( "vectorMask" ));

  reference2.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));

  descriptor.putReference( s2t( "from" ), reference2 );

  executeAction( s2t( "make" ), descriptor, DialogModes.NO );

}

// =======================================================

set();

function set() {

  var c2t = function (s) {

  return app.charIDToTypeID(s);

  };

  var s2t = function (s) {

  return app.stringIDToTypeID(s);

  };

  var descriptor = new ActionDescriptor();

  var descriptor2 = new ActionDescriptor();

  var reference = new ActionReference();

  var reference2 = new ActionReference();

  reference.putProperty( s2t( "path" ), s2t( "clippingPath" ));

  descriptor.putReference( c2t( "null" ), reference );

  reference2.putEnumerated( s2t( "path" ), s2t( "ordinal" ), s2t( "targetEnum" ));

  descriptor2.putReference( s2t( "path" ), reference2 );

  descriptor2.putDouble( s2t( "flatness" ), 0.200000 ); // flatness value

  descriptor.putObject( s2t( "to" ), s2t( "clippingPathEPS" ), descriptor2 );

  executeAction( s2t( "set" ), descriptor, DialogModes.NO );

}

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
June 15, 2019

Not sure if this helps or not... the following cleaned SL code will duplicate the selected (shape?) path and set it to a clipping path with a 0.2 flatness value:

// =======================================================

make();

function make() {

  var c2t = function (s) {

  return app.charIDToTypeID(s);

  };

  var s2t = function (s) {

  return app.stringIDToTypeID(s);

  };

  var descriptor = new ActionDescriptor();

  var reference = new ActionReference();

  var reference2 = new ActionReference();

  reference.putClass( s2t( "path" ));

  descriptor.putReference( c2t( "null" ), reference );

  reference2.putEnumerated( s2t( "path" ), s2t( "path" ), s2t( "vectorMask" ));

  reference2.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));

  descriptor.putReference( s2t( "from" ), reference2 );

  executeAction( s2t( "make" ), descriptor, DialogModes.NO );

}

// =======================================================

set();

function set() {

  var c2t = function (s) {

  return app.charIDToTypeID(s);

  };

  var s2t = function (s) {

  return app.stringIDToTypeID(s);

  };

  var descriptor = new ActionDescriptor();

  var descriptor2 = new ActionDescriptor();

  var reference = new ActionReference();

  var reference2 = new ActionReference();

  reference.putProperty( s2t( "path" ), s2t( "clippingPath" ));

  descriptor.putReference( c2t( "null" ), reference );

  reference2.putEnumerated( s2t( "path" ), s2t( "ordinal" ), s2t( "targetEnum" ));

  descriptor2.putReference( s2t( "path" ), reference2 );

  descriptor2.putDouble( s2t( "flatness" ), 0.200000 ); // flatness value

  descriptor.putObject( s2t( "to" ), s2t( "clippingPathEPS" ), descriptor2 );

  executeAction( s2t( "set" ), descriptor, DialogModes.NO );

}

edvardkrupke
Known Participant
June 18, 2019

Well, I cannot for the life figure out why or how - but it does indeed do what I want i to do. Hah... I'll need to take a closer look at this.

Thank you immensely much, Stephen_A_March - take care - hope you will have a fantastic day!

Stephen Marsh
Community Expert
Community Expert
June 19, 2019

Your welcome, that's the "beauty" of Script Listener/Action Manager recorded code... even when cleaned via the Clean SL script, it can still be esoteric!