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

Move RulerOrigin using script in Indesign

Enthusiast ,
Aug 18, 2024 Aug 18, 2024

Copy link to clipboard

Copied

I have a text frame and I want to move the ruler origin to the top left coordinates of the textframe. i.e move the zero mark to Top Left side of the text frame. I know it can be done manually but I need this in my code.

 

Here is the code which I have writtten but for some reason it is not setting the value.

 

var myDoc = app.activeDocument;
// Ensure there is a selected text frame
if (app.selection.length !== 1 || !(app.selection[0] instanceof TextFrame)) {
    alert("Please select a single text frame.");
} else {
    var textFrame = app.selection[0];	
	var txtBounds = textFrame.geometricBounds;
	var txt_y = txtBounds[0].toFixed(2);
	var txt_x = txtBounds[1].toFixed(2);
	rulerOrigin = [txt_y,txt_x];
	myDoc.viewPreferences.rulerOrigin=rulerOrigin;

 

I am getting following error:
Error Number: 30477
Error String: Invalid value for set property 'rulerOrigin'. Expected RulerOrigin enumerator, but received ("336.00", "103.20").

TOPICS
How to , Scripting

Views

188

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

Community Expert , Aug 18, 2024 Aug 18, 2024

Hi @shahidr100 , The text frame’s geometric bounds is relative to the current zero point, so you would have to set the zero point to [0,0] before you get the geometricBounds, then set a new zero point after you get the bounds

 

/**
 * Move ruler zero point to top left of selected text frame.
 */
(function () {

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

    if (
        !textFrame.isValid
        || 'TextFrame' !== textFrame.constructor.name
    )
        return al
...

Votes

Translate

Translate
Community Expert ,
Aug 18, 2024 Aug 18, 2024

Copy link to clipboard

Copied

Hi @shahidr100, something like this I think:

/**
 * Move ruler zero point to top left of selected text frame.
 */
(function () {

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

    if (
        !textFrame.isValid
        || 'TextFrame' !== textFrame.constructor.name
    )
        return alert("Please select a text frame and try again.");

    var bounds = textFrame.geometricBounds;
    doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
    doc.zeroPoint = [bounds[1], bounds[0]];

})();

 - Mark

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
Enthusiast ,
Aug 18, 2024 Aug 18, 2024

Copy link to clipboard

Copied

@m1b Thank you so much. It worked like a charm.

I would like to know where I was wrong. Thanks

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 ,
Aug 18, 2024 Aug 18, 2024

Copy link to clipboard

Copied

Your code was fine I think. You just were giving the wrong thing to rulerOrigin (it must be a RulerOrigin object) and what you actually wanted to do is set the document's zeroPoint. Only those two things. 
- Mark

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
Enthusiast ,
Aug 18, 2024 Aug 18, 2024

Copy link to clipboard

Copied

Thanks @m1b.

 

I ran into a problem. The script is working fine if the Ruler 0 mark is at the start of the page (i.e top left edge of the page). If for some reason the 0 point is changed manually and then you run the script, it is not setting the 0 point to the selected textframe.

 

I have attached before and after screenshot.

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 ,
Aug 18, 2024 Aug 18, 2024

Copy link to clipboard

Copied

Hi @shahidr100 , The text frame’s geometric bounds is relative to the current zero point, so you would have to set the zero point to [0,0] before you get the geometricBounds, then set a new zero point after you get the bounds

 

/**
 * Move ruler zero point to top left of selected text frame.
 */
(function () {

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

    if (
        !textFrame.isValid
        || 'TextFrame' !== textFrame.constructor.name
    )
        return alert("Please select a text frame and try again.");
    doc.zeroPoint = [0, 0];    
    var bounds = textFrame.geometricBounds;
    doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
    doc.zeroPoint = [bounds[1], bounds[0]];

})();

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
Enthusiast ,
Aug 18, 2024 Aug 18, 2024

Copy link to clipboard

Copied

@rob day Thank you so much @rob day I made the changes and it worked absolutely fine.

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 ,
Aug 18, 2024 Aug 18, 2024

Copy link to clipboard

Copied

LATEST

Oops! I forgot that part. Thanks @rob day!

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