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

Illustrator - Reset Ruler's (x, y) to a given Image's (x, y)

Explorer ,
Oct 06, 2022 Oct 06, 2022

Hey guys,

 

I am trying to reset the ruler of Illustrator to a given (x, y) position from an image.

 

From what I can see I have this:

function ResetOrigins(doc, bounds) {
    preferences.setBooleanPreference("isRulerOriginTopLeft", true);
    preferences.setBooleanPreference("isRulerIn4thQuad", true);

    var abd = doc.artboards[0];

    abd.rulerOrigin = new Array(bounds[0], bounds[1]);
    doc.rulerOrigin = new Array(bounds[0], bounds[1]);
}


var lay = activeDocument.layers.getByName("background");
var img = lay.pageItems[0];
ResetOrigins(app.activeDocument, img.position);

 

But the Y doesn't seem to be working as intended and reset it to the Image's position.

pic.png

TOPICS
Scripting
994
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 1 Correct answer

Community Expert , Oct 06, 2022 Oct 06, 2022

Hi @JSuX, There are some little gotcha's here.

 

First: you are better off not changing the document's rulerOrigin. If you do, your bounds will also have to be adjusted by the same amount or they will not match your item's bounds anymore. I have commented this line out for now.

 

Second: Illustrator is a bit confusing about which way the Y axis ascends. In some coordinate systems or contexts it goes up and others down. I have never bothered to understand which is which because, in practice, I'm

...
Translate
Adobe
Community Expert ,
Oct 06, 2022 Oct 06, 2022

Hi @JSuX, There are some little gotcha's here.

 

First: you are better off not changing the document's rulerOrigin. If you do, your bounds will also have to be adjusted by the same amount or they will not match your item's bounds anymore. I have commented this line out for now.

 

Second: Illustrator is a bit confusing about which way the Y axis ascends. In some coordinate systems or contexts it goes up and others down. I have never bothered to understand which is which because, in practice, I'm used to flipping the Y when it goes wrong. Which I did here.

 

Let me know if it works for you.

- Mark

 

function ResetOrigins(doc, bounds) {

    preferences.setBooleanPreference("isRulerOriginTopLeft", true);
    preferences.setBooleanPreference("isRulerIn4thQuad", true);

    var abd = doc.artboards[0];

    var newOrigin = [bounds[0], bounds[1]];

    // we want the new ruler position expressed
    // in the artboard coordinate system:
    newOrigin = doc.convertCoordinate(newOrigin, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);

    // if you change doc's rulerOrigin,
    // the bounds will now be wrong!
    //  If you really need to change,
    // you can calculate the relative change
    // and apply that to the bounds var.

    // I've commented this out for now:
    // doc.rulerOrigin = [0, 0];

    // the Y axis for the artboard is flipped!
    abd.rulerOrigin = [newOrigin[0], -newOrigin[1]];

}

var lay = activeDocument.layers.getByName("background");
var img = lay.pageItems[0];
ResetOrigins(app.activeDocument, img.position);

 

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
Explorer ,
Oct 07, 2022 Oct 07, 2022

This seems to work, thank you for taking the time, @m1b

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
Community Expert ,
Oct 07, 2022 Oct 07, 2022
quote

Second: Illustrator is a bit confusing about which way the Y axis ascends. In some coordinate systems or contexts it goes up and others down. I have never bothered to understand which is which because, in practice, I'm used to flipping the Y when it goes wrong. Which I did here.

 

- Mark

 

By @m1b

 

Mark, if I remember correctly, when the Y axis changed direction in CS5, new documents created via a script would behave the way they used to (Y going up). When the script manipulated active documents, Y would increase going down.

 

Regardless, I always do what you do, I switch direction myself if it goes the wrong way.

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
Explorer ,
Oct 10, 2022 Oct 10, 2022

@m1b , @CarlosCanto ,

How can you make the Y direction go down? I've been trying to adjust this now since I was able to fix the rulers, but the Y param of positions seems to be going from bottom to top.

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
Community Expert ,
Oct 10, 2022 Oct 10, 2022

I doubt that you can change the direction. I think you just need to go with how it is. And like Carlos and I said, if you code goes the wrong way, flip the sign on Y. In other words, you change the direction in your code. It's weird, but I've never found it especially frustrating.

- Mark

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
Community Expert ,
Oct 10, 2022 Oct 10, 2022

you can change the Y direction. The default is positive Y going down. If in your script Y goes up these lines are probably the cause

    preferences.setBooleanPreference("isRulerOriginTopLeft", true);
    preferences.setBooleanPreference("isRulerIn4thQuad", true);

 

check this old thread for more info

https://community.adobe.com/t5/illustrator-discussions/js-change-cs5-ruler-origin-to-bottom-left-lik...

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
Community Expert ,
Oct 10, 2022 Oct 10, 2022

Thanks @CarlosCanto well now I'm thinking of all my scripts that don't check for this preference before positioning! Haha. Hee. Eeeh.

- Mark

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
Community Expert ,
Oct 10, 2022 Oct 10, 2022

Hi Mark, there shouldn't be a need to check, it's been long enough to assume the preference has not been changed by the user to match CS4 coordinate system. But there's a very slim chance nonetheless 🙂

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
Community Expert ,
Oct 10, 2022 Oct 10, 2022

Hopefully! Well @JSuX @if you change the preference you will know why some scripts may move things the wrong way. 

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
Explorer ,
Oct 11, 2022 Oct 11, 2022
LATEST

I don't think that those two settings do anything at all

preferences.setBooleanPreference("isRulerOriginTopLeft", true);
preferences.setBooleanPreference("isRulerIn4thQuad", true);

What I am trying to achieve is after placing the (0, 0) on an image's position, for the X to increase from left to right, and for the Y to increase from top to bottom. This should also be reflected in the API when calling an (for example) Image's position. At the moment, no matter how you play around with the two settings, after you change the (0, 0) to be at the top, Y is always increasing from bottom to top.

I guess the alternative of just using the negative Y is all that's left

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