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

InDesign script help - Units from selections and units in input boxes

Participant ,
Dec 12, 2023 Dec 12, 2023

Copy link to clipboard

Copied

Hello, 

There is a script below which changes the value of the width and height of many objects at the same time. 

Two issues with it 

One: I want a user to be able to leave a field blank and the script then uses the left out field as the value for the object size. Importantly, this could run into issues if there are many selected items which each have differerent sizes.... so maybe this i a concept that is impractical? 

Two: This is a comment issue I run into, dialogue boxes in ID usally display the unit that document is set up in, best seen in the screenshots below 

 

SmythWharf_0-1702419611769.pngSmythWharf_1-1702419652898.png

 

 

However in this script below, there is a horrible pt 

SmythWharf_2-1702419725928.png

 

The pt is not important - this is becuase no matter what the user has their document unit preferance set in the input value becomes the size of whatever thing they want to resize - thus the pt is not important 

How can I either remove the pt or have the pt change to show the unit which the document is set. importantly the in the latter as ID converts inputs to points, also dont want a sitution where 40mm becomes xxxxxpt as it converts it..... perhaps I have this issue wrong 

// Wrap the script in app.doScript for a single undoable step
app.doScript(function() {
    // Define the dialog
    var myDialog = app.dialogs.add({name: "Change Object Size"});
    with (myDialog.dialogColumns.add()) {
        with (dialogRows.add()) {
            with (dialogColumns.add()) {
                staticTexts.add({staticLabel: "Width:"});
                staticTexts.add({staticLabel: "Height:"});
            }
            with (dialogColumns.add()) {
                var widthEditbox = measurementEditboxes.add();
                var heightEditbox = measurementEditboxes.add();
            }
        }
    }

    // Show the dialog
    if (myDialog.show() == true) {
        // Get the user input values
        var newHeight = widthEditbox.editValue;
        var newWidth = heightEditbox.editValue;

        // Close the dialog
        myDialog.destroy();

        // Get the selected objects
        var selectedObjects = app.activeDocument.selection;

        // Loop through selected objects and change their size from the center
        for (var i = 0; i < selectedObjects.length; i++) {
            var currentObject = selectedObjects[i];

            // Check if the selected object is a valid page item
            if (currentObject.hasOwnProperty("geometricBounds")) {
                // Get the current geometric bounds
                var currentBounds = currentObject.geometricBounds;

                // Calculate the center of the current bounds
                var centerX = (currentBounds[0] + currentBounds[2]) / 2;
                var centerY = (currentBounds[1] + currentBounds[3]) / 2;

                // Calculate the new bounds
                var newBounds = [
                    centerX - newWidth / 2,
                    centerY - newHeight / 2,
                    centerX + newWidth / 2,
                    centerY + newHeight / 2
                ];

                // Apply the new geometric bounds
                currentObject.geometricBounds = newBounds;
            }
        }
    } else {
        // Dialog was canceled
        myDialog.destroy();
    }
}, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change Object Size");
TOPICS
How to , Scripting , SDK

Views

117

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 ,
Dec 23, 2023 Dec 23, 2023

Copy link to clipboard

Copied

Just throwing my hat at it - not the best at this but hope it helps a little

I'm guessing this probably won't work out of the box - but hope it helps in some way.

// Get the current document unit
var documentUnit = document.measurementUnits;

// Wrap the script in app.doScript for a single undoable step
app.doScript(function() {
    // Define the dialog with dynamic labels based on the document unit
    var myDialog = app.dialogs.add({name: "Change Object Size"});
    with (myDialog.dialogColumns.add()) {
        with (dialogRows.add()) {
            with (dialogColumns.add()) {
                staticTexts.add({staticLabel: "Width: ({0})".format(documentUnit)});
                staticTexts.add({staticLabel: "Height: ({0})".format(documentUnit)});
            }
            with (dialogColumns.add()) {
                var widthEditbox = measurementEditboxes.add();
                var heightEditbox = measurementEditboxes.add();
            }
        }
    }

    // Show the dialog
    if (myDialog.show() == true) {
        // Get the user input values
        var newHeight = widthEditbox.editValue;
        var newWidth = heightEditbox.editValue;

        // Check if both fields are not empty
        if (newHeight && newWidth) {
            // Close the dialog
            myDialog.destroy();

            // Get the selected objects
            var selectedObjects = app.activeDocument.selection;

            // Loop through selected objects and change their size from the center
            for (var i = 0; i < selectedObjects.length; i++) {
                var currentObject = selectedObjects[i];

                // Check if the selected object is a valid page item
                if (currentObject.hasOwnProperty("geometricBounds")) {
                    // Get the current geometric bounds
                    var currentBounds = currentObject.geometricBounds;

                    // Calculate the center of the current bounds
                    var centerX = (currentBounds[0] + currentBounds[2]) / 2;
                    var centerY = (currentBounds[1] + currentBounds[3]) / 2;

                    // Calculate the new bounds
                    var newBounds = [
                        centerX - newWidth / 2,
                        centerY - newHeight / 2,
                        centerX + newWidth / 2,
                        centerY + newHeight / 2
                    ];

                    // Apply the new geometric bounds
                    currentObject.geometricBounds = newBounds;
                }
            }
        } else {
            // Show an error message if a field is empty
            myDialog.alert("Please enter a value for both the width and height.");
        }
    } else {
        // Dialog was canceled
        myDialog.destroy();
    }
}, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change Object Size");

 

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 ,
Dec 23, 2023 Dec 23, 2023

Copy link to clipboard

Copied

How can I either remove the pt

 

Check the properties of the dialogColumn object. Maybe it has some useful property other than measurementEditBox.

 

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 ,
Dec 23, 2023 Dec 23, 2023

Copy link to clipboard

Copied

LATEST

Hi @SmythWharf , I think this is the same question you asked here:

 

https://community.adobe.com/t5/indesign-discussions/script-ui-issue-script-allows-change-of-w-amp-h-...

 

The part that is still missing from your code is setting the scripting preferences measurement unit to points at the top of the script and resetting at the end outside of the loop. You have to do that because a measurementEditbox always converts the returned values to points, so this works:

 

 
// Wrap the script in app.doScript for a single undoable step
app.doScript(function() {
    // Define the dialog
    var myDialog = app.dialogs.add({name: "Change Object Size"});
    with (myDialog.dialogColumns.add()) {
        with (dialogRows.add()) {
            with (dialogColumns.add()) {
                staticTexts.add({staticLabel: "Width:"});
                staticTexts.add({staticLabel: "Height:"});
            }
            with (dialogColumns.add()) {
                var widthEditbox = measurementEditboxes.add({editUnits:app.activeDocument.viewPreferences.horizontalMeasurementUnits});
                var heightEditbox = measurementEditboxes.add({editUnits:app.activeDocument.viewPreferences.verticalMeasurementUnits});
            }
        }
    }

    // Show the dialog
    if (myDialog.show() == true) {
        
/////////////Set Scripting units to points/////////////
        app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
        
        // Get the user input values
        var newHeight = widthEditbox.editValue;
        var newWidth = heightEditbox.editValue;

        // Close the dialog
        myDialog.destroy();

        // Get the selected objects
        var selectedObjects = app.activeDocument.selection;

        // Loop through selected objects and change their size from the center
        for (var i = 0; i < selectedObjects.length; i++) {
            var currentObject = selectedObjects[i];

            // Check if the selected object is a valid page item
            if (currentObject.hasOwnProperty("geometricBounds")) {
                // Get the current geometric bounds
                var currentBounds = currentObject.geometricBounds;

                // Calculate the center of the current bounds
                var centerX = (currentBounds[0] + currentBounds[2]) / 2;
                var centerY = (currentBounds[1] + currentBounds[3]) / 2;

                // Calculate the new bounds
                var newBounds = [
                    centerX - newWidth / 2,
                    centerY - newHeight / 2,
                    centerX + newWidth / 2,
                    centerY + newHeight / 2
                ];

                // Apply the new geometric bounds
                currentObject.geometricBounds = newBounds;
            }
        }
        ////////////Reet Scripting units to defaults//////////////
        app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
    } else {
        // Dialog was canceled
        myDialog.destroy();
    }
}, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Change Object Size");

 

 

Screen Shot 1.pngScreen Shot 2.png

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