Exporting Element Coordinates as Pixel Values

Community Beginner ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

I am working with a client to prepare template print artwork for a custom print tool which will be run through their website. Users pick a template, edit the text in defined fields and change the images in defined frames, and the software generates a print-ready PDF.

 

The tool can accept a PDF upload of the template but requires that all custom elements (in our case mostly text and image swaps) have defined coordinates in pixels. Doing this manually will be quite painstaking so I was wondering if there is a way to export the coordinates of all page elements that we could then work through semi-automatically.

 

So far I've used IDML in a text editor to find the coordinates, but they don't seem to make any logical sense - in the business card I tried as a simple example, the origin point is listed as 317.57972440853706 -380.9635826724157.

 

Is there a way:

  1. To export the co-ordinates in a human-readable way, either with IDMl, XML or an existing script.
  2. Failing that, to find an easy formula to convert the confusing values, as in the example given above, to pixel values with an origin point of 0, 0, using a known DPI and physical dimensions.

 

I suppose a corollary to this is that the method would ideally take less time than simply noting and converting coordinate values manually - though if it's only slightly more, it may have future use. Is this a goal anyone here has accomplished before and if so, how did you go about it?

TOPICS
Import and export, Scripting

Views

129

Likes

translate

Translate

Translate

Report

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 1 Correct Answer

Adobe Community Professional , Feb 01, 2021 Feb 01, 2021
Here it is all put together:  var main = function() { var currHPrefs = app.viewPreferences.horizontalMeasurementUnits; var currVPrefs = app.viewPreferences.verticalMeasurementUnits; app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS; app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS; var allPIs = app.documents[0].allPageItems; for (var i = 0; i < allPIs.length; i++) { try { logger(allPIs[i].constructor.name);...

Likes

translate

Translate

Translate
Adobe Community Professional ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Here's a quick .jsx script that will all log all page item coordinates to a txt file on your desktop called: image_coords_log.txt. Run it on an open doc, with your coords set to pixels. 

 

var main = function() {
    var allPIs = app.documents[0].allPageItems;
    for (var i = 0; i < allPIs.length; i++) {
        try { 
            logger(allPIs[i].geometricBounds);
        } catch(e) {
        }
    }
};


var logger = function(msg) {
    try {
        var f = File(Folder.desktop + "/image_coords_log.txt");
        f.encoding = "UTF-8";
        f.open("a");
        f.writeln(msg);
        f.close();
    }
    catch(e) {
    }
};

main();

 

Likes

translate

Translate

Translate

Report

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
Community Beginner ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Thanks, this seems like a good start. Is there a simple way to add the type of element associated with the coordinates? E.g.:

 

var main = function() {
    var allPIs = app.documents[0].allPageItems;
    for (var i = 0; i < allPIs.length; i++) {
        try { 
            logger(allPIs[i]./*PageItemType*/);
            logger(allPIs[i].geometricBounds);
        } catch(e) {
        }
    }
};

 

Just because these still need to be human-readable to allow us to do the upload and know that the object we're putting at 0,0 is a text box, rather than a line, for example.

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Change your comment to:

constructor.name

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Should note that the numbers you see are the: y1, x1, y2, x2 coordinates, respectively. 

Likes

translate

Translate

Translate

Report

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
Community Beginner ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Thanks! This is working and is nearly there. The coordinates just seem a little bit off. The horizontal and vertical units are set to pixels, but this Oval, for example, is identified as having its origin at X: 483 (which is correct) but Y: 5.9 (when I can see from the rulers that it begins at around Y: 16):

robinw84444352_1-1612204555606.png

Is there anything obvious I've missed?

Likes

translate

Translate

Translate

Report

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
Community Beginner ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

By changing the measurement units I've discovered that it the script is outputting the X coordinates in pixels but the Y coordinates in mm. My settings suggest both dimensions are in pixels - any advice?

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Right click on your Y ruler and change the settings to Pixels. 

 

You could also do it in the script at the start of main(): 

app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;
app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;

 

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Here it is all put together: 

var main = function() {
    var currHPrefs = app.viewPreferences.horizontalMeasurementUnits;
    var currVPrefs = app.viewPreferences.verticalMeasurementUnits;
    app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;
    app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;
    var allPIs = app.documents[0].allPageItems;
    for (var i = 0; i < allPIs.length; i++) {
        try { 
            logger(allPIs[i].constructor.name);
            logger(allPIs[i].geometricBounds);
        } catch(e) {
        }
    }
    app.viewPreferences.horizontalMeasurementUnits = currHPrefs;
    app.viewPreferences.verticalMeasurementUnits = currVPrefs;

};


var logger = function(msg) {
    try {
        var f = File(Folder.desktop + "/image_coords_log.txt");
        f.encoding = "UTF-8";
        f.open("a");
        f.writeln(msg);
        f.close();
    }
    catch(e) {
    }
};

main();

Likes

translate

Translate

Translate

Report

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
Community Beginner ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

This works perfectly. Thank you!

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Hm. Wouldn't it make sense to base this on visibleBounds instead of geometricBounds ?

 

Regards,
Uwe Laubender

( ACP )

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

Unclear what kind of software they're using, but seems like replacing one image at XY coords with another would be the same regardless of whether a stroke was involved or not. OP, if stroke is important and you want to include it in your calculation, use visibleBounds instead of geometricBounds. 

Likes

translate

Translate

Translate

Report

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
Adobe Community Professional ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

I think you’ll need to set the document’s ruler units to pixels rather than the app’s. Also there is the zero point and ruler origin to consider:

 

var main = function() {
    var doc = app.documents[0];
    var currHPrefs = doc.viewPreferences.horizontalMeasurementUnits;
    var currVPrefs = doc.viewPreferences.verticalMeasurementUnits;
    var currRO = doc.viewPreferences.rulerOrigin;
    var currZP = doc.zeroPoint
    
    doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.PIXELS;
    doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.PIXELS;
    doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN
    doc.zeroPoint = [0, 0];
    var allPIs = doc.allPageItems;
    for (var i = 0; i < allPIs.length; i++) {
        try { 
            logger(allPIs[i].constructor.name);
            logger(allPIs[i].geometricBounds);
        } catch(e) {
        }
    }
    doc.viewPreferences.horizontalMeasurementUnits = currHPrefs;
    doc.viewPreferences.verticalMeasurementUnits = currVPrefs;
    doc.viewPreferences.rulerOrigin = currRO
    doc.zeroPoint = currZP;
};


var logger = function(msg) {
    try {
        var f = File(Folder.desktop + "/image_coords_log.txt");
        f.encoding = "UTF-8";
        f.open("a");
        f.writeln(msg);
        f.close();
    }
    catch(e) {
    }
};

main();

 

Likes

translate

Translate

Translate

Report

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