How to Automatically set x y Coordinates to a Selected Shape

Participant ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

Hello,

It's been a long time friends. I have an idea in my head that I can't shake and I'm hoping one of you may know just how to crack this.

 

Is there a way to automatically set the x y coordinates to 0,0 at the upper left corner of the bounding box of a selected shape in illustrator?

Capture.JPG

 

Thank you so much!

TOPICS
Draw and design, How to, Scripting

Views

182

Likes

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

Enthusiast , Feb 18, 2021 Feb 18, 2021
femkeblanco is right, but it's really hard to tell exactly what you mean from your question. It'd be great if you edited to make it clearer for us.  Just for a different answer, I'm going to assume you want to set the *ruler* coordinates *to* the coordinates of the selected item. It seems like a simple thing to do, but there's a few parts to it. setArtboardRulerOriginToPositionOfItem(selection[0]); function setArtboardRulerOriginToPositionOfItem(item) { // first set the documents ruler o...

Likes

Translate

Translate
Enthusiast ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

selection[0].position = [0, 0];

Likes

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
Enthusiast ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

femkeblanco is right, but it's really hard to tell exactly what you mean from your question. It'd be great if you edited to make it clearer for us. 🙂

 

Just for a different answer, I'm going to assume you want to set the *ruler* coordinates *to* the coordinates of the selected item. It seems like a simple thing to do, but there's a few parts to it.

 

 

 

setArtboardRulerOriginToPositionOfItem(selection[0]);

function setArtboardRulerOriginToPositionOfItem(item) {
    // first set the documents ruler origin
    app.activeDocument.rulerOrigin = [0, 0]; // if we don't reset, item position will give wrong coordinates
    app.activeDocument.rulerOrigin = [item.geometricBounds[0], item.geometricBounds[1]];
    // then set item artboard's ruler origin
    var itemArtboard = app.activeDocument.artboards[artboardIndex];
    // find out for which artboard to set the ruler origin
    var artboardIndex = getArtboardIndicesOfItem(item);
    // item may have no artboard (we'll use the first in document),
    // or may have multiple (we'll use its first)
    artboardIndex = artboardIndex.length == 0 ? 0 : artboardIndex[0];
    // get the artboard
    var itemArtboard = app.activeDocument.artboards[artboardIndex]
    // work out distance from top left of artboard
    var dx = itemArtboard.artboardRect[0] - item.geometricBounds[0];
    var dy = itemArtboard.artboardRect[1] - item.geometricBounds[1];
    // set the ruler origin for the artboard to that distance
    itemArtboard.rulerOrigin = [-dx, dy];
}

function getArtboardIndicesOfItem(item) {
    // returns array of index of each artboard item intersects
    var bounds = item.visibleBounds;
    var abs = app.activeDocument.artboards;
    var indices = [];
    for (var i = 0; i < abs.length; i++) {
        if (boundsIntersectWithBounds(bounds, abs[i].artboardRect)) {
            indices.push(i);
        }
    }
    return indices
}

function boundsIntersectWithBounds(b1, b2) {
    return !(b2[0] > b1[2] || b2[2] < b1[0] || b2[1] < b1[3] || b2[3] > b1[1]);
}

 

Edit: added line to set the document ruler too so it works if you are looking at "Global Rulers" as opposed to "Artboard Rulers". Also, you should decide if you want geometricBounds or visibleBounds. VisibleBounds includes strokes and, I think, effects.

Screen Shot 2021-02-19 at 12.20.59 pm.png

 

 

 

- Mark

Likes

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
Participant ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

Hey sorry if I wasn't clear.   

 

Hopefully, this explains it better 🙂

 

So I have an oversized artboard with all of my graphic styles and more that I use for every project. 

 

When I create a vector project it could be anywhere from 2"x5" to 48"x96" 

Once I have Rectangle or Circle shape drawn, in order to be sure all of the elements are placed correctly, I always set the top left corner of the bounding box to the 0,0 coordinates so that way I can always be sure any additional elements are placed correctly.

 

I do this by manually clicking and dragging the upper corner of the Rulers to the desired spot, and because the sizes and shapes vary so much it's different every time but it is a task I repeat multiple times a day.  So that got me thinking that there had to be a way to automate that step.

 

Likes

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
Participant ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

I should also state that, while I have worked really well with actions I am not so good with scripts.

Capture2.JPG

 

Likes

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
Enthusiast ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

Oops! My bad code. Didn't test with no artboard intersecting. Fixed now I think. - Mark

Likes

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
Enthusiast ,
Feb 18, 2021 Feb 18, 2021

Copy link to clipboard

Copied

LATEST

Hi @BryanPagenkopf, would you mind editing the topic heading to something more like: "How to set ruler origin to page item position"? It would make your question much more useful to others who have the same question. - Mark

Likes

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