Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Script for the absolute value of a text box middle value

Engaged ,
Dec 05, 2024 Dec 05, 2024

Hi community. 

 

can someone please share a script which I can select all the text in my file, and it will display only the middle X value? 

 

I already do have 2-3 other scripts which display the X & Y Locations of a bouding box of each object. Once i run the script the values are output into a notepad on my desktop which is super helpful. But now if Possible I just need the script to show the X value of the bounding box.

 

StefanCargoski_0-1733455610926.pngexpand image

 

TOPICS
Actions and scripting , Windows
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Dec 05, 2024 Dec 05, 2024

@Stefan Cargoski 

 

Try this (please use the </> control to paste code next time as the standard forum formatting can break the code):

 

// 2024 modified for middle center, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

    var theLayers = collectSelectedLayersBounds();
    alert(theLayers.join("\n"));
    // copy text to clipboard
    var d = new ActionDescriptor();
    d.putString(stringIDToTypeID("textData"), theLayers.join("\n"));
    executeAction(stringIDToTy
...
Translate
People's Champ , Dec 11, 2024 Dec 11, 2024
Did you write this code yourself or did GPT do it?
There is incorrect work with layers and their indexes, that's why there is confusion.
Check this code
 

 

////////////////////////////////////////////////////////
function get_selected_layers_idx()
    {
    try {
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
        r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"
...
Translate
Adobe
People's Champ ,
Dec 09, 2024 Dec 09, 2024

@c.pfaffenbichler 

Your script gives me different results than the algorithm I gave in the link.

By the way, there was an error with determining the width and height of the document if the resolution is not 72.

 

My script (try it)

 

try 
{

function tranform(p, xx, xy, yx, yy, tx, ty)
    {
    try {
        var x = p[0];
        var y = p[1];

        p[0] = xx*x + yx*y + tx;
        p[1] = xy*x + yy*y + ty;
        }
    catch (e) { alert(e); }
    }

var doc = app.activeDocument;

var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

var w = doc.width.value;
var h = doc.height.value;

app.preferences.rulerUnits = old_units;

var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var tkey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));

var xx = 1;
var xy = 0;
var yx = 0;
var yy = 1;
var tx = 0;
var ty = 0;
          
if (tkey.hasKey(stringIDToTypeID("transform")))
    {
    xx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xx"));
    xy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xy"));
    yx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yx"));
    yy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy"));
    tx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("tx")); // not used
    ty = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("ty")); // not used
    }    
            
var x0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("left"));
var y0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("top"));  
var x1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("right"));  
var y1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("bottom"));  

var p1 = [[x0,y0],[x1,y0],[x1,y1],[x0,y1]];

var ch = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("horizontal"));
var cv = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("vertical"));

tx += w*ch/100;
ty += h*cv/100;

tranform(p1[0], xx, xy, yx, yy, tx, ty);
tranform(p1[1], xx, xy, yx, yy, tx, ty);
tranform(p1[2], xx, xy, yx, yy, tx, ty);
tranform(p1[3], xx, xy, yx, yy, tx, ty);


var l = Math.min(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var t = Math.min(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);
var r = Math.max(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var b = Math.max(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);

var mid_x = (l+r)/2;
var mid_y = (t+b)/2;

alert(mid_x+"\n"+mid_y)


}
catch (e) { alert(e); }

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2024 Dec 10, 2024

Hello, thanks for the script.

 

But I was getting an error, and I remembered I had the chatGTP for help, so it rewrote the code below, and yes it worked. The tool panel X value is 520, and the script indicated value of X at 520.

 

But I needed some more assistance in the code. I want to be able to select multiple layers, and run the script. (This script works only for one layer)

 

Usually when running the script a notepad is generated on my desktop, which shows only the X and Y values. (as the example below).

 

Also running the script, there also some generic numbers which come along, is it possible just for the X & Y value to be displayed in the script, because the string of numbers is confusing. 

 

Thank you.

 

This is an example running another script, which generates a notepad on my desktop.

Notepad example.pngexpand image

Adobe Forum_1.pngexpand image 

 





Script_Error.pngexpand image

{

function transform(p, xx, xy, yx, yy, tx, ty) {
try {
var x = p[0];
var y = p[1];

p[0] = xx * x + yx * y + tx;
p[1] = xy * x + yy * y + ty;
} catch (e) {
alert(e);
}
}

try {
var doc = app.activeDocument;

var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

var w = doc.width.value;
var h = doc.height.value;

app.preferences.rulerUnits = old_units;

var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var tkey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));

var xx = 1;
var xy = 0;
var yx = 0;
var yy = 1;
var tx = 0;
var ty = 0;

if (tkey.hasKey(stringIDToTypeID("transform"))) {
xx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xx"));
xy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xy"));
yx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yx"));
yy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy"));
tx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("tx")); // not used
ty = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("ty")); // not used
}

