Skip to main content
Known Participant
July 10, 2025
Answered

Automatic centered markers

  • July 10, 2025
  • 4 replies
  • 1003 views

Hello, in Photoshop, I used a shortcut to automatically add horizontally and vertically centered guides. Is this possible in InDesign? Thanks for your answers!
Olivier

Correct answer Mike Witherell

The above script puts guides on the page but makes a mistake in positioning the Horizontal Guide. It sits too high up on a default portrait page size. Oddly, if I undo one step, the guide moves to the center. Can anyone figure out the mistake?

Also, adding the page centering guide feature seems to break the original rest of the script that puts guides around objects. 

4 replies

Mike Witherell
Mike WitherellCorrect answer
Community Expert
July 10, 2025

The above script puts guides on the page but makes a mistake in positioning the Horizontal Guide. It sits too high up on a default portrait page size. Oddly, if I undo one step, the guide moves to the center. Can anyone figure out the mistake?

Also, adding the page centering guide feature seems to break the original rest of the script that puts guides around objects. 

Mike Witherell
Known Participant
July 10, 2025

thank you for your answers !!

Mike Witherell
Community Expert
July 10, 2025

I thought it would be pretty cool to combine this above script with the built-in script named AddGuides.jsx which "sort of" does what you want it to do (although it requires you draw/select a shape first). Here is my rough take on a combined script based on AddGuides.jsx:

//MikesAddGuides2025.jsx
//An InDesign JavaScript
/*  
Based on "AddGuides.jsx" 3.0.0.0 14 December 2009
*/
//This script draws guides around the selected object or objects.
//Added in July 2025: This script also can put center guides on a page.
//Choose Visible Bounds to consider the stroke weight of the paths when placing the guides, 
//or choose Geometric Bounds to ignore the stroke weight when placing the guides
//Choose Each Item to position the guides around individual items in the selection, 
//or Entire Selection to position the guides around the selection as a whole.
//

main();
function main(){
    //Make certain that user interaction (display of dialogs, etc.) is turned on.
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
    var myObjectList = new Array;
    var allowCurrentPageGuides = false;
    var haveSelectionObjects = false;

    if (app.documents.length == 0) {
        alert ("Please open a document, select an object or choose current page guides, and try again.");
        return;
    }

    // Check if there are selected objects of desired types
    if (app.selection.length != 0){
        for(var myCounter = 0;myCounter < app.selection.length; myCounter++){
            switch (app.selection[myCounter].constructor.name){
                case "Rectangle":
                case "Oval":
                case "Polygon":
                case "TextFrame":
                case "Group":
                case "Button":
                case "GraphicLine":
                    myObjectList.push(app.selection[myCounter]);
                    break;
            }
        }
        if (myObjectList.length != 0){
            haveSelectionObjects = true;
        }
    }

    // Show the dialog and get user choice
    var dialogResult = myDisplayDialog(myObjectList.length, haveSelectionObjects);
    if(dialogResult === null){
        // User cancelled dialog
        return;
    }

    allowCurrentPageGuides = dialogResult.allowCurrentPageGuides;

    if(allowCurrentPageGuides){
        // Add guides on current page only
        var doc = app.activeDocument;
        var page = app.layoutWindows[0].activePage;
        var bounds = page.bounds; // [y1, x1, y2, x2]
        var x1 = bounds[1], y1 = bounds[0], x2 = bounds[3], y2 = bounds[2];

        var myOldRulerOrigin = doc.viewPreferences.rulerOrigin;
        doc.viewPreferences.rulerOrigin = RulerOrigin.spineOrigin;

        // Create "Guides" layer or get it if exists
        var myLayer;
        try{
            myLayer = doc.layers.item("Guides");
            var myLayerName = myLayer.name; // Test if layer exists
        }
        catch(e){
            myLayer = doc.layers.add({name:"Guides"});
        }

        // Add horizontal guide at vertical center
        page.guides.add(myLayer, undefined, undefined).orientation = HorizontalOrVertical.horizontal;
        page.guides[-1].location = y1 + (y2 - y1)/2;

        // Add vertical guide at horizontal center
        page.guides.add(myLayer, undefined, undefined).orientation = HorizontalOrVertical.vertical;
        page.guides[-1].location = x1 + (x2 - x1)/2;

        doc.viewPreferences.rulerOrigin = myOldRulerOrigin;

        alert("Guides added on current page center.");
        return;
    }
    else{
        if(!haveSelectionObjects){
            alert ("Please select a page item and try again.");
            return;
        }
    }

    // If here, proceed with original logic with selection
    var myOldXUnits = app.documents.item(0).viewPreferences.horizontalMeasurementUnits;
    var myOldYUnits = app.documents.item(0).viewPreferences.verticalMeasurementUnits;
    app.documents.item(0).viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
    app.documents.item(0).viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;

    myDisplayDialog(null, myObjectList); // Call with object list to continue original flow

    app.documents.item(0).viewPreferences.horizontalMeasurementUnits = myOldXUnits;
    app.documents.item(0).viewPreferences.verticalMeasurementUnits = myOldYUnits;
}

