Skip to main content
Joachim Hiller
Known Participant
August 11, 2019
Answered

Select subpath

  • August 11, 2019
  • 1 reply
  • 4801 views

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

This topic has been closed for replies.
Correct answer r-bin

If the document has a resolution of 300dpi, it will not work for. It works in 72dpi


SOLUTION!

app.preferences.rulerUnits = Units.POINTS;

1 reply

Legend
August 11, 2019

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); }    
    }  
Joachim Hiller
Known Participant
August 11, 2019

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

Legend
August 11, 2019
I do not have CC2019.
Try uncommenting the lines 28 and 32
(with set_doc_zoom() call)