var x0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("left"));
var y0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("top"));
var x1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("right"));
var y1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("bottom"));

var p1 = [[x0, y0], [x1, y0], [x1, y1], [x0, y1]];

var ch = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("horizontal"));
var cv = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("vertical"));

tx += w * ch / 100;
ty += h * cv / 100;

transform(p1[0], xx, xy, yx, yy, tx, ty);
transform(p1[1], xx, xy, yx, yy, tx, ty);
transform(p1[2], xx, xy, yx, yy, tx, ty);
transform(p1[3], xx, xy, yx, yy, tx, ty);

var l = Math.min(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var t = Math.min(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);
var r = Math.max(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var b = Math.max(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);

var mid_x = (l + r) / 2;
var mid_y = (t + b) / 2;

alert(mid_x + "\n" + mid_y);
} catch (e) {
alert(e);
}

}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Dec 10, 2024 Dec 10, 2024

My script starts with the word "try", yours doesn't.

Wrap my code (slightly modified) in the function get_text_mid_x_y(idx) { ..... }

 

 

function get_text_mid_x_y(idx)
{
try 
{

function tranform(p, xx, xy, yx, yy, tx, ty)
    {
    try {
        var x = p[0];
        var y = p[1];

        p[0] = xx*x + yx*y + tx;
        p[1] = xy*x + yy*y + ty;
        }
    catch (e) { alert(e); }
    }

var doc = app.activeDocument;

var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

var w = doc.width.value;
var h = doc.height.value;

app.preferences.rulerUnits = old_units;

var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));

if (idx >= 1)
    r.putIndex(stringIDToTypeID("layer"), idx);
else    
    r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var tkey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));

var xx = 1;
var xy = 0;
var yx = 0;
var yy = 1;
var tx = 0;
var ty = 0;
          
if (tkey.hasKey(stringIDToTypeID("transform")))
    {
    xx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xx"));
    xy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xy"));
    yx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yx"));
    yy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy"));
    tx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("tx")); // not used
    ty = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("ty")); // not used
    }    
            
var x0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("left"));
var y0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("top"));  
var x1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("right"));  
var y1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("bottom"));  

var p1 = [[x0,y0],[x1,y0],[x1,y1],[x0,y1]];

var ch = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("horizontal"));
var cv = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("vertical"));

tx += w*ch/100;
ty += h*cv/100;

tranform(p1[0], xx, xy, yx, yy, tx, ty);
tranform(p1[1], xx, xy, yx, yy, tx, ty);
tranform(p1[2], xx, xy, yx, yy, tx, ty);
tranform(p1[3], xx, xy, yx, yy, tx, ty);


var l = Math.min(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var t = Math.min(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);
var r = Math.max(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var b = Math.max(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);

var mid_x = (l+r)/2;
var mid_y = (t+b)/2;

return [mid_x, mid_y];

}
catch (e) { alert(e); }
}

 

 

 

In your code, replace the code

 

 

var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top"))];

 

 

in the first case with

 

 

var theseBounds = get_text_mid_x_y(theIndex);

 

 

 

in the second case with

 

 

var theseBounds = get_text_mid_x_y(-1);

 

 

 

P.S. I haven't checked it, because it won't work on CS6 anyway.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2024 Dec 10, 2024

It's near perfect.

 

But The output file, is getting the layer order incorrect. And When selecting only 1 layer, it results to selecting the wrong layer, in the screenshot, I've selected layer 1, but its showing layer 3.

 

text_1 X value is 103, but its showing 103 for Layer Text_3

Adobe_Forum_ScnSh_2t.pngexpand image

 

And this screenshot, the values are corresponding to the wrong layer stacking order.

text_3 X value should be: 518, not 103.

text 2 V value is correct

text 1 value should be 103, not 518.

 

Adobe_Forum_ScnSht.pngexpand image

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Dec 10, 2024 Dec 10, 2024