function myDisplayDialog(emptySelectionLength, myObjectList){
    // If myObjectList is null, show dialog with current page guides only.
    var myTopCheckbox, myLeftCheckbox, myBottomCheckbox, myRightCheckbox, myXCenterCheckbox, myYCenterCheckbox, myXPointCheckbox, myYPointCheckbox;
    var myRangeButtons, myBasedOnButtons;
    var myCurrentPageCheckbox;
    var myXOffsetField, myYOffsetField;

    var myLabelWidth = 100;
    var myDialog = app.dialogs.add({name:"AddGuides"});
    with(myDialog){
        with(dialogColumns.add()){
            with(borderPanels.add()){
                staticTexts.add({staticLabel:"Add Guides At:"});
                with(dialogColumns.add()){
                    myTopCheckbox = checkboxControls.add({staticLabel:"&Top", checkedState:false});
                    myLeftCheckbox = checkboxControls.add({staticLabel:"&Left", checkedState:false});
                    myBottomCheckbox = checkboxControls.add({staticLabel:"&Bottom", checkedState:false});
                    myRightCheckbox = checkboxControls.add({staticLabel:"&Right", checkedState:false});
                    myXCenterCheckbox = checkboxControls.add({staticLabel:"&Horizontal Center", checkedState:false});
                    myYCenterCheckbox = checkboxControls.add({staticLabel:"&Vertical Center", checkedState:false});
                    myXPointCheckbox = checkboxControls.add({staticLabel:"Path Point Hori&zontal Anchor", checkedState:false});
                    myYPointCheckbox = checkboxControls.add({staticLabel:"Path Point Verti&cal Anchor", checkedState:false});
                }
            }
            with(borderPanels.add()){
                staticTexts.add({staticLabel:"Add Guides Around:"});
                with(myRangeButtons = radiobuttonGroups.add()){
                    radiobuttonControls.add({staticLabel:"Each &Object", checkedState:true, minWidth:156});
                    radiobuttonControls.add({staticLabel:"Entire &Selection"});
                }
            }
            with(borderPanels.add()){
                with(dialogColumns.add()){
                    with(dialogRows.add()){
                        with(dialogColumns.add()){
                            staticTexts.add({staticLabel:"Guides Based On:"});
                        }
                        with(dialogColumns.add()){
                            with(myBasedOnButtons = radiobuttonGroups.add()){
                                radiobuttonControls.add({staticLabel:"&Geometric Bounds", checkedState:true, minWidth:160});
                                radiobuttonControls.add({staticLabel:"V&isible Bounds"});
                            }
                        }
                    }
                    with(dialogRows.add()){
                        with(dialogColumns.add()){
                            staticTexts.add({staticLabel:"Horizontal Offset:", minWidth:myLabelWidth});
                        }
                        with(dialogColumns.add()){
                            myXOffsetField = measurementEditboxes.add({editValue:0, editUnits:MeasurementUnits.points});
                        }
                    }
                    with(dialogRows.add()){
                        with(dialogColumns.add()){
                            staticTexts.add({staticLabel:"Vertical Offset:", minWidth:myLabelWidth});
                        }
                        with(dialogColumns.add()){
                            myYOffsetField = measurementEditboxes.add({editValue:0, editUnits:MeasurementUnits.points});
                        }
                    }
                }
            }
            with(borderPanels.add()){
                staticTexts.add({staticLabel:"Option:"});
                with(dialogColumns.add()){
                    myCurrentPageCheckbox = checkboxControls.add({staticLabel:"Add Centering Guides On Current Page", checkedState:false});
                    if(emptySelectionLength > 0){
                        myCurrentPageCheckbox.enabled = false;
                        myCurrentPageCheckbox.checkedState = false;
                    }
                }
            }
        }
    }
    var myReturn = myDialog.show();
    if(myReturn == true){
        var myTop = myTopCheckbox.checkedState;
        var myLeft = myLeftCheckbox.checkedState;
        var myBottom = myBottomCheckbox.checkedState;
        var myRight = myRightCheckbox.checkedState;
        var myXCenter = myXCenterCheckbox.checkedState;
        var myYCenter = myYCenterCheckbox.checkedState;
        var myXPoint = myXPointCheckbox.checkedState;
        var myYPoint = myYPointCheckbox.checkedState;
        var myBasedOn = myBasedOnButtons.selectedButton;
        var myRange = myRangeButtons.selectedButton;
        var myXOffset = myXOffsetField.editValue;
        var myYOffset = myYOffsetField.editValue;
        var allowCurrentPageGuides = myCurrentPageCheckbox.checkedState;

        myDialog.destroy();

        if(allowCurrentPageGuides){
            return {allowCurrentPageGuides:true};
        }
        else{
            if(myTop+myLeft+myBottom+myRight+myXCenter+myYCenter+myXPoint+myYPoint !=0){
                myAddGuides(myTop, myLeft, myBottom, myRight, myXCenter, myYCenter, myRange, myBasedOn, myXOffset, myYOffset, myXPoint, myYPoint, myObjectList);
                return {allowCurrentPageGuides:false};
            }
            else{
                alert("No guides were specified.");
                return null;
            }
        }
    }
    else{
        myDialog.destroy();
        return null;
    }
}

