get a list of points used in a Lasso selection from a CEP event

Contributor ,
Feb 25, 2020 Feb 25, 2020

Copy link to clipboard

Copied

Hi, I wonder if it's possible to get a list of points used in a setd event from Lasso tool? I'm sending an ID of the event from my panel and then on he JSX side I'm doing

 

var myDescriptor = new ActionDescriptor();
myDescriptor.fromID(_id);

 

 But where do I go from there? I've tried...

 

myDescriptor.getObjectType(cTID('T   ')); // error, was expecting to get 'Plgn'
myDescriptor.getObjectValue(cTID('T   ')); // error, was expecting to get a new ActionDescriptor
var ref = myDescriptor.getReference(myDescriptor.getKey(0)); // error, was expecting... something
var ref = myDescriptor.getReference(cTID("null")); // error...

 

 

Here's an example of using a lasso tool in an action:

 

var desc11 = new ActionDescriptor();
    var ref3 = new ActionReference();
    ref3.putProperty( cTID('Chnl'), cTID('fsel') );
desc11.putReference( cTID('null'), ref3 );
    var desc12 = new ActionDescriptor();
        var list1 = new ActionList();
            var desc13 = new ActionDescriptor();
            desc13.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), 71.500000 );
            desc13.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), 133.000000 );
        list1.putObject( cTID('Pnt '), desc13 );
            var desc14 = new ActionDescriptor();
            desc14.putUnitDouble( cTID('Hrzn'), cTID('#Pxl'), 71.500000 );
            desc14.putUnitDouble( cTID('Vrtc'), cTID('#Pxl'), 133.000000 );
        list1.putObject( cTID('Pnt '), desc14 );
    desc12.putList( cTID('Pts '), list1 );
desc11.putObject( cTID('T   '), cTID('Plgn'), desc12 );
desc11.putBoolean( cTID('AntA'), true );
executeAction( cTID('setd'), desc11, DialogModes.NO );

 

So I was expecting to get ActionList from the second descriptor. Any advices?

TOPICS
Actions and scripting, How to, Problem or error

Views

327

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Feb 27, 2020 Feb 27, 2020
>> What does alert(myDescriptor.count) show?>An error.... This is strange and by and large cannot be. What kind of error?What this code shows and where the error occurs. var myDescriptor = new ActionDescriptor(); alert(myDescriptor.count); myDescriptor.fromID(_id); alert(_id + "\n\n" + typeof(_id)); alert(myDescriptor); alert(myDescriptor.count); What version of photoshop?  

Likes

translate

Translate

Translate
Adobe Community Professional ,
Feb 25, 2020 Feb 25, 2020

Copy link to clipboard

Copied

What does alert(myDescriptor.count) show?

What is the _id?
What does the fromID() method do? Where did you get the description of this method?
 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 27, 2020 Feb 27, 2020

Copy link to clipboard

Copied

What does alert(myDescriptor.count) show?

An error. However I'm using the same method to deconstruct a select event with the following code:

myDescriptor.fromID(_id);

var ref = myDescriptor.getReference(myDescriptor.getKey(0));

which also give and error when I try to read .count property but I can get a name with ref.getName(). However in the case of setd .getKey(0) gives me an error. Maybe the descriptor is just  empty 😞

 

> What is the _id?

my PhotoshopCallback handler gets an event, in event data there's a string like this:  '1936028772, 123456' where the first number is the eventID (setd) and the second is the descriptor ID

 

> What does the fromID() method do? Where did you get the description of this method?

it reconstricts a descriptor from ID. I didn't get the description of it anywhere just saw it's being mentioned once here on the forum, but that was on a previous forum version and olinks don't work anymore... As it happens often with Photoshop scripts, I can't find anything about this method except for ambigous 'this reconsctructs a descriptor' 😕

 

@JJMack, thank you for your suggestion! Unfortunately this won't work for me as I need the points to be ordered as they were created.

 

It's possible to get the list of points in Photoshop2020 as a part of com.adobe.PhotoshopJSONCallbackcom, but I wanted to try the AM fromID() to be able to use my function in older Photoshops

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Feb 27, 2020 Feb 27, 2020

Copy link to clipboard

Copied

>> What does alert(myDescriptor.count) show?

>An error....

 

This is strange and by and large cannot be. What kind of error?
What this code shows and where the error occurs.

 

var myDescriptor = new ActionDescriptor();

alert(myDescriptor.count);

myDescriptor.fromID(_id);

alert(_id + "\n\n" + typeof(_id));

alert(myDescriptor);
alert(myDescriptor.count);

 

What version of photoshop?

 

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 27, 2020 Feb 27, 2020

Copy link to clipboard

Copied

Oh! Sorry, I was wrong, .count is actually 3 after the .fromID(). Weird, maybe something else was throwing an error and I thought it was count... anyway, I found my object in there 🙂 Thank you for your help!

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 10, 2021 Apr 10, 2021

Copy link to clipboard

Copied

Can you show me .jsx version where .fromID() works? If it works only with CEP then also .js?

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 10, 2021 Apr 10, 2021

Copy link to clipboard

Copied

 

 

