Copy link to clipboard
Copied
...or any selection of any shape, for that matter?
I've only just realised the potential for scripting in PS so this is very new to me. Have some JS experience though.
The reason I need to be able to do this is actually becuase Adobe got rid of ImageReady - it had the ability to turn a selection into an HTML set of coordinates you could use in an image map. Can't do it in Firworks so I reckon my best option is to try to get PS to output just the coords and I can do the rest of the HTML.
Copy link to clipboard
Copied
For selection made with the rectangular selection tool you could use the selection bounds.
var b = app.activeDocument.selection.bounds;
alert('Top Left XY = '+b[0]+','+b[1]+'\rTop right XY = '+b[2]+':'+b[1]+'\rBottom left XY = '+b[0]+':'+b[3]+'\rBottom right XY = '+b[2]+':'+b[3])
Note with any other type selection the bounds will be bigger than the area selected. For those type shapes you could draw a path and get the bounds from the pathPonts.
Copy link to clipboard
Copied
And here is an example of getting details from a path, results are written to the ExtendScript console.
(a selection is required).
var doc = activeDocument;
if(hasSelection(doc)){
doc.selection.makeWorkPath(0.000000001);
var workPath = doc.pathItems.getByName("Work Path");
$.writeln(workPath.pathKind);
$.writeln("Number of Sub Paths = " +workPath.subPathItems.length);
for(var b =0;b<workPath.subPathItems.length;b++){
$.writeln("Sub Path "+b +" Closed = "+workPath.subPathItems.closed);
}
for(var b =0;b<workPath.subPathItems.length;b++){
$.writeln("\n\nSub Path "+b+" has " +workPath.subPathItems.pathPoints.length+ " Pathpoints");
$.writeln("Sub Path "+b+" Operation = " +workPath.subPathItems.operation);
for(var a =0;a<workPath.subPathItems.pathPoints.length;a++){
$.writeln("Kind - "+workPath.subPathItems.pathPoints.kind);
$.writeln("Anchor - "+workPath.subPathItems.pathPoints.anchor);
$.writeln("Left - "+workPath.subPathItems.pathPoints.leftDirection);
$.writeln("Right - "+workPath.subPathItems.pathPoints.rightDirection);
}
}
}
function hasSelection (doc) {
var res = false;
var as = doc.activeHistorySt...
Copy link to clipboard
Copied
Wow!!! That works...almost perfectly!!!
Have a look at the attached files - the first is the selection, the second the resulting path before it's chucked out to the script console. It seems that PS isn't quite drawing the path along the lines of the selection - it draws a diagonal line where it should step around the edges of the selection. Is there any way to force it to follow the selection religiously?
Copy link to clipboard
Copied
The smallest tolerance Photoshop allows when converting a selection to a path is 0.5 pixels. That is why the path doesn't match the selection exactly.
The only way I can think of to have the path exactly match the selection would be to have an event handler for the Addt event to capture and store the selection's descriptor. Then have a script that gets the stored descriptor and create a path. But that would be a lot of work to setup and you would only be able to convert the last selection made in the GUI.
Copy link to clipboard
Copied
Wanna know what's really odd? In some cases it gets it bang on and in others it takes the short cut. See the attached which is another screenshot from the same selection to path as before. It gets it right in the middle of the "stepping" section and then seems to get bored and cut the corner ![]()
Copy link to clipboard
Copied
I think Paul found a bug in the scripting DOM that lets the script set the tolerance below 0.5. If you think the path you posted doesn't match the selection try making a path using the path panel. It will be worse.
You could edit the mismatched points.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more