// The rest of the functions remain unchanged:

function myAddGuides(myTop, myLeft, myBottom, myRight, myXCenter, myYCenter, myRange, myBasedOn, myXOffset, myYOffset, myXPoint, myYPoint, myObjectList){
    var myLayer, myCounter;
    var myOldRulerOrigin = app.activeDocument.viewPreferences.rulerOrigin;
    app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.spineOrigin;
    //Create a layer to hold the printers marks (if it does not already exist).
    myLayer = app.activeDocument.layers.item("Guides");
    try{
        myLayerName = myLayer.name;
    }
    catch (myError){
        myLayer = app.activeDocument.layers.add({name:"Guides"});
    }
    //Process the objects in the selection.        
    for(myCounter = 0; myCounter < myObjectList.length; myCounter ++){
        var myObject = myObjectList[myCounter];
        if (myBasedOn == 0){
            myBounds = myObject.geometricBounds;
        }
        else{
            myBounds = myObject.visibleBounds;
        }
        //Draw guides at path point locations, if necessary.
        if ((myXPoint == true)||(myYPoint == true)){
            myDrawGuidesAtPathPoints(myObject, myXPoint, myYPoint);
        }
        //Set up some initial bounding box values.
        if ((myRange != 0)&&(myCounter==0)){
            myX1 = myBounds[1];
            myY1 = myBounds[0];
            myX2 = myBounds[3];
            myY2 = myBounds[2];
        }
        if(myRange == 0){
            myDrawGuides (myBounds[1], myBounds[0], myBounds[3], myBounds[2], myTop, myLeft, myBottom, myRight, myXCenter, myYCenter, myLayer, myXOffset, myYOffset);
        }
        else{
            //Compare the bounds values to the stored bounds.
            //If a given bounds value is less than (for x1 and y1) or 
            //greater than (for x2 and y2) the stored value,
            //then replace the stored value with the bounds value.
            if (myBounds[0] < myY1){
                myY1 = myBounds[0];
            }
            if (myBounds[1] < myX1){
                myX1 = myBounds[1];
            }
            if (myBounds[2] > myY2){
                myY2 = myBounds[2];
            }
            if (myBounds[3] > myX2){
                myX2 = myBounds[3];
            }
        }
    }
    if(myRange != 0){
        myDrawGuides (myX1, myY1, myX2, myY2, myTop, myLeft, myBottom, myRight, myXCenter, myYCenter, myLayer, myXOffset, myYOffset);
    }
    app.activeDocument.viewPreferences.rulerOrigin = myOldRulerOrigin;
}
function myDrawGuidesAtPathPoints(myObject, myXPoint, myYPoint){
    for(var myPathCounter = 0; myPathCounter < myObject.paths.length; myPathCounter++){
        for(var myPointCounter = 0; myPointCounter < myObject.paths.item(myPathCounter).pathPoints.length; myPointCounter ++){
            if(myXPoint==true){
                myDrawGuide(myObject.paths.item(myPathCounter).pathPoints.item(myPointCounter).anchor[0], 1);
            }
            if(myYPoint==true){
                myDrawGuide(myObject.paths.item(myPathCounter).pathPoints.item(myPointCounter).anchor[1], 0);
            }
        }
    }
}
function myDrawGuides(myX1, myY1, myX2, myY2, myTop, myLeft, myBottom, myRight, myXCenter, myYCenter, myLayer, myXOffset, myYOffset){
    if (myTop == true){
        myDrawGuide(myY1 - myYOffset, 0);
    }
    if (myLeft == true){
        myDrawGuide(myX1 - myXOffset, 1);
    }
    if (myBottom == true){
        myDrawGuide(myY2 + myYOffset, 0);
    }
    if (myRight == true){
        myDrawGuide(myX2 + myXOffset, 1);
    }
    if (myXCenter == true){
        myDrawGuide(myX1+((myX2-myX1)/2), 1);
    }
    if (myYCenter == true){
        myDrawGuide(myY1+((myY2-myY1)/2), 0);
    }
}
function myDrawGuide(myGuideLocation, myGuideOrientation){
    var myLayer = app.activeDocument.layers.item("Guides");
    with(app.activeWindow.activeSpread){
        if(myGuideOrientation == 0){
            with (guides.add(myLayer, undefined, undefined)){
                orientation=HorizontalOrVertical.horizontal;
                location=myGuideLocation;
            }
        }
        else{
            with (guides.add(myLayer, undefined, undefined)){
                orientation=HorizontalOrVertical.vertical;
                location=myGuideLocation;
            }
        }
    }
}
function  myValidateNumber(myString){
    var myRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
    return myRegExp.test(myString);
}
Mike Witherell
Willi Adelberger
Community Expert
July 10, 2025

