Copy link to clipboard
Copied
Is it possible to select a subpath in a shape via script?
The goal is to select the star, then rotate it and subtract it from the other shape.
The problem is that I have not found a way to select the star
Kind Regards
Joe
SOLUTION!
app.preferences.rulerUnits = Units.POINTS;
Copy link to clipboard
Copied
Try this way (CC2018+)
var len = app.activeDocument.pathItems.length;
if (len)
{
var pth = app.activeDocument.pathItems[len-1];
pth.deselect();
pth.select();
len = pth.subPathItems.length;
var n = prompt("Enter subpath index to select (0-"+(len-1)+")", "0", "");
var x = -1;
if (n != null) x = Number(n);
if (isNaN(x) || x < 0 || x > len-1)
{
alert("Bad number");
}
else
{
var p = pth.subPathItems[x].pathPoints[pth.subPathItems[x].pathPoints.length-1];
var zoom = get_doc_zoom();
//set_doc_zoom(100);
point_select(p.anchor[0], p.anchor[1]);
//set_doc_zoom(zoom);
}
}
else
alert("No path");
function point_select(x, y)
{
var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("pathComponentSelectTool"));
d.putReference(stringIDToTypeID("null"), r);
d.putData(stringIDToTypeID("toolRecordingData"),
String.fromCharCode(
0x00,0x00,0x00,0x06,0x03,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
0x18,0x54,0x50,0x61,0x74,0x68,0x43,0x6F,0x6D,0x70,0x6F,0x6E,0x65,0x6E,0x74,0x53,0x65,0x6C,0x65,0x63,0x74,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,
0x18,0x54,0x50,0x61,0x74,0x68,0x43,0x6F,0x6D,0x70,0x6F,0x6E,0x65,0x6E,0x74,0x53,0x65,0x6C,0x65,0x63,0x74,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,
0x18,0x54,0x50,0x61,0x74,0x68,0x43,0x6F,0x6D,0x70,0x6F,0x6E,0x65,0x6E,0x74,0x53,0x65,0x6C,0x65,0x63,0x74,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,
0x06,0x3C,0x4E,0x55,0x4C,0x4C,0x3E,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x20,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00)+
toIEEE754(x, 8, 23) + toIEEE754(y, 8, 23) +
String.fromCharCode(0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00));
executeAction(stringIDToTypeID('toolRecording'), d, DialogModes.NO);
};
function set_doc_zoom(zoom)
{
try {
if (!zoom) zoom = 100;
var d = new ActionDescriptor();
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("zoom"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
d1.putUnitDouble(stringIDToTypeID("zoom"), stringIDToTypeID("pixelsUnit"), zoom/100);
d.putObject(stringIDToTypeID("to"), stringIDToTypeID("zoom"), d1);
executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
}
catch (e) { /*throw(e);*/ }
}
function get_doc_zoom()
{
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("zoom"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
return executeActionGet(r).getUnitDoubleValue(stringIDToTypeID("zoom"))*100;
}
catch (e) { throw(e); }
}
function toIEEE754(v, ebits, fbits)
{
try {
var bias = (1 << (ebits - 1)) - 1;
var s, e, f;
if (isNaN(v))
{
e = (1 << bias) - 1; f = 1; s = 0;
}
else if (v === Infinity || v === -Infinity)
{
e = (1 << bias) - 1; f = 0; s = (v < 0) ? 1 : 0;
}
else if (v === 0)
{
e = 0; f = 0; s = (1 / v === -Infinity) ? 1 : 0;
}
else
{
s = v < 0;
v = Math.abs(v);
if (v >= Math.pow(2, 1 - bias))
{
var ln = Math.min(Math.floor(Math.log(v) / Math.LN2), bias);
e = ln + bias;
f = v * Math.pow(2, fbits - ln) - Math.pow(2, fbits);
}
else
{
e = 0;
f = v / Math.pow(2, 1 - bias - fbits);
}
}
var i, bits = [];
for (i = fbits; i; i -= 1) { bits.push(f % 2 ? 1 : 0); f = Math.floor(f / 2); }
for (i = ebits; i; i -= 1) { bits.push(e % 2 ? 1 : 0); e = Math.floor(e / 2); }
bits.push(s ? 1 : 0);
bits.reverse();
var str = bits.join('');
var bytes = "";
while (str.length)
{
bytes += String.fromCharCode(parseInt(str.substring(0, 8), 2));
str = str.substring(8);
}
return bytes;
}
catch (e) { throw(e); }
}
Copy link to clipboard
Copied
Thank you r-bin ,
this works fine in CC 2017 and CC 2018, unfortunately not in CC 2019.
Maybe you have an idea what you have to adjust there, so it will work in 2019
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I've already tried that, unfortunately it does not work
Copy link to clipboard
Copied
Can you select a subpath manually using the Path Selection Tool (black arrow)?
Copy link to clipboard
Copied
Yes, I can manually select the subpath.
Copy link to clipboard
Copied
If the document has a resolution of 300dpi, it will not work for. It works in 72dpi
Copy link to clipboard
Copied
Try workaround
var orig_resolution = activeDocument.resolution;
activeDocument.resizeImage(undefined, undefined, 72, ResampleMethod.NONE);
//selecting code here
activeDocument.resizeImage(undefined, undefined, orig_resolution, ResampleMethod.NONE);
Copy link to clipboard
Copied
SOLUTION!
app.preferences.rulerUnits = Units.POINTS;
Copy link to clipboard
Copied
Thanks, now it works great
Copy link to clipboard
Copied
r-bin is there also the possibility to select all points of the path?
Copy link to clipboard
Copied
function selectallanchors() {
var doc = app.activeDocument;
var myPath = doc.pathItems[0];
for(var i = 0; i < myPath.pathPoints.length; i++) {
myPath.pathPoints[i].selected = PathPointSelection.ANCHORPOINT;
}
};
selectallanchors();
Copy link to clipboard
Copied
Thanks, but I'm getting Error on line 4;
Copy link to clipboard
Copied
Ah sorry,
I didn't notice that you use Photoshop. The script is for Illustrator.
Copy link to clipboard
Copied
Thanks anyway
Copy link to clipboard
Copied
var len = app.activeDocument.pathItems.length;
if (len)
{
app.preferences.rulerUnits = Units.POINTS;
var pth = app.activeDocument.pathItems[len-1];
pth.deselect();
pth.select();
len = pth.subPathItems.length;
for (var i = 0; i < len; i++)
{
var p = pth.subPathItems[i].pathPoints[0];
var zoom = get_doc_zoom();
//set_doc_zoom(100);
point_select(p.anchor[0], p.anchor[1], true);
//set_doc_zoom(zoom);
}
}
else
alert("No path");
function point_select(x, y, add)
{
var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("pathComponentSelectTool"));
d.putReference(stringIDToTypeID("null"), r);
d.putData(stringIDToTypeID("toolRecordingData"),
String.fromCharCode(
0x00,0x00,0x00,0x06,0x03,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
0x18,0x54,0x50,0x61,0x74,0x68,0x43,0x6F,0x6D,0x70,0x6F,0x6E,0x65,0x6E,0x74,0x53,0x65,0x6C,0x65,0x63,0x74,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,
0x18,0x54,0x50,0x61,0x74,0x68,0x43,0x6F,0x6D,0x70,0x6F,0x6E,0x65,0x6E,0x74,0x53,0x65,0x6C,0x65,0x63,0x74,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,
0x18,0x54,0x50,0x61,0x74,0x68,0x43,0x6F,0x6D,0x70,0x6F,0x6E,0x65,0x6E,0x74,0x53,0x65,0x6C,0x65,0x63,0x74,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,
0x06,0x3C,0x4E,0x55,0x4C,0x4C,0x3E,0x00,0x00,0x00,0x01,0x00,0x00,add?1:0,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x20,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00)+
toIEEE754(x, 8, 23) + toIEEE754(y, 8, 23) +
String.fromCharCode(0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00));
executeAction(stringIDToTypeID("toolRecording"), d, DialogModes.NO);
}
function set_doc_zoom(zoom)
{
try {
if (!zoom) zoom = 100;
var d = new ActionDescriptor();
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("zoom"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
d1.putUnitDouble(stringIDToTypeID("zoom"), stringIDToTypeID("pixelsUnit"), zoom/100);
d.putObject(stringIDToTypeID("to"), stringIDToTypeID("zoom"), d1);
executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
}
catch (e) { /*throw(e);*/ }
}
function get_doc_zoom()
{
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("zoom"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
return executeActionGet(r).getUnitDoubleValue(stringIDToTypeID("zoom"))*100;
}
catch (e) { throw(e); }
}
function toIEEE754(v, ebits, fbits)
{
try {
var bias = (1 << (ebits - 1)) - 1;
var s, e, f;
if (isNaN(v))
{
e = (1 << bias) - 1; f = 1; s = 0;
}
else if (v === Infinity || v === -Infinity)
{
e = (1 << bias) - 1; f = 0; s = (v < 0) ? 1 : 0;
}
else if (v === 0)
{
e = 0; f = 0; s = (1 / v === -Infinity) ? 1 : 0;
}
else
{
s = v < 0;
v = Math.abs(v);
if (v >= Math.pow(2, 1 - bias))
{
var ln = Math.min(Math.floor(Math.log(v) / Math.LN2), bias);
e = ln + bias;
f = v * Math.pow(2, fbits - ln) - Math.pow(2, fbits);
}
else
{
e = 0;
f = v / Math.pow(2, 1 - bias - fbits);
}
}
var i, bits = [];
for (i = fbits; i; i -= 1) { bits.push(f % 2 ? 1 : 0); f = Math.floor(f / 2); }
for (i = ebits; i; i -= 1) { bits.push(e % 2 ? 1 : 0); e = Math.floor(e / 2); }
bits.push(s ? 1 : 0);
bits.reverse();
var str = bits.join('');
var bytes = "";
while (str.length)
{
bytes += String.fromCharCode(parseInt(str.substring(0, 8), 2));
str = str.substring(8);
}
return bytes;
}
catch (e) { throw(e); }
}
Copy link to clipboard
Copied
Due to my previous post I tried also newer version of script where after correcting:
var p = pth.subPathItems[i].pathPoints[0];
I met the same problem, the script seems to work but freezes at the end.
Ps. I tried it without ScriptListener in Plugins Folder, also with/out Allowed Tool Recording.
Copy link to clipboard
Copied
It's not my fault.
The script works perfectly on CC2018, with poor accuracy on CS6 (results vary depending on zoom and other parameters).
For 2020 it freezes (for 2021 apparently too).
This is a bug.
How to check.
Enable AllowToolRecording.
Record the click of the pathComponentSelectTool click on any pathpoint.
Run Action. Hanging is evident. Write a bug report if you like.
Copy link to clipboard
Copied
I tried it manually like you described and Ps freezes as expected. I also reported this bug.
It happened now to me in 22.4.2, 22.4.1 and older 22.3. In the report I mentioned also of 2020 and the last one I know it is working is 2018, but I have no idea if the error occurs in 2019.
I don't know if I remember well but I think for the first time I tried your other script (Photoshop Script to continue work path) few weeks ago, that is similar to this one, and that worked. Now on earlier versions (mentioned above) I see that doesn't work, so maybe I have a hole in the head 😛
Does that script is supposed to work in CS6 with some special zoom or other parameters? I tried it but it only selects last path point and nothing else.
The script from current thread works in CS6 with error while executing 'toolRecording'.
Could you advice what to set to make boths scripts work in CS6, or at least one of them?
For the one from this topic I tried it on attached .psd, but as I said I'm getting error 😕
Ps. I edited your posts with codes in this thread to insert the missing indexes.
Copy link to clipboard
Copied
Hi r-bin,
Could you help ?
I would like to use your code but for pathitems with points whose coordinates are not integers.
Copy link to clipboard
Copied
Hi.
Where did you get the idea that the coordinates of the points were integers?
Copy link to clipboard
Copied
I just have tested your script with different paths. The code works perfectly with path whose all points snap to pixels but it fails with the others.
Copy link to clipboard
Copied
I don't think you should use this script. It does not work reliably on many versions of Photoshop. It was designed as a temporary workaround to solve this problem through undocumented features using ToolRecording, which may change from version to version.
Copy link to clipboard
Copied
I don't really understand all the code;I 'm just guessing.
The code runs reliably for point with integers coords, so afterall we are not so far away ?
I thought the problem could come from the IEEE754 function ( which is a totaly brand new universe for me ).
Can you confirm the problem doesn't come from this function ?