(mthds = (new ActionDescriptor()).reflect.methods)[mthds.length - 1]

 

The only 2 posts where I found something about it are: Aug 27, 2015 and Aug 31, 2015

 

I tried to use it without CEP but I failed 😞

 

if (name.split('Photoshop').length - 1 && !$.getenv('not')) $.setenv('not', 1), notifiers.add('slct', File($.fileName))
else if(args = arguments) {(dsc = new ActionDescriptor()).fromID(args[1]); try{alert(dsc.count)}catch(err){alert(err)}}

 

'C:\Program Files (x86)\Common Files\Adobe\Startup Scripts CC\Adobe Photoshop\slct.jsx', that's where to save the code. After (re)launching Photoshop select some unselected layer in the document. You can use dsc.typename, but dsc.count does not work.

 

How to use fromID() method then. Does it work only sent by CEP to jsx (after registering the events)?

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Feb 25, 2020 Feb 25, 2020

Copy link to clipboard

Copied

I know mothing about codinf a Photoshop extension. So I do not know what information you can get with Action Manager code for a setd event even tell if the setd event was from a lasso tool setting a selection. With DOM code I see only how one can set a selection region with an array of points no way to retrieve the selection region points. Just the bounds of the selection can be retrieved.  Can you have the user use the pen tool to set a Path rather the as selection with the lasso tools there will be many point if they use a lasso tool.   If they use the polygonal lasso tool  there may not be a huge number of point the script would need to teals with.  You could cut the number of point down by converting the selection to a Path with a tolerance like 1 to 4 to lower to number of control points that will be set them Process the WorkPath

points. You can get Path Points.  Here I through some Scriptlistener code together It sort of got the selection area points.

 

 

 

 

if (hasSelection()) {app.activeDocument.suspendHistory('selectionPathPoints','main()');} 
else {alert("An Active selection is required")}

