Skip to main content
Participant
January 19, 2025
Question

Repositioning text in a document

  • January 19, 2025
  • 2 replies
  • 372 views

Any ideas why this script isn't working as desired? I'm trying to write a simple script that moves any text boxes / objects (grouping them first if there's multiple) to an exact x and y position on the page. 

 

So if the current position of the text box is .5 inches and 1.5 inches, and I want to just move all of the text boxes down and inch so its new position is .5 inches and 2.5 inches, why is this moving the text box to a weird position when I type in .5 and 2.5 into the dialog box?

 

// @targetengine "session"

function repositionTextFrames() {
    // Check if there's an active document
    if (app.documents.length === 0) {
        alert("Please open a document first.");
        return;
    }

    var myDoc = app.activeDocument;
    
    // Create dialog box
    var myDialog = new Window("dialog", "Reposition Text Frames");
    myDialog.orientation = "column";
    
    // Create input groups
    var coordGroup = myDialog.add("group");
    coordGroup.orientation = "row";
    
    // X coordinate input
    coordGroup.add("statictext", undefined, "X Position (inches):");
    var xInput = coordGroup.add("edittext", undefined, "0");
    xInput.characters = 6;
    
    // Y coordinate input
    coordGroup.add("statictext", undefined, "Y Position (inches):");
    var yInput = coordGroup.add("edittext", undefined, "0");
    yInput.characters = 6;
    
    // Add buttons
    var buttonGroup = myDialog.add("group");
    buttonGroup.orientation = "row";
    var okButton = buttonGroup.add("button", undefined, "OK");
    var cancelButton = buttonGroup.add("button", undefined, "Cancel");
    
    // Show dialog
    if (myDialog.show() == 1) {
        // Convert inches to points (1 inch = 72 points)
        var xPos = parseFloat(xInput.text) * 72;
        var yPos = parseFloat(yInput.text) * 72;
        
        // Process each page
        for (var i = 0; i < myDoc.pages.length; i++) {
            var currentPage = myDoc.pages[i];
            var textFrames = [];
            
            // Collect all text frames on the page
            for (var j = 0; j < currentPage.textFrames.length; j++) {
                textFrames.push(currentPage.textFrames[j]);
            }
            
            // Skip if no text frames found
            if (textFrames.length === 0) continue;
            
            // Group text frames if multiple exist
            var textGroup;
            if (textFrames.length > 1) {
                textGroup = currentPage.groups.add(textFrames);
            } else {
                textGroup = textFrames[0];
            }
            
            try {
                // Get the current position of the text group
                var bounds = textGroup.geometricBounds;
                
                // Calculate the target position relative to the page margins
                var targetX = currentPage.marginPreferences.left + xPos;
                var targetY = currentPage.marginPreferences.top + yPos;
                
                // Calculate the amount we need to move the text group
                var deltaX = targetX - bounds[1];
                var deltaY = targetY - bounds[0];
                
                // Move the text group
                textGroup.move(undefined, [deltaX, deltaY]);
                
            } catch (e) {
                alert("Error repositioning text on page " + (i + 1) + ": " + e);
            }
        }
        
        alert("Text repositioning complete!");
    }
}

// Run the script
repositionTextFrames();

2 replies

Peter Kahrel
Community Expert
Community Expert
January 20, 2025

As Robert mentioned, you should use the first parameter of move(), which is 'move to'. You're currently using 'move by', which moves distances relative to the object's current position.

Participant
February 7, 2025

Sorry, can you explain what I should be using? I have 

textGroup.move(undefined, [deltaX, deltaY]);

 

so I need to change this to what?

Robert at ID-Tasker
Legend
February 7, 2025
quote

Sorry, can you explain what I should be using? I have 

textGroup.move(undefined, [deltaX, deltaY]);

 

so I need to change this to what?

 

By @Jaime307149798mu6


I think you need to skip "undefined":

 

textGroup.move(, [deltaX, deltaY]);

 

But leave comma, so InDesign will know that you want to move by a vector.

 

Robert at ID-Tasker
Legend
January 19, 2025
// Move the text group
                textGroup.move(undefined, [deltaX, deltaY]);

 

First param is TO and second is BY:

 

https://community.adobe.com/t5/indesign-discussions/examples-of-the-move-method-in-scripting-move-object-to-a-specific-page/m-p/11041952#M182128

 

I'm replying from my phone and I'm not JS guy so if the above doesn't help - you need to be more specific with "weird position".

 

Participant
January 19, 2025

Weird as in unexpected. If I put 0,0 then it will move it to that position. But if I put 1,1 then it moves it to a location I can't even find in the document. I'm not able to see or select the text or even move it back to a 0,0 position using the same script. 

Robert at ID-Tasker
Legend
January 19, 2025

What are your document's units? 

 

1 INCH * 72 = 72 Inches - which is a lot... 

 

And if your page is much smaller...