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
However in this script below, there is a horrible pt
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");
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");
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.
Copy link to clipboard
Copied
Hi @SmythWharf , I think this is the same question you asked here:
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");
Copy link to clipboard
Copied
Many months later returning to this - sorry missed this wheny you posted it
Will check it out
Best
Smyth
Find more inspiration, events, and resources on the new Adobe Community
Explore Now