10 Replies Latest reply on Jul 15, 2018 5:30 AM by Kukurykus

    Script for "fill with pattern" - "use last one used"


      Hello guys, I need a bit of help to automate my work.


      I work with patterns (as a pattern designer), but I need to slightly automate my work.


      There is this great function now "fill with ... pattern" and then you have scripts for those.

      This said what I usually do is that I wokr on a couple of patterns, make the design then test test them on a big format blank file.


      1. I do my drawing, then

      2. click "define as a new pattern" and then

      3. I open a fresh new fbig big file and

      4. use "fill it with... pattern" and

      5. select the last one I created...

      So I tried to create an action / script so that I could you know, work in batches, having to decide which of the patterns are good on a big format only later.



      When I Added exactly the same steps in the actions macro - it will work for the first file, but for the second it will still use the same pattern used previously, as the actions macro sees it as "fill it with pattern XXX" (namely, not as "last one used"), so I land up with all of the pictures in big format filled with the same pattern instead of different ones.


      I was thinking about then modifying the macro so when I g oto step 4

      4. use "fill it with... pattern" and

      add " use script" among the scripts loading there, that would define something like "use the last pattern defined" - can be as well a code as "use newest pattern defined" or "use newest pattern added to the lot"


      I understand the logic but I have no clue how to edit the scripts in this area.


      This will save me hours of work, so all of your help will be more than appreciated.

        • 1. Re: Script for "fill with pattern" - "use last one used"
          SuperMerlin Level 5

          This should fill using the last pattern.


          #target photoshop;
          function main(){
          if(!documents.length) return;
          var patt = new File(Folder.temp + "/tempPatterns");
          if(patt.exists) patt.remove();
          var patterns = getPatternsFromFile(patt);
          fillPattern(patterns[patterns.length-1][0].toString(),patterns[patterns.length-1][1].toString(), 100);
          function savePatterns(file) {
          var desc625 = new ActionDescriptor();
          desc625.putPath( charIDToTypeID('null'), new File( file ) );
          var ref462 = new ActionReference();
          ref462.putProperty( charIDToTypeID('Prpr'), charIDToTypeID('Ptrn') );
          ref462.putEnumerated( charIDToTypeID('capp'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
          desc625.putReference( charIDToTypeID('T   '), ref462 );
          executeAction( charIDToTypeID('setd'), desc625, DialogModes.NO );
          function getPatternsFromFile(file){
            file.encoding = 'BINARY';
            var str = file.read();
            var patterns=[];
            var re = /(\x00\w|\x00\d)(\x00\-|\x00\w|\x00\s|\x00\d)+\x00\x00\$[-a-z\d]+/g;
            var parts = str.match(re);
            for (var i = 0; i < parts.length; i++) {
              var p = parts[i];
              var sp = p.replace(/\x00/g, '').split('$');
               patterns.push([[sp[0]], [sp[1]]]);   
          return patterns;
          function fillPattern(name, id, opacity) { 
              var desc6 = new ActionDescriptor(); 
              desc6.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('FlCn'), charIDToTypeID('Ptrn') ); 
                  var desc7 = new ActionDescriptor(); 
                  desc7.putString( charIDToTypeID('Nm  '), name ); 
                  desc7.putString( charIDToTypeID('Idnt'), id); 
              desc6.putObject( charIDToTypeID('Ptrn'), charIDToTypeID('Ptrn'), desc7 ); 
              desc6.putUnitDouble( charIDToTypeID('Opct'), charIDToTypeID('#Prc'), opacity ); 
              desc6.putEnumerated( charIDToTypeID('Md  '), charIDToTypeID('BlnM'), charIDToTypeID('Nrml') ); 
              executeAction( charIDToTypeID('Fl  '), desc6, DialogModes.NO ); 
          1 person found this helpful
          • 2. Re: Script for "fill with pattern" - "use last one used"
            qwazar1981 Level 1

            thank you - could you save it as a ready to use jsx?

            Or how can I do this?


            Thanks a lot !

            • 3. Re: Script for "fill with pattern" - "use last one used"
              SuperMerlin Level 5

              Here is a link for the jsx file..


              • 4. Re: Script for "fill with pattern" - "use last one used"
                qwazar1981 Level 1

                omng omg omg omg it works perfectly !! you literally saved me hours and hours of manual clicking through the pictures!!! you're a life savior!



                • 5. Re: Script for "fill with pattern" - "use last one used"

                  Hi. I know this post is from a long time ago but any chance you could re-upload a jsx file again from that code.  I have exactly the same need for automating pattern tiling using the most recent pattern.  Much appreciated if you're still out there :-))  

                  • 7. Re: Script for "fill with pattern" - "use last one used"
                    Kukurykus Adobe Community Professional

                    It may be done also this way:


                    function sTT(v) {return stringIDToTypeID(v)}
                    (ref1 = new ActionReference()).putClass(p = sTT('pattern'));
                    (dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1);
                    (ref2 = new ActionReference()).putProperty(sTT('property'), sTT('selection'))
                    ref2.putEnumerated(sTT('document'), sTT('ordinal'), sTT('targetEnum'))
                    dsc1.putReference(u = sTT('using'), ref2), dsc1.putString(n = sTT('name'), '')
                    res = executeAction(sTT('make'), dsc1, DialogModes.ALL);
                    (dsc1 = new ActionDescriptor()).putEnumerated(u, sTT('fillContents'), p);
                    documents.add(), (dsc2 = new ActionDescriptor()).putString(n, res.getString(n))
                    dsc1.putObject(p, p, dsc2), dsc1.putUnitDouble(sTT('opacity'), sTT('percentUnit'), 100)
                    dsc1.putEnumerated(sTT('mode'), sTT('blendMode'), sTT('normal'))
                    executeAction(sTT('fill'), dsc1, DialogModes.NO)



                    If you need other document dimension just insert appriopate values to documents.add(), or use:


                    documents.add(UnitValue((a = String($.screens).split(/[^\d+]/))[2], 'px'), UnitValue(a[3], 'px'), activeDocument.resolution)
                    • 8. Re: Script for "fill with pattern" - "use last one used"
                      josephf8809928 Level 1

                      Thank you so much     .  Very very much appreciated.

                      • 9. Re: Script for "fill with pattern" - "use last one used"

                        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';
                        var lastPatternIndex = getPatternIndexByName(patternName);
                        exportPattern(lastPatternIndex, patternPath);
                        function definePattern(patternName) {
                           var dialogMode = DialogModes.NO;
                           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 '), 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;
                                return -1;
                            catch (e) {
                                return -1;
                        • 10. Re: Script for "fill with pattern" - "use last one used"
                          Kukurykus Adobe Community Professional

                          Wouldn't be it better to link a topic where you just posted it?