6 Replies Latest reply on Feb 21, 2018 3:31 PM by JJMack

    Long code optimizing

    teslaball

      Hello,

      in the below code its function is to load a tool preset, selecting certain preset, and then delete the whole tool preset to keep the archive clean.

      The code is functioning OK except two things - first, it cycles through all the presets which takes time, and second the code looks a bit long.

      What I need is if someone kindly could help me optimize(shorten) the code if possible, so the loading, selecting and deleting time to be shorter than now:

       

      function tool_one() {
          function step1(enabled, withDialog) {
              if (enabled != undefined && !enabled) {
                  return;
              }
              var dialogMode = withDialog ? DialogModes.ALL : DialogModes.NO;
              var desc1 = new ActionDescriptor();
              var ref1 = new ActionReference();
              var desc2 = new ActionDescriptor();
              var ref2 = new ActionReference();
              var desc3 = new ActionDescriptor();
              var ref3 = new ActionReference();
              var desc4 = new ActionDescriptor();
              var ref4 = new ActionReference();
              var desc5 = new ActionDescriptor();
              var ref5 = new ActionReference();
              var desc6 = new ActionDescriptor();
              var ref6 = new ActionReference();
              var desc7 = new ActionDescriptor();
              var ref7 = new ActionReference();
              var desc8 = new ActionDescriptor();
              var ref8 = new ActionReference();
              var desc9 = new ActionDescriptor();
              var ref9 = new ActionReference();
              var desc10 = new ActionDescriptor();
              var ref10 = new ActionReference();
              var desc11 = new ActionDescriptor();
              var ref11 = new ActionReference();
              var desc12 = new ActionDescriptor();
              var ref12 = new ActionReference();
              var desc13 = new ActionDescriptor();
              var ref13 = new ActionReference();
              var desc14 = new ActionDescriptor();
              var ref14 = new ActionReference();
              var desc15 = new ActionDescriptor();
              var ref15 = new ActionReference();
              var desc16 = new ActionDescriptor();
              var ref16 = new ActionReference();
              var desc17 = new ActionDescriptor();
              var ref17 = new ActionReference();
              var desc18 = new ActionDescriptor();
              var ref18 = new ActionReference();
              ref1.putProperty(cTID("Prpr"), sTID("toolPreset"));
              ref1.putEnumerated(cTID("capp"), cTID("Ordn"), cTID("Trgt"));
              ref3.putEnumerated(sTID("toolPreset"), cTID("Ordn"), cTID("Trgt"));
              desc1.putReference(cTID("null"), ref1);
              desc1.putPath(cTID("T   "), new File(app.path + "/Presets/Tools/sixteentools.tpl"));
              desc3.putReference(cTID("null"), ref3);
              ref2.putName(sTID("toolPreset"), "t_one");
              ref4.putName(sTID("toolPreset"), "t_two");
              ref5.putName(sTID("toolPreset"), "t_three");
              ref6.putName(sTID("toolPreset"), "t_four");
              ref7.putName(sTID("toolPreset"), "t_five");
              ref8.putName(sTID("toolPreset"), "t_six");
              ref9.putName(sTID("toolPreset"), "t_seven");
              ref10.putName(sTID("toolPreset"), "t_eight");
              ref11.putName(sTID("toolPreset"), "t_nine");
              ref12.putName(sTID("toolPreset"), "t_ten");
              ref13.putName(sTID("toolPreset"), "t_eleven");
              ref14.putName(sTID("toolPreset"), "t_twelve");
              ref15.putName(sTID("toolPreset"), "t_thirteen");
              ref16.putName(sTID("toolPreset"), "t_fourteen");
              ref17.putName(sTID("toolPreset"), "t_fifteen");
              ref18.putName(sTID("toolPreset"), "t_sixteen");
              desc1.putBoolean(cTID("Appe"), true);
              desc2.putReference(cTID("null"), ref2);
              desc4.putReference(cTID('null'), ref4);
              desc5.putReference(cTID('null'), ref5);
              desc6.putReference(cTID('null'), ref6);
              desc7.putReference(cTID('null'), ref7);
              desc8.putReference(cTID('null'), ref8);
              desc9.putReference(cTID('null'), ref9);
              desc10.putReference(cTID('null'), ref10);
              desc11.putReference(cTID('null'), ref11);
              desc12.putReference(cTID('null'), ref12);
              desc13.putReference(cTID('null'), ref13);
              desc14.putReference(cTID('null'), ref14);
              desc15.putReference(cTID('null'), ref15);
              desc16.putReference(cTID('null'), ref16);
              desc17.putReference(cTID('null'), ref17);
              desc18.putReference(cTID('null'), ref18);
              executeAction(cTID("setd"), desc1, dialogMode);
              executeAction(cTID("slct"), desc2, dialogMode);
              executeAction(cTID("Dlt "), desc3, dialogMode);
              executeAction(cTID('slct'), desc4, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc5, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc6, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc7, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc8, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc9, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc10, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc11, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc12, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc13, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc14, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc15, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc16, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc17, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
              executeAction(cTID('slct'), desc18, dialogMode);
              executeAction(cTID('Dlt '), desc3, dialogMode);
          }
          app.bringToFront();
          step1();
      };
      

       

      Thanks.

        • 1. Re: Long code optimizing
          JJMack Most Valuable Participant

          That code seems to fo 30+ action manager steps.  I see no code to test it the tools presets are loaded on or not every time the script is used I think it would load and additional copy of sixteentools.tpl I think that would duplicate presets that may be loaded.

           

          You may wamt to look at Cycle through Tool Presets with shortcut?

          • 2. Re: Long code optimizing
            teslaball Level 1

            Thank you JJMack but I am not trying to achieve cycling through presets. Instead, the script is working OK as I said (the .tpl name and presets names are generic).

             

            What it does is, when clicking on a button, it loads the .tpl (in this case sixteentools.tpl as a generic name), selects an assigned preset and then it deletes all loaded presets contained in the .tpl  to save up space in the archive.

             

            In the above example I made a mistake, the function have to be named tool_sixteen instead of tool_one, because it selects t_sixteen preset as a last one (I think the code reads in a synchronous manner). When selection is made, it cycles through all sixteen presets, it even is visible what it selects when cycling through and takes up time to finish.

             

            What I am trying to achieve is to optimize the code (which of course I don't know how and need help on that), first to shorten it if possible and second to speed up loading of a preset.

             

            I want to do this from one .tpl file because if I separate each preset in its on .tpl it will consume much more HDD(SDD) space. Another reason for optimizing the code.

             

            Thanks

            • 3. Re: Long code optimizing
              JJMack Most Valuable Participant

              I'm sorry I do not understand what your code is doing. All looks hard coded with specific names I do not understand what you mean by as a generic name.  If the tpl file does not contain the hard coded names I would expect the script to generate an error and fails in one of the select steps.

               

              I do not understant the action manager code you posted I do not see code changing for example descripter  desc3 I see it used with function "Dlt " many times why is that Action manager code to delet targeted preset in the preset mangers ui.  I find it very hard to understand Action manager code.  To me its look like the script may loads as set of presets then prodceed to delete 15 tool presets loaded.  I do not see it changing anything on HDD(SSD).

               

              1.         executeAction(cTID("setd"), desc1, dialogMode); 
              2.         executeAction(cTID("slct"), desc2, dialogMode); 
              3.         executeAction(cTID("Dlt "), desc3, dialogMode); 
              4.         executeAction(cTID('slct'), desc4, dialogMode); 
              5.         executeAction(cTID('Dlt '), desc3, dialogMode); 
              6.         executeAction(cTID('slct'), desc5, dialogMode); 
              7.        executeAction(cTID('Dlt '), desc3, dialogMode); 
              8.         executeAction(cTID('slct'), desc6, dialogMode); 
              9.         executeAction(cTID('Dlt '), desc3, dialogMode); 
              10.         executeAction(cTID('slct'), desc7, dialogMode); 
              11.         executeAction(cTID('Dlt '), desc3, dialogMode); 
              12.         executeAction(cTID('slct'), desc8, dialogMode); 
              13.         executeAction(cTID('Dlt '), desc3, dialogMode);
              14.         executeAction(cTID('slct'), desc9, dialogMode); 

               

              • 4. Re: Long code optimizing
                Jarda Bereza Level 4

                You should extract part of code into new function and pass preset name as argument:

                So you will need one ref, one desc and one executeAction.

                And you will store names in array and "for loop" will call this function in each cycle and pass argument from array at specified index.

                 

                Pseudocode:

                 

                function doIt(nameVariable){

                     var desc18 = new ActionDescriptor(); 

                     var ref18 = new ActionReference(); 

                     desc5.putReference(cTID('null'), ref5); 

                  ref9.putName(sTID("toolPreset"), nameVariable);

                     executeAction(cTID("slct"), desc2, dialogMode); 

                     executeAction(cTID("Dlt "), desc3, dialogMode); 

                 

                }

                1 person found this helpful
                • 5. Re: Long code optimizing
                  teslaball Level 1

                  Please, can you be more specific by explaining through an example how to create the for loop? I was trying for so long without success.

                  Thank you,

                  teslaball

                  • 6. Re: Long code optimizing
                    JJMack Most Valuable Participant

                    Cycle Tool Presets Package Added Sept. 6, 2017

                    Contains

                    • CycleTool.jsx - Tool Presets cycling function need to be included in your Tool Presets script
                      Include four Tool Presets cycling scripts for four free tools Presets sets I downloads from the web
                    • CycleBrushes.jsx - Cycle presets in tool preset set the_sketch_arsenal_by_thatld.tpl
                    • the_sketch_arsenal_by_thatld.tpl - need to be in the scripts folder
                    • CycleMixerBrushes.jsx - Cycle presets in tool preset set Yngvar_BasicMixerBrushes.tpl
                    • Yngvar_BasicMixerBrushes.tpl - need to be in the scripts folder
                    • CyclePencilBrushes.jsx- Clocker presets in tool preset set CS1_Pencil_Presets.tpl
                    • CS1_Pencil_Presets.tpl - need to be in the scripts folder
                    • CycleWaterBrushes.jsx- Cycle presets in tool preset set alenah_watercolor511(set_of_3_brushes).tpl
                    • alenah_watercolor511(set_of_3_brushes).tpl - need to be in the scripts folder
                    • CycleToolsPresets.html- This brief write up
                    • CycleBrushes.gif - Animated Gif use in the above html file

                    Download

                    Tool Presets Usage - I find using tools presets can be awkward. You need to pull-down the Tools preset list from Photoshop's option bar then find and select the preset you want to use. If you have unchecked current tool only so you can select any tool's presets from the pull-down list or to be able to select any tools presets in actions and scripts. Your list of presets can be quite large and finding what you want to use can take some time. Even when you have current tool only checked the presets list can be long. Using Photoshop reset presets can help you to keep your presets list short. But then you will need to load or reload Presets you want to use. If your are a painter you may want to switch between brushes often, quickly and easily in a subset of your brushes for the painting you are doing.

                     

                    Scripting can help here but because of the way Photoshop works that script can get complex for most likely you do not want to just rotate through big set of brushes. You may want to switch back and forth between brushes in a set of brushes. Actually I really do not know for I'm not a painters but, I have seen many make set of brush presets available on the web for others to use. Like at DeviantArt Brushes

                     

                    I also see instruction for installing presets. Because I have a programming background I thought I would make an attempt at making it easier for users to load and switch between a tools presets.

                     

                    In my opinion you should use Adobe Photoshop Alt+Right-click dragging up, down and left,right feature to change your paint brush tip size and hardness. You should use paint brush preset for switching between types of brushes with some useful size.

                     

                    The script I came up with is designed to be use via shortcut keys. In fact for full function two shortcut keys are required a F(n) Forward shortcut key and a (Shift/Alt/Ctrl)+F(n) Backward shortcut key. You can create a script for each set of tool presets you wish to switch between. However each script will need two shortcut keys and be for a single tool for a set of its presets. The script can be used for other tools besides Photoshop's paint brush tool. You only need to customize the provided sample script for your preset sets. Three items need to be set the cycle script function will do all the work. The script is well commented is should be easy to understand. The script checks Photoshop's current tool and if is the correct tool for the Presets the tools settings will be set using your next or previous Tool's Preset else Photoshop current tool will be changed to the correct tool. If you have set up your workspace so your Brush Palette is docked as an Auto-collapse Iconic panel there is a commented out if statement in the script you can uncomment to have the script open the Brush palette when it switches Photoshop's current tool so you will see its feedback.

                    1. Photoshop Tool Name - The tool the presets are for. A list of Photoshop internal tools names is included at the bottom of the script file.
                    2. Photoshop Presets Set - The Set name of your Photoshop's tool's presets. Your *.tpl file is required to be in your scripts folder.
                    3. Ordered Preset Names List - The list of the preset names in the set you want to use via shortcuts.

                    If the Photoshop tool is the Paint Brush tool Photoshop will provide visual feedback via the tool option bar brush preview and also in the Photoshop Brush palette.
                    [Visual Feedback]