7 Replies Latest reply on Oct 9, 2014 8:32 AM by c.pfaffenbichler

    Return to Previous Tool

    boo radley 2013 Level 3

      I've managed to assemble a small script which:

       

       

      a) gets the current tool (brush tool, mixer tool, smudge tool, etc - whatever it may be- but let's assume that it's not the eraser tool). Then, an alert will pop up, which tells us what the current tool is

       

      b) After hitting 'OK', the script then switches to the eraser tool

       

       

      ..now I'm trying to add a 3rd and last piece to ther script: how do I re-select the first tool that the script captured in Step a)? (assuming it wasn't the eraser tool)

       

      // =======================================================
      alert(currentTool());
      function currentTool(){
      var ref = new ActionReference(); 
      ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID
      
      
      ("Ordn"), charIDToTypeID("Trgt") ); 
      var cTool = typeIDToStringID(executeActionGet
      
      
      (ref).getEnumerationType(stringIDToTypeID('tool')));
      return cTool;
      }
      // =======================================================
      var idslct = charIDToTypeID( "slct" );
          var desc3337 = new ActionDescriptor();
          var idnull = charIDToTypeID( "null" );
              var ref307 = new ActionReference();
              var idErTl = charIDToTypeID( "ErTl" );
              ref307.putClass( idErTl );
          desc3337.putReference( idnull, ref307 );
      executeAction( idslct, desc3337, DialogModes.NO );
      // =======================================================
      
      
      
        • 1. Re: Return to Previous Tool
          c.pfaffenbichler Level 9

          I guess can store that information in the meantime (txt-file, custom namespace, …) and have the second Script evaluate that and then perform the corresponding operation.

           

          Maybe you could just use one Script for this, though.

          If the Eraser Tool is selected do the one thing, else wise the other.

          • 2. Re: Re: Return to Previous Tool
            boo radley 2013 Level 3

            I was thinking that when the alert shows up indicating the current tool (brush tool for example), that I would need to immediately store that as a variable, so something like (var toolnow = currentTool), and then call that variable as the final step near the end of the script.  Something like this:

             

            // =======================================================
            alert(currentTool()); 
            var toolnow = currentTool
            function currentTool(){ 
            var ref = new ActionReference();  
            ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID
            
            
            ("Ordn"), charIDToTypeID("Trgt") );  
            var cTool = typeIDToStringID(executeActionGet
            
            
            (ref).getEnumerationType(stringIDToTypeID('tool'))); 
            return cTool; 
            } 
            // =======================================================
            var idslct = charIDToTypeID( "slct" );
                var desc3337 = new ActionDescriptor();
                var idnull = charIDToTypeID( "null" );
                    var ref307 = new ActionReference();
                    var idErTl = charIDToTypeID( "ErTl" );
                    ref307.putClass( idErTl );
                desc3337.putReference( idnull, ref307 );
            executeAction( idslct, desc3337, DialogModes.NO );
            // =======================================================
            currentTool = toolnow
            
            

             

            I know this script isn't correct, but you get the idea of where I'm going with the last line. The problem is that the current tool could be anything (like brush, eraser, lasso, paintbucket, etc). I've found a way to do this with other scripting language, which picks up the name of the current tool in the alert box (it does this by searching the text in the alert box for the current tool name), but it's too long of a workaround. I was hoping that when the alert box pops up, it means that Photoshop searched for the current tool in use, so there might be a way to store that current tool to recall it later.. again by current tool I mean just brush, eraser, crop,etc. and not their preset names or anything like that

            • 3. Re: Return to Previous Tool
              JJMack Most Valuable Participant

              I had trouble with your script using CC 2014..   I could not figure out. However it has to do with "currentTool"  as the function name string.  The script worked when I change the function name.  Its like currentTool is a reserved word.

               

              I have written script that I use in Actions that are used twice processing a document. I use the document's metadata as a scratch pad area.   The scripts are more or less store and restore functions for actions. So there is no need for a text file if a single document is being processed. If several documents could be involved you could also use a temp work document. If the document does not exist in the document ring you create it new and store the information. If it does exist in the document ring  restore the stored state.   I have scripted the clipboard like that. for screen captures . If a document named clipboard exists I will paste the clipboard into it  and expand the canvas at to bottom and to the right if need be to house the new layer them position the new layer else I create  a new small document named clipboard paste in expand the the canvas and delete the original background layer..  Have a F key set to use the clipboard...

               

               

              For example:

              /* ======================================================================================

              // 2009  John J. McAssey (JJMack)  http://www.mouseprints.net/

              //

              // This script is supplied as is. It is provided as freeware.

              // The author accepts no liability for any problems arising from its use.

              //

              // This script is designed to be used by a Photoshop Action twice

              // A good pratice to use when creating an actions that use this scipt is for the action

              // not to do a save or play some other action between its two useages of this Script.

              //

              // The first time this script is used by an action the documents current resolution

              // and ruler units are saved into the document's meta-data Info Instructions field.

              //

              // The second time this script used by the action the script retreives what was

              // saved in the meta-data during the first usage, resolution and ruler units.

              // The document is resized to the saved resolution,

              // Photoshop's ruler units are set to saved units

              // and the saved data is removed from the document's meta-data Info Instructions field.

              //

              // ===================================================================================== */

               

               

              /*

              <javascriptresource>

              <about>$$$/JavaScripts/SaveAndRestoreResolution/About=JJMack's SaveAndRestoreResolution.^r^rCopyright 2009 Mouseprints.^r^rRun twice script utility for action.^rNOTE:Don't play other actions between runs!^r^rFirst Run records Photoshop's preferences Units and documents DPI resolution.^rSecond Run restores the recorded setting and removes the recording.</about>

              <category>JJMack's Action Run Twice Utility</category>

              </javascriptresource>

              */

               

               

              if (app.documents.length > 0) {

                if (app.activeDocument.info.instructions.indexOf("<Resolution>") == -1 ){ // no footprint fisrt useage

                //alert("first");

                // Retreive Document information for Foot Print

                              var units = app.preferences.rulerUnits;

                app.preferences.rulerUnits = Units.PIXELS; // set ruler units to PIXELS

                              var typeunits = app.preferences.typeUnits;

                var res = app.activeDocument.resolution;

               

               

                // put footprint in metadata info instructions

                app.activeDocument.info.instructions = app.activeDocument.info.instructions + "<Units>" + units + "</Units>"  + "<Tunits>" + typeunits + "</Tunits>" + "<Resolution>" + res + "</Resolution>";

                //alert( "Saved ="  + "<Units>" + units + "</Units>" + "<Tunits>" + typeunits + "</Tunits>" + "<Resolution>" + res + "</Resolution>" );

               

               

                app.preferences.rulerUnits = units; // restore ruler units

                }

                else {

                //alert("second");

               

               

                // Retreive saved information

                unitsOffset = app.activeDocument.info.instructions.indexOf("<Units>") + "<Units>".length;

                unitsLength = app.activeDocument.info.instructions.indexOf("</Units>") -unitsOffset;

                savedUnits = app.activeDocument.info.instructions.substr(unitsOffset, unitsLength);

               

               

                tunitsOffset = app.activeDocument.info.instructions.indexOf("<Tunits>") + "<Tunits>".length;

                tunitsLength = app.activeDocument.info.instructions.indexOf("</Tunits>") -tunitsOffset;

                savedTunits = app.activeDocument.info.instructions.substr(tunitsOffset, tunitsLength);

               

               

                resOffset = app.activeDocument.info.instructions.indexOf("<Resolution>") + "<Resolution>".length;

                resLength = app.activeDocument.info.instructions.indexOf("</Resolution>") + -resOffset;

                savedResolution = app.activeDocument.info.instructions.substr(resOffset, resLength);

                //alert("Resolution = " + savedResolution + " Units = " + savedUnits );

               

               

                // Restore resolution

                app.preferences.rulerUnits = Units.PIXELS;

                activeDocument.resizeImage(null, null, savedResolution, ResampleMethod.NONE);

               

               

                // Restore ruler units

                // I get a message Enumerated value expected if I try to use var savedUnits app.preferences.rulerUnits = savedUnits;

                // perhaps if I knew Javascript I would not need to use the following if else if .....

                if ( savedUnits == "Units.INCHES" ){ app.preferences.rulerUnits = Units.INCHES;}

                else if ( savedUnits == "Units.CM" ){ app.preferences.rulerUnits = Units.CM;}

                else if ( savedUnits == "Units.PERCENT" ){ app.preferences.rulerUnits = Units.PERCENT;}

                else if ( savedUnits == "Units.MM" ){ app.preferences.rulerUnits = Units.MM;}

                else if ( savedUnits == "Units.PIXELS" ){ app.preferences.rulerUnits = Units.PIXELS;}

                else if ( savedUnits == "Units.POINTS" ){ app.preferences.rulerUnits = Units.POINTS;}

                else if ( savedUnits == "Units.PICAS" ){ app.preferences.rulerUnits = Units.PICAS;}

               

               

                // Restore Type units

                if ( savedTunits == "TypeUnits.PIXELS" ){ app.preferences.typeUnits = TypeUnits.PIXELS;}

                else if ( savedTunits == "TypeUnits.POINTS" ){ app.preferences.typeUnits = TypeUnits.POINTS;}

                else if ( savedTunits == "TypeUnits.MM" ){ app.preferences.typeUnits = TypeUnits.MM;}

               

               

               

                // Remove footprint from metadata info instructions

                before = app.activeDocument.info.instructions.substr(0,app.activeDocument.info.instructions.indexO f("<Units>"));

                afterOffset = app.activeDocument.info.instructions.indexOf("</Resolution>") + "</Resolution>".length;

                after = app.activeDocument.info.instructions.substr(afterOffset, app.activeDocument.info.instructions.length - afterOffset);

                //alert ("before = " + before + " after = " + after);

                app.activeDocument.info.instructions = before + after;

                }

              }

              else { alert("You must have at least one open document to run this script!"); }

              • 4. Re: Return to Previous Tool
                boo radley 2013 Level 3

                jj, it's throwing an error message:

                 

                Error 25: Expected: ).

                Line: 94

                ->    before = app.activeDocument.info.instructions.substr(0,app.activeDocument.info.instructions.indexO f("<Units>"));

                 

                 

                It looks like it's missing a closing bracket, but when I look at line 94 of the code, both brackets are closed. I can't find the error.

                • 5. Re: Return to Previous Tool
                  JJMack Most Valuable Participant

                  This site messes up paste my script in my editor only has 90 lines....

                  Capture.jpg

                  • 6. Re: Return to Previous Tool
                    JJMack Most Valuable Participant

                    If you want my scripts there in my crafting actions package

                    Crafting Actions Package UPDATED Aug 10, 2014 Added Conditional Action steps to Action Palette Tips.
                    Contains

                    Example
                    Download

                    • 7. Re: Return to Previous Tool
                      c.pfaffenbichler Level 9
                      This site messes up paste my script

                      I’ve also experienced such things – for example pasting code that contains three consecutive spaces but the posted line only has two consecutive spaces …