Skip to main content
Bedazzled532
Inspiring
August 18, 2024
Answered

Move RulerOrigin using script in Indesign

  • August 18, 2024
  • 1 reply
  • 1119 views

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").

This topic has been closed for replies.
Correct answer rob day

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.


Hi @Bedazzled532 , 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]];

})();

1 reply

m1b
Braniac
August 18, 2024

Hi @Bedazzled532, 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

Bedazzled532
Inspiring
August 18, 2024

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

I would like to know where I was wrong. Thanks

m1b
Braniac
August 18, 2024

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