Copy link to clipboard
Copied
I've already asked this question in the PS-SCRIPTS forum and gotten 2 answers. I wonder if anyone else has some other ideas on how to do this.
Is there a way I can have the user click inside the opened document and have the X,Y coordinates, where the user clicked, returned to the script while the script is running?
Larry
Copy link to clipboard
Copied
Hi Larry,
After your other post I tried again to come up with a way for the user to pick a point while the script is running. I stll don't think it can be done with just a click but I have come up with a way to get a user defined point while the script is running.
It's not pretty but it was the best I could come up with. Have your script alert before hand that it will bring up a transform and they need to press any arrow key once then move the crosshair that in normally in the middle to the point they want. They have to nudge the layer, just setting the crosshair does not return the needed transform descriptor. And they must move the crosshair or its not in the descriptor.
The function below makes a selection, brings up the transform in dialogmode.all, after the user confrims the trans it undoes the move and deselects and returns an x,y array where user moved the point.
function ftn1() {
app.activeDocument.selection.selectAll();
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
try{
var desc = executeAction( charIDToTypeID('Trnf'), desc, DialogModes.ALL );
executeAction( charIDToTypeID('undo'), undefined, DialogModes.NO );
app.activeDocument.selection.deselect();
var point = desc.getObjectValue(charIDToTypeID( "Pstn" ));
var x = point.getUnitDoubleValue(charIDToTypeID( "Hrzn" ));
var y = point.getUnitDoubleValue(charIDToTypeID( "Vrtc" ));
return [x,y];
}catch(e){}
}
Note that this is a CS4 version and the ruler must be set to pixels as this doesn't check the unit value. You may need to change getUnitDoubleValue to getDouble with older versions. Or let me know and I can change it to return a UnitValue instead of double.
Mike
Copy link to clipboard
Copied
It has been a long time since I worked on scripts.
How do I display the values of x and y using "alert"? I'm not sure what
"return " is returning - an array?
I not very good at this scriptlisterner type code. I'm also very rusty on
the Javascript. It has been over 2 years since I looked at any of this. I
have to relearn what the scripts that I wrote do.
Thanks for the help!
Larry
Copy link to clipboard
Copied
It's great that you are back.
It does return an array, sorry for the shorthand [] and lack of comments. I also thought I would let you name the function. Here are two examples using that function.
var p = ftn1();
alert('The selected point is at x'+k[0]+', y'+k[1]+' pixels');
var p = k[0];
var p = k[1];
// make a 1x1 pixel selection at that point
activeDocument.selection.select([[x,y],[x+1,y],[x+1,y+1],[x,y+1]]);
Find more inspiration, events, and resources on the new Adobe Community
Explore Now