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

Change the rulers original point to center of selected object

Explorer ,
Jan 26, 2023 Jan 26, 2023

Copy link to clipboard

Copied

I am trying to write a JavaScript that changes the rulers orgin point to the center of the current selected object.

 

Here is my code:

var doc = app.activeDocument;
var sel = doc.selection;
if (sel.length > 0) {
var obj = sel[0];
var objBounds = obj.geometricBounds;
var objX = (objBounds[0] + objBounds[2]) / 2;
var objY = (objBounds[1] + objBounds[3]) / 2;
doc.rulerOrigin = [objX, objY];
}

 

It quits out on this line:

var sel = doc.selection;

 

Error:

Underfined is not an object

 

Can someone point me in the right direction.

 

Thank You

TOPICS
Scripting

Views

402

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

Explorer , Feb 06, 2023 Feb 06, 2023

Hi @m1b , @pixxxelschubser , @femkeblanco , @CarlosCanto ,

 

Thanks for the help. But I over thought this in needing a script for the task. I was able to do it using actions.

 

Thanks again.

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 26, 2023 Jan 26, 2023

Copy link to clipboard

Copied

are you running the script from the ESTK? you have to select Illustrator as a target application

Votes

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
Explorer ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

ESTK? I am running the script inside of Illustrator.

 

Thanks for the response

Votes

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
Guide ,
Jan 26, 2023 Jan 26, 2023

Copy link to clipboard

Copied

Try

doc.artboards[0].rulerOrigin = [objX, -objY];

Votes

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
Explorer ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

Try:

doc.artboards[0].rulerOrigin = [objX, -objY];
In place of?
 

Thanks for the response

 
 

Votes

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
Guide ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

In place of this line

doc.rulerOrigin = [objX, objY];

Votes

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 Expert ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

I would rather replace this line:

var objY = (objBounds[1] + objBounds[3]) / 2;

 

replace with this line:

var objY = (objBounds[1] + objBounds[3]) / 2 -doc.artboards[doc.artboards.getActiveArtboardIndex()].artboardRect[3];

 

Votes

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 Expert ,
Jan 27, 2023 Jan 27, 2023

Copy link to clipboard

Copied

Hi @Deprive, it looks like you probably have this covered, but I wanted to chime in myself, because, strangely, none of the scripts/modifications you guys @femkeblanco@pixxxelschubser mentioned actually work on my system (Mac OS 13.1, AI 27.2). They just don't move the ruler origin—nothing happens.

 

Here is what I do and works for me. I really do not understand what I'm doing in part of this script, but since it works, I figure I can post it! (Also @pixxxelschubser I am laughing when I post this because you may be appalled by how much of a long process I make something so simple, haha, but that is just how my mind and system works 🙂 ).

- Mark

 

/**
 * Sets the artboard's ruler origin to selected item's center.
 * @discussion https://community.adobe.com/t5/illustrator-discussions/change-the-rulers-original-point-to-center-of-selected-object/m-p/13531771
 */

(function () {

    var doc = app.activeDocument,
        item = doc.selection[0];

    setArtboardRulerOriginToItemCenter(item);



    /**
     * Sets artboard ruler origin to item's center.
     * @author m1b
     * @version 2022-10-08
     * @param {PageItem} item - an Illustrator PageItem.
     */
    function setArtboardRulerOriginToItemCenter(item) {

        if (item == undefined)
            return;

        var doc = getParentDocument(item),

            // I'll restore this later
            oldOrigin = doc.rulerOrigin,

            // get the item's artboard, or the first
            itemArtboard = getArtboardOfItem(item) || doc.artboards[0];

        // first reset the document's ruler origin
        doc.rulerOrigin = [0, 0];

        // if I don't calculate the center *after* resetting the doc.rulerOrigin,
        // the item coordinates may be incorrect
        var centre = centerOfBounds(item.geometricBounds);

        // I honestly don't understand what I'm doing here
        // but if I don't do this it doesn't work
        doc.rulerOrigin = [centre[0], centre[1]];

        // offset the artboard's ruler origin by it's position in document
        itemArtboard.rulerOrigin = [-itemArtboard.artboardRect[0], itemArtboard.artboardRect[1]];

        // cleanup
        doc.rulerOrigin = oldOrigin;

    };


    /**
     * Returns first artboard that shares
     * bounding box with the item.
     * @author m1b
     * @version 2022-07-25
     * @param {PageItem} item - an Illustrator page item.
     * @returns {Artboard}
     */
    function getArtboardOfItem(item) {

        var bounds = item.visibleBounds,
            abs = app.activeDocument.artboards;

        for (var i = 0; i < abs.length; i++)
            if (boundsIntersectWithBounds(bounds, abs[i].artboardRect))
                return abs[i];

    };


    /**
     * Returns true when b1 intersections with b2.
     * @param {Array<Number>} b1 - [l, t, r, b].
     * @param {Array<Number>} b2 - [l, t, r, b].
     * @returns {Boolean}
     */
    function boundsIntersectWithBounds(b1, b2) {

        return !(
            b2[0] > b1[2]
            || b2[2] < b1[0]
            || b2[1] < b1[3]
            || b2[3] > b1[1]
        );

    };


    /**
     * Returns the center of bounds.
     * @param {Arrray<Number>} bounds - [l,t,r,b].
     * @returns {Arrray<Number>} - [x, y].
     */
    function centerOfBounds(bounds) {

        var l = bounds[0],
            t = bounds[1],
            r = bounds[2],
            b = bounds[3];

        return [
            l + ((r - l) / 2),
            t + ((b - t) / 2)
        ];

    };


    /**
     * Returns the item's document, if possible.
     * @param {PageItem} item - an Illustrator page item.
     * @returns {Document}
     */
    function getParentDocument(item) {

        while (
            item.hasOwnProperty('parent')
            && item.constructor.name != 'Document'
        )
            item = item.parent;

        return item;

    };



})();

 

Votes

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
Explorer ,
Feb 06, 2023 Feb 06, 2023

Copy link to clipboard

Copied

Hi @m1b , @pixxxelschubser , @femkeblanco , @CarlosCanto ,

 

Thanks for the help. But I over thought this in needing a script for the task. I was able to do it using actions.

 

Thanks again.

Votes

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 Expert ,
Feb 06, 2023 Feb 06, 2023

Copy link to clipboard

Copied

LATEST

Well done @Deprive on solving it. Are you able to post the action for others?

Votes

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