function main() {
	// Save the current preferences
	var startRulerUnits = app.preferences.rulerUnits;
	// Set Photoshop to use pixels 
	app.preferences.rulerUnits = Units.PIXELS;
	saveAlpha("saveActiveSelection");
	// 1 to 4 tolerance
	makePathFromSelection(4);
	var thePath = selectedPath();
	var myPathInfo = extractSubPathInfo(thePath);
	var message ="    Path Points\n";	
	for(var k=0;k<myPathInfo.length;k++){  
		for(var j=0;j<myPathInfo[k].entireSubPath.length;j++){ 
			message = message +  "P " + (j + 1) + "   X " + myPathInfo[k].entireSubPath[j].anchor[0] + "     Y " + myPathInfo[k].entireSubPath[j].anchor[1] + "\n";
			MarkX(myPathInfo[k].entireSubPath[j].anchor[0]);
			MarkY(myPathInfo[k].entireSubPath[j].anchor[1]);
			}
		}
	alert(message);
	//makeSelectionFromPath(1, true);  // Changed to restoring saved active selection
	loadAlpha("saveActiveSelection");
	deleteAlpha("saveActiveSelection");
	deleteWorkPath();
	app.preferences.rulerUnits = startRulerUnits;
}
////// determine selected path //////
function selectedPath () {
	try {
		var ref = new ActionReference(); 
		ref.putEnumerated( charIDToTypeID("Path"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
		var desc = executeActionGet(ref);
		var theName = desc.getString(charIDToTypeID("PthN"));
		return app.activeDocument.pathItems.getByName(theName)
	}
	catch (e) {
		return undefined
	}
};
////// michael l hale’s code //////
function extractSubPathInfo(pathObj){  
    var pathArray = new Array();// each element can be used as the second arugment in pathItems.add ie doc.pathItems.add("myPath1", [pathArray[0]]);  
    var pl = pathObj.subPathItems.length;  
    for(var s=0;s<pl;s++){  
        var pArray = new Array();  
          for(var i=0;i<pathObj.subPathItems[s].pathPoints.length;i++){  
             pArray[i] = new PathPointInfo;  
             pArray[i].kind = pathObj.subPathItems[s].pathPoints[i].kind;  
             pArray[i].anchor = pathObj.subPathItems[s].pathPoints[i].anchor;  
			 //alert("Anchor " + pathObj.subPathItems[s].pathPoints[i].anchor );
             pArray[i].leftDirection = pathObj.subPathItems[s].pathPoints[i].leftDirection;  
             pArray[i].rightDirection = pathObj.subPathItems[s].pathPoints[i].rightDirection;  
          };  
        pathArray[pathArray.length] = new Array();  
        pathArray[pathArray.length - 1] = new SubPathInfo();  
        pathArray[pathArray.length - 1].operation = pathObj.subPathItems[s].operation;  
        pathArray[pathArray.length - 1].closed = pathObj.subPathItems[s].closed;  
        pathArray[pathArray.length - 1].entireSubPath = pArray;  
    };  
    return pathArray;  
};  

////// check for selection //////
function hasSelection(){
	var ref = new ActionReference();
	ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
	var docDesc = executeActionGet(ref);
	var hasSelection = docDesc.hasKey(stringIDToTypeID("selection"));
	return hasSelection;
};

function makePathFromSelection(tolerance) {
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putClass( stringIDToTypeID( "path" ));
	descriptor.putReference( charIDToTypeID( "null" ), reference );
	reference2.putProperty( stringIDToTypeID( "selectionClass" ), stringIDToTypeID( "selection" ));
	descriptor.putReference( stringIDToTypeID( "from" ), reference2 );
	descriptor.putUnitDouble( stringIDToTypeID( "tolerance" ), stringIDToTypeID( "pixelsUnit" ), tolerance );
	executeAction( stringIDToTypeID( "make" ), descriptor, DialogModes.NO );
}

function makeSelectionFromPath(version, vectorMaskParams) {
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putProperty( stringIDToTypeID( "channel" ), stringIDToTypeID( "selection" ));
	descriptor.putReference( charIDToTypeID( "null" ), reference );
	reference2.putProperty( stringIDToTypeID( "path" ), stringIDToTypeID( "workPath" ));
	descriptor.putReference( stringIDToTypeID( "to" ), reference2 );
	descriptor.putInteger( stringIDToTypeID( "version" ), version );
	descriptor.putBoolean( stringIDToTypeID( "vectorMaskParams" ), vectorMaskParams );
	executeAction( stringIDToTypeID( "set" ), descriptor, DialogModes.NO );
}

function deleteWorkPath() {
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putProperty( stringIDToTypeID( "path" ), stringIDToTypeID( "workPath" ));
	descriptor.putReference( charIDToTypeID( "null" ), reference );
	executeAction( stringIDToTypeID( "delete" ), descriptor, DialogModes.NO );
}

function MarkX(x) {
// =======================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc61 = new ActionDescriptor();
    var idNw = charIDToTypeID( "Nw  " );
        var desc62 = new ActionDescriptor();
        var idPstn = charIDToTypeID( "Pstn" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc62.putUnitDouble( idPstn, idPxl, x);
        var idOrnt = charIDToTypeID( "Ornt" );
        var idOrnt = charIDToTypeID( "Ornt" );
        var idVrtc = charIDToTypeID( "Vrtc" );
        desc62.putEnumerated( idOrnt, idOrnt, idVrtc );
    var idGd = charIDToTypeID( "Gd  " );
    desc61.putObject( idNw, idGd, desc62 );
executeAction( idMk, desc61, DialogModes.NO );
}
function MarkY(y) {
// =======================================================
var idMk = charIDToTypeID( "Mk  " );
    var desc63 = new ActionDescriptor();
    var idNw = charIDToTypeID( "Nw  " );
        var desc64 = new ActionDescriptor();
        var idPstn = charIDToTypeID( "Pstn" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc64.putUnitDouble( idPstn, idPxl, y );
        var idOrnt = charIDToTypeID( "Ornt" );
        var idOrnt = charIDToTypeID( "Ornt" );
        var idHrzn = charIDToTypeID( "Hrzn" );
        desc64.putEnumerated( idOrnt, idOrnt, idHrzn );
    var idGd = charIDToTypeID( "Gd  " );
    desc63.putObject( idNw, idGd, desc64 );
executeAction( idMk, desc63, DialogModes.NO );
}

function  saveAlpha(alphaName){
	var idDplc = charIDToTypeID( "Dplc" );
	    var desc27 = new ActionDescriptor();
 	   var idnull = charIDToTypeID( "null" );
	        var ref11 = new ActionReference();
	        var idChnl = charIDToTypeID( "Chnl" );
	        var idfsel = charIDToTypeID( "fsel" );
	        ref11.putProperty( idChnl, idfsel );
	    desc27.putReference( idnull, ref11 );
	    var idNm = charIDToTypeID( "Nm  " );
	    desc27.putString( idNm, alphaName );
	try {
	executeAction( idDplc, desc27, DialogModes.NO );
	}
	catch(e) {}
}

// ======Load Alpha Channel Selection===============================
function loadAlpha(channel) {
	var idsetd = charIDToTypeID( "setd" );
	    var desc2 = new ActionDescriptor();
	    var idnull = charIDToTypeID( "null" );
	        var ref1 = new ActionReference();
	        var idChnl = charIDToTypeID( "Chnl" );
	        var idfsel = charIDToTypeID( "fsel" );
	        ref1.putProperty( idChnl, idfsel );
	    desc2.putReference( idnull, ref1 );
	    var idT = charIDToTypeID( "T   " );
	        var ref2 = new ActionReference();
	        var idChnl = charIDToTypeID( "Chnl" );
	        ref2.putName( idChnl, channel );
	    desc2.putReference( idT, ref2 );
	executeAction( idsetd, desc2, DialogModes.NO );
}

function deleteAlpha(channel) {
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();

	reference.putName( stringIDToTypeID( "channel" ), channel );
	descriptor.putReference( charIDToTypeID( "null" ), reference );
	executeAction( stringIDToTypeID( "delete" ), descriptor, DialogModes.NO );
}

 

JJMack

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Feb 26, 2020 Feb 26, 2020

Copy link to clipboard

Copied

A selection can be very complex and contain more than one area path.

Capture.jpg

JJMack

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines