2 Replies Latest reply on Jul 15, 2018 3:48 AM by igorm86051102

    How do I fill a selection using a pre defined pattern with photoshop scripting

    aj2460

      var docRef = app.documents.add()

      var layerRef =docRef.layerSets.add();

      layerRef.kind = LayerKind.PATTERNFILL;

      docRef.selection.selectAll();

      docRef.selection.fill(pattern?);

       

       

      and also how do I create a pattern using the script

        • 1. Re: How do I fill a selection using a pre defined pattern with photoshop scripting
          JJMack Most Valuable Participant

          I think you will need to use Action Manager code create function using scriptlister generated code. Fill selection in DOM seems to be for solid color.  However looking at Action manager code for pattern fill it look like there my be some UUID. It may be because I use Adobe Pattern bubbles not a pattern I defined.  In the Define Pattern step I recorded all I see in the name I created that may be a problem for it look like I can create patterns with duplicate names which could mat selection a pattern by name a problem. I had a problem like that creating Custom shape I need to make the name I created unique so I added a time suffix to the Custom shape name.

           

           

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

          var idFl = charIDToTypeID( "Fl  " );

              var desc16 = new ActionDescriptor();

              var idUsng = charIDToTypeID( "Usng" );

              var idFlCn = charIDToTypeID( "FlCn" );

              var idPtrn = charIDToTypeID( "Ptrn" );

              desc16.putEnumerated( idUsng, idFlCn, idPtrn );

              var idPtrn = charIDToTypeID( "Ptrn" );

                  var desc17 = new ActionDescriptor();

                  var idNm = charIDToTypeID( "Nm  " );

                  desc17.putString( idNm, """$$$/Presets/Patterns/Patterns_pat/Bubbles=Bubbles""" );

                  var idIdnt = charIDToTypeID( "Idnt" );

                  desc17.putString( idIdnt, """b7334da0-122f-11d4-8bb5-e27e45023b5f""" );

              var idPtrn = charIDToTypeID( "Ptrn" );

              desc16.putObject( idPtrn, idPtrn, desc17 );

              var idOpct = charIDToTypeID( "Opct" );

              var idPrc = charIDToTypeID( "#Prc" );

              desc16.putUnitDouble( idOpct, idPrc, 100.000000 );

              var idMd = charIDToTypeID( "Md  " );

              var idBlnM = charIDToTypeID( "BlnM" );

              var idNrml = charIDToTypeID( "Nrml" );

              desc16.putEnumerated( idMd, idBlnM, idNrml );

          executeAction( idFl, desc16, DialogModes.NO );

           

           

           

           

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

          var idMk = charIDToTypeID( "Mk  " );

              var desc21 = new ActionDescriptor();

              var idnull = charIDToTypeID( "null" );

                  var ref4 = new ActionReference();

                  var idPtrn = charIDToTypeID( "Ptrn" );

                  ref4.putClass( idPtrn );

              desc21.putReference( idnull, ref4 );

              var idUsng = charIDToTypeID( "Usng" );

                  var ref5 = new ActionReference();

                  var idPrpr = charIDToTypeID( "Prpr" );

                  var idfsel = charIDToTypeID( "fsel" );

                  ref5.putProperty( idPrpr, idfsel );

                  var idDcmn = charIDToTypeID( "Dcmn" );

                  var idOrdn = charIDToTypeID( "Ordn" );

                  var idTrgt = charIDToTypeID( "Trgt" );

                  ref5.putEnumerated( idDcmn, idOrdn, idTrgt );

              desc21.putReference( idUsng, ref5 );

              var idNm = charIDToTypeID( "Nm  " );

              desc21.putString( idNm, """Pattern O""" );

          executeAction( idMk, desc21, DialogModes.NO );

          • 2. Re: How do I fill a selection using a pre defined pattern with photoshop scripting
            igorm86051102

            If you want also to export the last pattern defined (as I needed), then this is how it should be done:

             

            var patternName = 'PatternName';
            var patternPath = 'somePath/' + patternName + '.pat';
            
            definePattern(patternName);
            
            var lastPatternIndex = getPatternIndexByName(patternName);
            
            exportPattern(lastPatternIndex, patternPath);
            
            
            function definePattern(patternName) {
               var dialogMode = DialogModes.NO;
               var desc1 = new ActionDescriptor();
               var ref1 = new ActionReference();
               ref1.putClass(cTID('Ptrn'));
               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 '), patternName);
               executeAction(cTID('Mk '), desc1, dialogMode);
            }
            
            function exportPattern(patternIndex, patternPath) {
               var idsetd = charIDToTypeID( "setd" );
               var desc74 = new ActionDescriptor();
               var idnull = charIDToTypeID( "null" );
               desc74.putPath( idnull, new File( patternPath ) );
               var idT = charIDToTypeID( "T " );
               var list10 = new ActionList();
            
               var ref31 = new ActionReference();
               var idPtrn = charIDToTypeID( "Ptrn" );
            
               ref31.putIndex( idPtrn, patternIndex );
               list10.putReference( ref31 );
               desc74.putList( idT, list10 );
               executeAction( idsetd, desc74, DialogModes.NO );
            }     
            
            function getPatternIndexByName(match) {
                try {
                    var r = new ActionReference();
                    r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("presetManager"));
                    r.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
            
            
                    var list = executeActionGet(r).getList(stringIDToTypeID("presetManager"));
            
            
                    for (var i = 0; i < list.count; i++) {
                        if (list.getObjectType(i) == charIDToTypeID("PttR")) {
                            var list2 = list.getObjectValue(i).getList(stringIDToTypeID("name"));
            
            
                            for (var x = 0; x < list2.count; x++) {
                                var name = list2.getString(x);
                                if (name.indexOf(match) >= 0) return x + 1;
                            }
            
            
                            break;
                        }
                    }
            
            
                    return -1;
                }
                catch (e) {
                    alert(e);
                    return -1;
                }
            }