Your original script does not show on the alert and does not write to the file

such words as "layer:" or "Mid X:".

You have modernized something there and perhaps there is a problem there.

Without your demonstration of the source code that you run, nothing can be said.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 10, 2024 Dec 10, 2024

My bad, I thought the Script code was posted. here's what I have so far:

function get_text_mid_x_y(idx) {
try {
function transform(p, xx, xy, yx, yy, tx, ty) {
var x = p[0];
var y = p[1];
p[0] = xx * x + yx * y + tx;
p[1] = xy * x + yy * y + ty;
}

var doc = app.activeDocument;

var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

var w = doc.width.value;
var h = doc.height.value;

app.preferences.rulerUnits = old_units;

var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));

if (idx >= 1) {
r.putIndex(stringIDToTypeID("layer"), idx);
} else {
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
}

var tkey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));

var xx = 1, xy = 0, yx = 0, yy = 1, tx = 0, ty = 0;

if (tkey.hasKey(stringIDToTypeID("transform"))) {
xx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xx"));
xy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xy"));
yx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yx"));
yy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy"));
tx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("tx"));
ty = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("ty"));
}

var x0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("left"));
var y0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("top"));
var x1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("right"));
var y1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("bottom"));

var p1 = [[x0, y0], [x1, y0], [x1, y1], [x0, y1]];

var ch = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("horizontal"));
var cv = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("vertical"));

tx += w * ch / 100;
ty += h * cv / 100;

transform(p1[0], xx, xy, yx, yy, tx, ty);
transform(p1[1], xx, xy, yx, yy, tx, ty);
transform(p1[2], xx, xy, yx, yy, tx, ty);
transform(p1[3], xx, xy, yx, yy, tx, ty);

var l = Math.min(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var r = Math.max(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var t = Math.min(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);
var b = Math.max(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);

var mid_x = (l + r) / 2;
var mid_y = (t + b) / 2;

return [mid_x, mid_y];
} catch (e) {
alert("Error in get_text_mid_x_y: " + e.message);
}
}

function processSelectedLayers() {
try {
var doc = app.activeDocument;

// Open file to save results
var desktop = Folder.desktop;
var file = new File(desktop + "/LayerMidXValues.txt");
file.open("w");

// Get active layers
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
ref.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var list = executeActionGet(ref).getList(stringIDToTypeID("targetLayers"));

for (var i = 0; i < list.count; i++) {
var layerIndex = list.getReference(i).getIndex();
var layer = doc.layers[layerIndex - 1];
var layerName = layer.name;

// Get mid X and Y
var mid = get_text_mid_x_y(layerIndex);

// Save to file
file.writeln("Layer Name: " + layerName);
file.writeln("Mid X: " + mid[0].toFixed(2));
file.writeln("Mid Y: " + mid[1].toFixed(2));
file.writeln("-----------------------");

// Show alerts for verification
alert("Layer: " + layerName + "\nMid X: " + mid[0].toFixed(2) + "\nMid Y: " + mid[1].toFixed(2));
}

// Close file
file.close();
alert("Results saved to LayerMidXValues.txt on desktop.");
} catch (e) {
alert("Error in processSelectedLayers: " + e.message);
}
}

// Run the script
processSelectedLayers();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Dec 11, 2024 Dec 11, 2024
Did you write this code yourself or did GPT do it?
There is incorrect work with layers and their indexes, that's why there is confusion.
Check this code
 

 

////////////////////////////////////////////////////////
function get_selected_layers_idx()
    {
    try {
        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
        r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

        try {
            var list = executeActionGet(r).getList(stringIDToTypeID("targetLayers"));
            }
        catch (e) 
            { 
            var r = new ActionReference();
            r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("itemIndex"));
            r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
            return [ executeActionGet(r).getInteger(stringIDToTypeID("itemIndex"))-1 ];
            }

        var selected_layers = new Array();

        for (var i = 0; i < list.count; i++) selected_layers.push(list.getReference(i).getIndex());

        return selected_layers;
        }

    catch (e) { alert(e.message +"\nline:"+e.line); }
    }

