How to make a custom pattern from selection in Photoshop?


TL;DR – I want to use code to define a custom pattern from selection in Photoshop using scripting (Extendscript).

Here is the code I have so far:

    (function () {
      var doc = app.activeDocument;
      // I put 2 guides in the doc to easily get coords for this test
      var guides = doc.guides;
      var hGuide = guides[0].coordinate.value;
      var vGuide = guides[1].coordinate.value;
      // Select whole doc
      var sel = doc.selection.bounds;
      // Get percentages so we can resize the selection
      var xVal = (hGuide / sel[2].value) * 100;
      var yVal = (vGuide / sel[3].value) * 100;
      doc.selection.resizeBoundary(xVal, yVal, AnchorPosition.TOPLEFT);
      cTID = function (s) { return app.charIDToTypeID(s); };
      sTID = function (s) { return app.stringIDToTypeID(s); };
      // This part is recorded from an ActionListener
      var desc1 = new ActionDescriptor();
      var ref1 = new ActionReference();
      desc1.putReference(cTID('null'), ref1);
      var ref2 = new ActionReference();
      ref2.putProperty(cTID('Prpr'), sTID("selection"));
      ref2.putEnumerated(cTID('Dcmn'), cTID('Ordn'), cTID('Trgt'));
      desc1.putReference(cTID('Usng'), ref2);
      desc1.putString(cTID('Nm  '), "Leaves");
      app.executeAction(cTID('Mk  '), desc1, DialogModes.NO);

This is done manually, in the UI, by using a marquee tool to make a selection, then going to Edit > Define Pattern…, entering the name, then clicking ok.

I have recorded myself doing just that and have gotten an action script in Javascript from it, but when I run it, Extendscript tells me: "General Photoshop error occured. This functionality may not be available in this version of Photoshop."

I am not so skilled when it comes to using the ActionDescriptors and ActionReferences, so I would love some help in getting this to define a custom pattern.

Thank you in advance!

**EDIT:**The ActionScript code works when I manually make the selection, but when I use the above code to make the selection, it doesn’t work. Why would there be a difference, I wonder?


I am not sure what was going on with my setup, but when I used the Ps Javascript API to make a selection, the code broke. But when I used ActionScript to make the selection, it worked. Here are the two functions I have made to select and then make a pattern from the selection:

    function setSelection(top, left, bottom, right) {
      var desc1 = new ActionDescriptor();
      var ref1 = new ActionReference();
      ref1.putProperty(app.charIDToTypeID('Chnl'), app.stringIDToTypeID("selection"));
      desc1.putReference(app.charIDToTypeID('null'), ref1);
      var desc2 = new ActionDescriptor();
      desc2.putUnitDouble(app.charIDToTypeID('Top '), app.charIDToTypeID('#Pxl'), top);
      desc2.putUnitDouble(app.charIDToTypeID('Left'), app.charIDToTypeID('#Pxl'), left);
      desc2.putUnitDouble(app.charIDToTypeID('Btom'), app.charIDToTypeID('#Pxl'), bottom);
      desc2.putUnitDouble(app.charIDToTypeID('Rght'), app.charIDToTypeID('#Pxl'), right);
      desc1.putObject(app.charIDToTypeID('T   '), app.charIDToTypeID('Rctn'), desc2);
      executeAction(app.charIDToTypeID('setd'), desc1, DialogModes.NO);

    function makePatternFromSelection(name) {
      var desc1 = new ActionDescriptor();
      var ref1 = new ActionReference();
      desc1.putReference(app.charIDToTypeID('null'), ref1);
      var ref2 = new ActionReference();
      ref2.putProperty(app.charIDToTypeID('Prpr'), app.stringIDToTypeID("selection"));
      ref2.putEnumerated(app.charIDToTypeID('Dcmn'), app.charIDToTypeID('Ordn'), app.charIDToTypeID('Trgt'));
      desc1.putReference(app.charIDToTypeID('Usng'), ref2);
      desc1.putString(app.charIDToTypeID('Nm  '), name);
      executeAction(app.charIDToTypeID('Mk  '), desc1, DialogModes.NO);

Answered By – Casey

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published