Skip to main content
AG_Ps_100
Inspiring
December 9, 2018
Answered

Photoshop Script to continue work path

  • December 9, 2018
  • 2 replies
  • 3350 views

Hello everyone,

I opened a document that already had a path, and then I select the path from the "Paths" window:

and it is shown on the canvas:

I want a script that will continue the current path so I can close the path. (sometimes the path will be somewhere else so I can't rely on exact X,Y position)
I want something like this for example:

that's it.

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

Try this. Works in CC2018. In CS6 does not work.

var len = app.activeDocument.pathItems.length;

if (len)

    {

    var pth = app.activeDocument.pathItems[len-1];

    pth.deselect();

    pth.select();

    len = pth.subPathItems.length;

    var p = pth.subPathItems[len-1].pathPoints[pth.subPathItems[len-1].pathPoints.length-1];

    var zoom = get_doc_zoom();

    set_doc_zoom(100);

    pen(p.anchor[0], p.anchor[1]);

    set_doc_zoom(zoom);

    }

else

    alert("No path");   

function pen(x, y)

    {

    var d = new ActionDescriptor();

    var r = new ActionReference();

    r.putClass(stringIDToTypeID("penTool"));

    d.putReference(charIDToTypeID('null'), r);

    var data = String.fromCharCode(

        0x00,0x00,0x00,0x06,0x02,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x08,0x54,0x50,0x65,0x6E,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,0x08,0x54,0x50,0x65,0x6E,0x54,0x6F,0x6F,

        0x6C,0x00,0x00,0x00,0x08,0x54,0x50,0x65,0x6E,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,0x06,0x3C,0x4E,0x55,0x4C,0x4C,0x3E,0x00,0x00,0x00,0x02,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,0x00,0x00,0x00,0x00,0x00,

        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,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,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);

    d.putData(stringIDToTypeID("toolRecordingData"), data);

    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); } 

    }

2 replies

c.pfaffenbichler
Community Expert
Community Expert
December 10, 2018

But it should be possible to auto-complete the open subPathItem if there are clear and unequivocal rules about how it is to be completed.

AG_Ps_100
AG_Ps_100Author
Inspiring
December 10, 2018

r-bin​, c.pfaffenbichler​ thank you for your replies. I'm on the way to solve it, I made a new path, and recorded the last anchor point in an action, I played the action afterwards and it said "Pick-up Path"

I think I have 2 ways to go from here (i need help to execute each one of them since I don't know how)

1. find a script to "Pick-up Path"

2. I've recorded the last anchor point in an action, if I could record the last anchor point and somehow save it for later, for example: write the X,Y position into the Metadata and later delete it, it should work, only question how I record the anchor point or the last anchor point

r-binCorrect answer
Legend
December 10, 2018

Try this. Works in CC2018. In CS6 does not work.

var len = app.activeDocument.pathItems.length;

if (len)

    {

    var pth = app.activeDocument.pathItems[len-1];

    pth.deselect();

    pth.select();

    len = pth.subPathItems.length;

    var p = pth.subPathItems[len-1].pathPoints[pth.subPathItems[len-1].pathPoints.length-1];

    var zoom = get_doc_zoom();

    set_doc_zoom(100);

    pen(p.anchor[0], p.anchor[1]);

    set_doc_zoom(zoom);

    }

else

    alert("No path");   

function pen(x, y)

    {

    var d = new ActionDescriptor();

    var r = new ActionReference();

    r.putClass(stringIDToTypeID("penTool"));

    d.putReference(charIDToTypeID('null'), r);

    var data = String.fromCharCode(

        0x00,0x00,0x00,0x06,0x02,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x08,0x54,0x50,0x65,0x6E,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,0x08,0x54,0x50,0x65,0x6E,0x54,0x6F,0x6F,

        0x6C,0x00,0x00,0x00,0x08,0x54,0x50,0x65,0x6E,0x54,0x6F,0x6F,0x6C,0x00,0x00,0x00,0x06,0x3C,0x4E,0x55,0x4C,0x4C,0x3E,0x00,0x00,0x00,0x02,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,0x00,0x00,0x00,0x00,0x00,

        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,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,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00);

    d.putData(stringIDToTypeID("toolRecordingData"), data);

    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); } 

    }

Legend
December 9, 2018

Do you want to activate (select) the last point in the Path?

If so, then I tried to do this in CS6 using the code obtained from the record of the PathSelectionTool action. It works extremely unreliable and I abandoned this idea.