Skip to content

Instantly share code, notes, and snippets.

@philippgitpush
Last active June 5, 2024 18:46
Show Gist options
  • Save philippgitpush/065945e13434d319ad862a7f5ebdd1e4 to your computer and use it in GitHub Desktop.
Save philippgitpush/065945e13434d319ad862a7f5ebdd1e4 to your computer and use it in GitHub Desktop.
Adobe InDesign script for extracting object coordinates
// Load the required libraries
#target "InDesign"
#targetengine "session"
// Create a new panel
var myPanel = new Window("palette", "Coordinates Extraction", undefined);
// Add content to the panel
var label = myPanel.add("statictext", undefined, "Coordinates:");
label.alignment = "left";
var coordinatesTextBox = myPanel.add("edittext", undefined, "", {
readonly: true,
});
coordinatesTextBox.alignment = "fill";
var offsetGroup = myPanel.add("group");
var offsetXLabel = offsetGroup.add("statictext", undefined, "Offset X:");
var offsetXInput = offsetGroup.add("edittext", undefined, "0");
offsetXInput.characters = 6;
var offsetYLabel = offsetGroup.add("statictext", undefined, "Offset Y:");
var offsetYInput = offsetGroup.add("edittext", undefined, "0");
offsetYInput.characters = 6;
// Set the event listeners for updating the coordinates
offsetXInput.onChanging = updateCoordinates;
offsetYInput.onChanging = updateCoordinates;
// Function to update the coordinates
function updateCoordinates(event) {
// Get the active document
var doc = app.activeDocument;
// Check if an object is selected
if (doc.selection.length === 1) {
var selectedObject = doc.selection[0];
// Check if the selected object has geometric bounds property
if (selectedObject.hasOwnProperty("geometricBounds")) {
// Get the top-left and bottom-right coordinates
var x1 = roundToThreeDecimals(selectedObject.geometricBounds[1] + parseFloat(offsetXInput.text));
var y1 = roundToThreeDecimals(selectedObject.geometricBounds[0] + parseFloat(offsetYInput.text));
var x2 = roundToThreeDecimals(selectedObject.geometricBounds[3] + parseFloat(offsetXInput.text));
var y2 = roundToThreeDecimals(selectedObject.geometricBounds[2] + parseFloat(offsetYInput.text));
// Format the coordinates
var coordinates = x1 + " " + y1 + " " + x2 + " " + y2;
// Update the text box with the coordinates
coordinatesTextBox.text = coordinates;
} else {
// Display "No object with geometric bounds" if the selected object doesn't have geometric bounds
coordinatesTextBox.text = "No object with geometric bounds";
}
} else {
// Display "No objects selected" if no object is selected
coordinatesTextBox.text = "No objects selected";
}
}
// Function to round a number to three decimal places
function roundToThreeDecimals(number) {
return parseFloat(number.toFixed(3));
}
// Function to handle the object click event
function objectClickEventHandler(event) {
// Update the coordinates when an object is clicked
updateCoordinates();
}
// Add the object click event listener to the active document
app.activeDocument.addEventListener("afterSelectionAttributeChanged", objectClickEventHandler);
app.activeDocument.addEventListener("afterSelectionChanged", objectClickEventHandler);
// Update coordinates on startup
updateCoordinates();
// Add a manual refresh button
var refreshButton = myPanel.add("button", undefined, "Refresh");
refreshButton.alignment = "left";
refreshButton.onClick = updateCoordinates;
// Show the panel
myPanel.show();
@philippgitpush
Copy link
Author

Preview in Adobe InDesign

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment