Skip to main content
Participant
August 20, 2012
Question

JS Scripting the Quick Selection Tool

  • August 20, 2012
  • 4 replies
  • 2652 views

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++

This topic has been closed for replies.

4 replies

c.pfaffenbichler
Community Expert
Community Expert
August 30, 2012

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

c.pfaffenbichler
Community Expert
Community Expert
August 21, 2012

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.length); c++) {

                    lineArray = new PathPointInfo;

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

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

                    lineArray.anchor = theArray[0];

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

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

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

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

                    };

          lineSubPathArray = new SubPathInfo();

          lineSubPathArray.operation = ShapeOperation.SHAPEADD;

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

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

          lineSubPathArray.entireSubPath = lineArray;

          };

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

app.preferences.rulerUnits = originalRulerUnits;

return myPathItem

};

Inspiring
November 7, 2017

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.length); c++) {

                    lineArray = new PathPointInfo;

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

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

                    lineArray.anchor = theArray[0];

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

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

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

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

                    };

          lineSubPathArray = new SubPathInfo();

          lineSubPathArray.operation = ShapeOperation.SHAPEADD;

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

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

          lineSubPathArray.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

c.pfaffenbichler
Community Expert
Community Expert
August 21, 2012

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

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

JJMack
Community Expert
Community Expert
August 21, 2012

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.

JJMack