Draw a guide out of the ruler. With the functions of the align panel you can align, distribute and center them betweens objects, pages, margins or selections.

Known Participant
July 10, 2025

Yes it's what I used to do but I'm looking fir a script like I do on Photoshop

Kasyan Servetsky
Brainiac
July 10, 2025

Here I wrote a quick & dirty script that adds two centered guides — horizontal and vertical — centered to the currently active page. (Margins are note taken into account.)

main();

function main() {
	try {
		var page = app.layoutWindows[0].activePage;
		var bounds = page.bounds;
		var width = bounds[3] - bounds[1];
		var height = bounds[2] - bounds[0];
		var halfWidth = width / 2;
		var halfHeight = height / 2;
		var verGuide = page.guides.add({orientation: HorizontalOrVertical.VERTICAL, location: halfWidth});
		var horGuide = page.guides.add({orientation: HorizontalOrVertical.HORIZONTAL, location: halfHeight});
	}
	catch(err) {
		alert("Something went wrong: " + err.message + ", line: " + err.line, true);
	}
}

 Hope it helps!
— Kas

Known Participant
July 10, 2025

Thanks a lot but I never used script. I d'ont know how to use it

Fred.L
Inspiring
July 10, 2025

Hey,
The almighty Kasyan Servetsky has replied to you. It's a sign from the universe, man! For that one reason (and there are so many more), I would personally rush into knowing how to use scripts. 
A quick search on the web to install a script like this
https://help.redokun.com/article/38-install-an-indesign-script
It should be quite thorough

 

Enjoy the script
Once you've started to use them, you can't live without them ^^