////////////////////////////////////////////////////////
function get_layer_info(idx) 
    {
    try {
        function transform(p, xx, xy, yx, yy, tx, ty) 
            {
            var x = p[0];
            var y = p[1];
            p[0] = xx * x + yx * y + tx;
            p[1] = xy * x + yy * y + ty;
            }

        var doc = app.activeDocument;

        var old_units = app.preferences.rulerUnits;
        app.preferences.rulerUnits = Units.PIXELS;

        var w = doc.width.value;
        var h = doc.height.value;

        app.preferences.rulerUnits = old_units;

        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));
        r.putIndex(stringIDToTypeID("layer"), idx);

        var tkey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));

        var xx = 1, xy = 0, yx = 0, yy = 1, tx = 0, ty = 0;

        if (tkey.hasKey(stringIDToTypeID("transform"))) 
            {
            xx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xx"));
            xy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xy"));
            yx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yx"));
            yy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy"));
            tx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("tx"));
            ty = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("ty"));
            }

        var x0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("left"));
        var y0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("top"));
        var x1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("right"));
        var y1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("bottom"));

        var p1 = [[x0, y0], [x1, y0], [x1, y1], [x0, y1]];

        var ch = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("horizontal"));
        var cv = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("vertical"));

        tx += w * ch / 100;
        ty += h * cv / 100;

        transform(p1[0], xx, xy, yx, yy, tx, ty);
        transform(p1[1], xx, xy, yx, yy, tx, ty);
        transform(p1[2], xx, xy, yx, yy, tx, ty);
        transform(p1[3], xx, xy, yx, yy, tx, ty);

        var l = Math.min(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
        var r = Math.max(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
        var t = Math.min(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);
        var b = Math.max(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);

        var mid_x = (l + r) / 2;
        var mid_y = (t + b) / 2;

        var r = new ActionReference();
        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("name"));
        r.putIndex(stringIDToTypeID("layer"), idx);
                              
        return [executeActionGet(r).getString(stringIDToTypeID("name")), mid_x, mid_y];
        } 
    catch (e) 
        {
        alert("Error in get_layer_info: " + e.message +"\nline:"+e.line);
        }
    }

////////////////////////////////////////////////////////
function processSelectedLayers() 
    {
    try {
        var doc = app.activeDocument;

        // Open file to save results
        var desktop = Folder.desktop;
        var file = new File(desktop + "/LayerMidXValues.txt");
        file.open("w");

        // Get active layers

        var idx = get_selected_layers_idx();

        var n = 0;
        try { activeDocument.backgroundLayer } catch (e) { n = 1; }

        for (var i = 0; i < idx.length; i++) 
            {
            var info = get_layer_info(idx[i]+n);

            // Save to file
            file.writeln("Layer Name: " + info[0]);
            file.writeln("Mid X: " + info[1].toFixed(2));
            file.writeln("Mid Y: " + info[2].toFixed(2));
            file.writeln("-----------------------");

            // Show alerts for verification
            alert("Layer: " + info[0] + "\nMid X: " + info[1].toFixed(2) + "\nMid Y: " + info[2].toFixed(2));
            }

            // Close file
            file.close();
            alert("Results saved to LayerMidXValues.txt on desktop.");
        } 
    catch (e) 
        {
        alert("Error in processSelectedLayers: " + e.message +"\nline:"+e.line);
        }
    }

////////////////////////////////////////////////////////
// Run the script
processSelectedLayers();

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 11, 2024 Dec 11, 2024

Wow! I cant thank you enough, Now it works 100%. 

 

If I have to be pedantic about the script, if an adjustment can be made. When I run the script, if I have many layers, I have to the click the OK for the notepad output, or the X button to close the script. But depending how many layers I selected, I have to either click the OK or X button depending on the selected layer count. For my task to run this particular script, Ill have 50-100 selected layers, so is it possible just to make the script click the OK only once, and the notepad gets generated on the desktop.

 

 

39 clicks..pngexpand imageClose_39 clicks..pngexpand image

 

Ive included a script I use for the X & Y top left registration, so you can see how it works.

 

https://filebin.net/gyvmog3emfaa0j8e/X_Y_Values.jsx

 

Thank you.

 

 

 

 

My lack of knowledge turned me to GTP, hoping it can modify the script with my prompt, but it lacked the ability to understand the workflow.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Dec 11, 2024 Dec 11, 2024

If I understood you correctly, try using the confirm() function instead of alert().

It returns (unlike alert) different values ​​when you click on different buttons, including the close button.

Depending on the answer, you choose your order of actions further.

The description of the function is given in the screenshot.

