5 Replies Latest reply on Nov 7, 2017 3:08 AM by joesickspack

    JS Scripting the Quick Selection Tool

    RaquelWelch

      Using the ScriptListener plug-in, I see that making a selection with the Quick Selection Tool produces this code:

       

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

      var idslct = charIDToTypeID( "slct" );

          var desc3 = new ActionDescriptor();

          var idnull = charIDToTypeID( "null" );

              var ref1 = new ActionReference();

              var idquickSelectTool = stringIDToTypeID( "quickSelectTool" );

              ref1.putClass( idquickSelectTool );

          desc3.putReference( idnull, ref1 );

      executeAction( idslct, desc3, DialogModes.NO );

       

       

      This makes sense. But, I don't see how the location of the selection I made is passed into the ActionDescriptor (desc3) used for the executeAction() call.

       

      Basically, I would like to be able to programmatically set the location of a Quick Selection, in JS.

       

      Any tips for how to set that information into the ActionDescriptor?

      (Or...is it not possible to set that info for a tool like Quick Selection?)

       

      Thanks++

        • 1. Re: JS Scripting the Quick Selection Tool
          JJMack Most Valuable Participant

          Sorry

           

          With CS6 you  may be able to record the use of the quick selection tool in an action. However you can not use the scriptlistener and record the use of tools. Installing the scriptlistener in CS6  disable Photoshop CS6 feature of recording the use of Photoshop's tools.

           

          What you see recorded by the scriptlistener the is the selection of the Quick selection tool not its use. 

           

          Versions of Photoshop prior to CS6 did not have the ability of recording the use of Photoshop tools in actions. Recording things like brush stroks is not posible in versions prior to CS6.

           

          The Scriptlistener Plugin in CS6 must have been updated to disable CS6 ability to record things like brush strokes rather then being updating to be compatable with CS6 features.

           

          Tool recording in CS6 was not implmented well at all.  More data is recorded on fast machines then on slow machines, Action recorded on fast machines preform so slowly on slow machines they are not useable.

           

           

          You May be able to program a script in CS6 that uses an action you recorded using a some stroke with the quick selection tool. Be Aware the size and resolution of the current document  would most likely need to be the same pixel size and resolution that you recorded the action on.

          • 2. Re: JS Scripting the Quick Selection Tool
            c.pfaffenbichler Level 9

            You might be able to create a Work Path and then use

            PathItem.strokePath (tool: ToolType , simulatePressure: Boolean )

            • 3. Re: JS Scripting the Quick Selection Tool
              c.pfaffenbichler Level 9

              There seems to be no ToolType in the DOM for the Quick Selection Tool, so ScriptingListener code may be necessary after all.

              What JJMack said on sizes and resolutions naturally applies.

               

              // use quick selection tool;
              // 2012; use it at your own risk;
              #target photoshop
              if (app.documents.length > 0) {
              var myDoc = app.activeDocument;
              // create path;
              var thePath = createPath ([[ [[70,70]], [[80,80]] ]], "delete this path");
              // stroke path;
              // =======================================================
              var idStrk = charIDToTypeID( "Strk" );
                  var desc2 = new ActionDescriptor();
                  var idnull = charIDToTypeID( "null" );
                      var ref2 = new ActionReference();
                      var idPath = charIDToTypeID( "Path" );
                      var idOrdn = charIDToTypeID( "Ordn" );
                      var idTrgt = charIDToTypeID( "Trgt" );
                      ref2.putEnumerated( idPath, idOrdn, idTrgt );
                  desc2.putReference( idnull, ref2 );
                  var idUsng = charIDToTypeID( "Usng" );
                  var idquickSelectTool = stringIDToTypeID( "quickSelectTool" );
                  desc2.putClass( idUsng, idquickSelectTool );
                  var idPrs = charIDToTypeID( "Prs " );
                  desc2.putBoolean( idPrs, true );
              executeAction( idStrk, desc2, DialogModes.NO );
              // remove path;
              thePath.remove();
              };
              ////// function to create path from array with one array per point that holds anchor, leftdirection, etc, 2010 //////
              function createPath (theArray, thePointsName) {
              var originalRulerUnits = app.preferences.rulerUnits;
              app.preferences.rulerUnits = Units.POINTS;
              lineSubPathArray = new Array ();
              if (theArray[theArray.length - 1].constructor != Array) {var numberOfPoints = theArray.length - 1}
              else {var numberOfPoints = theArray.length};
              for (var b = 0; b < numberOfPoints; b++) {
                        var lineArray = new Array ();
                        for (c = 0; c < (theArray[b].length); c++) {
                                  lineArray[c] = new PathPointInfo;
                                  if (!theArray[b][c][3]) {lineArray[c].kind = PointKind.CORNERPOINT}
                                  else {lineArray[c].kind = theArray[b][c][3]};
                                  lineArray[c].anchor = theArray[b][c][0];
                                  if (!theArray[b][c][1]) {lineArray[c].leftDirection = theArray[b][c][0]}
                                  else {lineArray[c].leftDirection = theArray[b][c][1]};
                                  if (!theArray[b][c][2]) {lineArray[c].rightDirection = theArray[b][c][0]}
                                  else {lineArray[c].rightDirection = theArray[b][c][2]};
                                  };
                        lineSubPathArray[b] = new SubPathInfo();
                        lineSubPathArray[b].operation = ShapeOperation.SHAPEADD;
                        if (theArray[b][theArray[b].length - 1].constructor == Array) {lineSubPathArray[b].closed = true}
                        else {lineSubPathArray[b].closed = theArray[b][theArray[b].length - 1]};
                        lineSubPathArray[b].entireSubPath = lineArray;
                        };
              var myPathItem = app.activeDocument.pathItems.add(thePointsName, lineSubPathArray);
              app.preferences.rulerUnits = originalRulerUnits;
              return myPathItem
              };
              
              1 person found this helpful
              • 4. Re: JS Scripting the Quick Selection Tool
                c.pfaffenbichler Level 9

                RaquelWelch, did the Script provide a soliution for the issue?

                • 5. Re: JS Scripting the Quick Selection Tool
                  joesickspack Level 1

                  Five years later, but your script works beautifully. Ran it on a batch of 80 images. Thank you so much.

                   

                  All I did was comment out #target photoshop (it caused an error), change the units to pixels (although points seemed to work also),  multiply the coords of my path by .06 (can't figure out why that was necessary--some kind of units thing maybe), and call it from an AppleScript.

                   

                  [Figured out the .06 business. My files are 1200 dpi. The script somehow is assuming 72 dpi. .06*1200 = 72]

                   

                  set themargin to 50

                  tell application "Finder"

                    set myPath to choose folder

                    set myFileList to entire contents of (myPath) as alias list

                  end tell

                   

                  tell application "Adobe Photoshop CS4"

                    activate

                    repeat with theFile in myFileList

                    open theFile

                    tell current document

                    set themult to 72 / resolution

                    set themargin to themargin * themult

                  set thewidth to width * themult

                    set theheight to height * themult

                   

                    end tell

                    set thescript to "// use quick selection tool;

                  // 2012; use it at your own risk;

                  // #target photoshop

                  if (app.documents.length > 0) {

                  var myDoc = app.activeDocument;

                  // create path;

                  var themargin = " & themargin & "

                  var thewidth = " & thewidth & "

                  var theheight = " & theheight & "

                  var thePath = createPath ([[ [[themargin, themargin]], [[thewidth - themargin, themargin]], [[thewidth - themargin,theheight - themargin]], [[themargin, theheight - themargin]] ]], 'delete this path');

                  // stroke path;

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

                  var idStrk = charIDToTypeID( 'Strk' );

                      var desc2 = new ActionDescriptor();

                      var idnull = charIDToTypeID( 'null' );

                          var ref2 = new ActionReference();

                          var idPath = charIDToTypeID( 'Path' );

                          var idOrdn = charIDToTypeID( 'Ordn' );

                          var idTrgt = charIDToTypeID( 'Trgt' );

                          ref2.putEnumerated( idPath, idOrdn, idTrgt );

                      desc2.putReference( idnull, ref2 );

                      var idUsng = charIDToTypeID( 'Usng' );

                      var idquickSelectTool = stringIDToTypeID( 'quickSelectTool' );

                      desc2.putClass( idUsng, idquickSelectTool );

                      var idPrs = charIDToTypeID( 'Prs ' );

                      desc2.putBoolean( idPrs, true );

                  executeAction( idStrk, desc2, DialogModes.NO );

                  // remove path;

                  thePath.remove();

                  };

                  ////// function to create path from array with one array per point that holds anchor, leftdirection, etc, 2010 //////

                  function createPath (theArray, thePointsName) {

                  var originalRulerUnits = app.preferences.rulerUnits;

                  app.preferences.rulerUnits = Units.PIXELS;

                  lineSubPathArray = new Array ();

                  if (theArray[theArray.length - 1].constructor != Array) {var numberOfPoints = theArray.length - 1}

                  else {var numberOfPoints = theArray.length};

                  for (var b = 0; b < numberOfPoints; b++) {

                            var lineArray = new Array ();

                            for (c = 0; c < (theArray[b].length); c++) {

                                      lineArray[c] = new PathPointInfo;

                                      if (!theArray[b][c][3]) {lineArray[c].kind = PointKind.CORNERPOINT}

                                      else {lineArray[c].kind = theArray[b][c][3]};

                                      lineArray[c].anchor = theArray[b][c][0];

                                      if (!theArray[b][c][1]) {lineArray[c].leftDirection = theArray[b][c][0]}

                                      else {lineArray[c].leftDirection = theArray[b][c][1]};

                                      if (!theArray[b][c][2]) {lineArray[c].rightDirection = theArray[b][c][0]}

                                      else {lineArray[c].rightDirection = theArray[b][c][2]};

                                      };

                            lineSubPathArray[b] = new SubPathInfo();

                            lineSubPathArray[b].operation = ShapeOperation.SHAPEADD;

                            if (theArray[b][theArray[b].length - 1].constructor == Array) {lineSubPathArray[b].closed = true}

                            else {lineSubPathArray[b].closed = theArray[b][theArray[b].length - 1]};

                            lineSubPathArray[b].entireSubPath = lineArray;

                            };

                  var myPathItem = app.activeDocument.pathItems.add(thePointsName, lineSubPathArray);

                  app.preferences.rulerUnits = originalRulerUnits;

                  return myPathItem

                  };"

                   

                   

                    do javascript thescript --show debugger on runtime error

                    tell current document

                    do action "Save Selection as KO" from "My Actions"

                    save

                    close

                    end tell

                    end repeat

                  end tell