Copy link to clipboard
Copied
Hi All
I want a script to remove all digits from the document except for the text box according to its size
( width and height ) . asked chat gpt 4 , replied with this code but It doesn't work
What is the problem please ?
Thanks
---------------------------------------------------------------------------------------
// Create a dialog with input fields for width and height
var dialog = new Window("dialog", "Excluded Text Box Size");
dialog.orientation = "column";
dialog.alignChildren = "left";
var widthGroup = dialog.add("group");
widthGroup.add("statictext", undefined, "Width (mm):");
var widthInput = widthGroup.add("edittext", undefined, "50");
widthInput.characters = 10;
var heightGroup = dialog.add("group");
heightGroup.add("statictext", undefined, "Height (mm):");
var heightInput = heightGroup.add("edittext", undefined, "25");
heightInput.characters = 10;
var buttonGroup = dialog.add("group");
var okButton = buttonGroup.add("button", undefined, "OK");
okButton.onClick = function() {
var specifiedWidthInMm = parseFloat(widthInput.text); // Convert input to number
var specifiedHeightInMm = parseFloat(heightInput.text); // Convert input to number
dialog.close();
removeDigitsExceptSpecifiedSize(specifiedWidthInMm, specifiedHeightInMm);
};
var cancelButton = buttonGroup.add("button", undefined, "Cancel");
cancelButton.onClick = function() {
dialog.close();
};
dialog.show();
// Remove Digits Function
function removeDigitsExceptSpecifiedSize(specifiedWidthInMm, specifiedHeightInMm) {
var doc = app.activeDocument;
var textFrames = doc.textFrames;
for (var i = 0; i < textFrames.length; i++) {
var textFrame = textFrames[i];
var frameBounds = textFrame.geometricBounds;
var frameWidth = frameBounds[3] - frameBounds[1];
var frameHeight = frameBounds[2] - frameBounds[0];
var specifiedWidthInPoints = specifiedWidthInMm * 2.83464567; // Convert mm to points
var specifiedHeightInPoints = specifiedHeightInMm * 2.83464567; // Convert mm to points
if (frameWidth !== specifiedWidthInPoints || frameHeight !== specifiedHeightInPoints) {
var textContent = textFrame.parentStory.contents;
var textWithoutDigits = textContent.replace(/\d/g, '');
textFrame.parentStory.contents = textWithoutDigits;
}
}
}
It's a pretty horrible script by any standard, but it works:
#targetengine xyz;
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
var dialog = new Window("palette", "Excluded Text Box Size");
dialog.orientation = "column";
dialog.alignChildren = "left";
var widthGroup = dialog.add("group");
widthGroup.add("statictext", undefined, "Width (mm):");
var widthInput = widthGroup.add("edittext", undefined, "50");
widthInput.characters = 10;
var heightGroup
...
Copy link to clipboard
Copied
Probably because of those three lines:
var specifiedWidthInPoints = specifiedWidthInMm * 2.83464567; // Convert mm to points
var specifiedHeightInPoints = specifiedHeightInMm * 2.83464567; // Convert mm to points
rounding error?
if (frameWidth !== specifiedWidthInPoints || frameHeight !== specifiedHeightInPoints) {
strict inequality?
But I'm not JS guy so you'll have to wait for someone else to fix it.
Copy link to clipboard
Copied
And So Iam a not a Js
Thanks for your reply
Copy link to clipboard
Copied
> remove all digits from the document except for the text box according to its size
What does this mean? What is a 'text box according to its size'?
Copy link to clipboard
Copied
I want to remove all digits from all text boxes in document Except the digits in text boxes that have certian Width and Height
Copy link to clipboard
Copied
You'll have to round those numbers a bit because of rounding errors in InDesign and because your multiplier isn#t precise enough (probably).
Do this experiment: place a text frame 50mm wide. Set your InDesign to points. A script reports the frame's width as follows:
box = app.selection[0];
alert (box.geometricBounds[3] - box.geometricBounds[1]); // >= 141.732283464567
But when you do 50*2.83464567 the result is 141.7322835, which is not the same, so you never match the boxes you want to exclude.
Try setting the document's units to millimeters, then you needn't convert any values.
Copy link to clipboard
Copied
I already set my document units to millimeters
But No change in the script
Can you fix this and post whole correct script please ?
Copy link to clipboard
Copied
I already set my document units to millimeters
But No change in the script
Can you fix this and post whole correct script please ?
By @r28071715i111
Then you can delete this part "* 2.83464567" - in both places - and it should work.
Copy link to clipboard
Copied
No change occur
Nothing happened
Copy link to clipboard
Copied
Any Help Here
Copy link to clipboard
Copied
It's a pretty horrible script by any standard, but it works:
#targetengine xyz;
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
var dialog = new Window("palette", "Excluded Text Box Size");
dialog.orientation = "column";
dialog.alignChildren = "left";
var widthGroup = dialog.add("group");
widthGroup.add("statictext", undefined, "Width (mm):");
var widthInput = widthGroup.add("edittext", undefined, "50");
widthInput.characters = 10;
var heightGroup = dialog.add("group");
heightGroup.add("statictext", undefined, "Height (mm):");
var heightInput = heightGroup.add("edittext", undefined, "25");
heightInput.characters = 10;
var buttonGroup = dialog.add("group");
var okButton = buttonGroup.add("button", undefined, "OK");
okButton.onClick = function() {
var specifiedWidthInMm = Number(widthInput.text).toPrecision(3); // Convert input to number
var specifiedHeightInMm = Number(heightInput.text).toPrecision(3); // Convert input to number
dialog.close();
removeDigitsExceptSpecifiedSize(specifiedWidthInMm, specifiedHeightInMm);
};
var cancelButton = buttonGroup.add("button", undefined, "Cancel");
cancelButton.onClick = function() {
dialog.close();
};
dialog.show();
// Remove Digits Function
function removeDigitsExceptSpecifiedSize(specifiedWidthInMm, specifiedHeightInMm) {
var doc = app.activeDocument;
var textFrames = doc.textFrames;
for (var i = 0; i < textFrames.length; i++) {
var textFrame = textFrames[i];
var frameBounds = textFrame.geometricBounds;
var frameWidth = (frameBounds[3] - frameBounds[1]).toPrecision(3);
var frameHeight = (frameBounds[2] - frameBounds[0]).toPrecision(3);
//var specifiedWidthInPoints = specifiedWidthInMm * 2.83464567; // Convert mm to points
//var specifiedHeightInPoints = specifiedHeightInMm * 2.83464567; // Convert mm to points
if (frameWidth !== specifiedWidthInMm && frameHeight !== specifiedHeightInMm) {
var textContent = textFrame.parentStory.contents;
var textWithoutDigits = textContent.replace(/\d/g, '');
textFrame.parentStory.contents = textWithoutDigits;
}
}
}
Copy link to clipboard
Copied
Great Thanks For you Peter
Script works like charm
Thaaaanks
Copy link to clipboard
Copied
@r28071715i111, but you need to remember about one thing - this script will mess up your formatting - if you have any there - Char/Para Styles, local formatting - so you need to be careful before you use it on a larger scale.
Unless, of course, you have a very simple contents there.
Copy link to clipboard
Copied
Thanks for your reply
My file is simple , doesn't contain any paragraph syle or char styles
Thanks for your advise dear