222.jpgexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 11, 2024 Dec 11, 2024
LATEST

OK here's the update, finally after 9 attempts with GTP, its working correctly 1000% now.

 

////////////////////////////////////////////////////////
function get_selected_layers_idx()
{
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("targetLayers"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

try {
var list = executeActionGet(r).getList(stringIDToTypeID("targetLayers"));
}
catch (e)
{
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("itemIndex"));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
return [ executeActionGet(r).getInteger(stringIDToTypeID("itemIndex"))-1 ];
}

var selected_layers = new Array();

for (var i = 0; i < list.count; i++) selected_layers.push(list.getReference(i).getIndex());

return selected_layers;
}

catch (e) { alert(e.message +"\nline:"+e.line); }
}

////////////////////////////////////////////////////////
function get_layer_info(idx)
{
try {
function transform(p, xx, xy, yx, yy, tx, ty)
{
var x = p[0];
var y = p[1];
p[0] = xx * x + yx * y + tx;
p[1] = xy * x + yy * y + ty;
}

var doc = app.activeDocument;

var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

var w = doc.width.value;
var h = doc.height.value;

app.preferences.rulerUnits = old_units;

var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));
r.putIndex(stringIDToTypeID("layer"), idx);

var tkey = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey"));

var xx = 1, xy = 0, yx = 0, yy = 1, tx = 0, ty = 0;

if (tkey.hasKey(stringIDToTypeID("transform")))
{
xx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xx"));
xy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("xy"));
yx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yx"));
yy = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy"));
tx = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("tx"));
ty = tkey.getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("ty"));
}

var x0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("left"));
var y0 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("top"));
var x1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("right"));
var y1 = tkey.getObjectValue(stringIDToTypeID("bounds")).getUnitDoubleValue(stringIDToTypeID("bottom"));

var p1 = [[x0, y0], [x1, y0], [x1, y1], [x0, y1]];

var ch = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("horizontal"));
var cv = tkey.getObjectValue(stringIDToTypeID("textClickPoint")).getUnitDoubleValue(stringIDToTypeID("vertical"));

tx += w * ch / 100;
ty += h * cv / 100;

transform(p1[0], xx, xy, yx, yy, tx, ty);
transform(p1[1], xx, xy, yx, yy, tx, ty);
transform(p1[2], xx, xy, yx, yy, tx, ty);
transform(p1[3], xx, xy, yx, yy, tx, ty);

var l = Math.min(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var r = Math.max(p1[0][0], p1[1][0], p1[2][0], p1[3][0]);
var t = Math.min(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);
var b = Math.max(p1[0][1], p1[1][1], p1[2][1], p1[3][1]);

var mid_x = (l + r) / 2;
var mid_y = (t + b) / 2;

var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("name"));
r.putIndex(stringIDToTypeID("layer"), idx);

return [executeActionGet(r).getString(stringIDToTypeID("name")), mid_x, mid_y];
}
catch (e)
{
alert("Error in get_layer_info: " + e.message +"\nline:"+e.line);
}
}

////////////////////////////////////////////////////////
function processSelectedLayers()
{
try {
var doc = app.activeDocument;

// Open file to save results
var desktop = Folder.desktop;
var file = new File(desktop + "/LayerMidXValues.txt");
file.open("w");

// Get active layers
var idx = get_selected_layers_idx();

var n = 0;
try { activeDocument.backgroundLayer } catch (e) { n = 1; }

// Ask for confirmation once before processing all layers
var userConfirmed = confirm("You are about to process " + idx.length + " layer(s). Click OK to continue.");
if (!userConfirmed) {
return; // Stop if the user cancels
}

// Process all layers after confirmation
for (var i = 0; i < idx.length; i++)
{
var info = get_layer_info(idx[i] + n);

// Save to file
file.writeln("Layer Name: " + info[0]);
file.writeln("Mid X: " + info[1].toFixed(2));
file.writeln("Mid Y: " + info[2].toFixed(2));
file.writeln("-----------------------");
}

// Close file
file.close();
alert("Results saved to LayerMidXValues.txt on desktop.");
}
catch (e)
{
alert("Error in processSelectedLayers: " + e.message +"\nline:"+e.line);
}
}

////////////////////////////////////////////////////////
// Run the script
processSelectedLayers();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Dec 06, 2024 Dec 06, 2024
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines