Skip to main content
Known Participant
January 6, 2024
Answered

Script to remove all digits from the document except for the text box according to its size

  • January 6, 2024
  • 4 replies
  • 1244 views

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;
}
}
}

This topic has been closed for replies.
Correct answer Peter Kahrel

 

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;
    }
  }
}

4 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
January 6, 2024

 

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;
    }
  }
}
Known Participant
January 6, 2024

Great Thanks For you Peter

Script works like charm

Thaaaanks

 

Robert at ID-Tasker
Legend
January 6, 2024

@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.

 

Peter Kahrel
Community Expert
Community Expert
January 6, 2024

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.

 

 

Known Participant
January 6, 2024

I already set my document units to millimeters

But No change in the script

Can you fix this and post whole correct script please ?

 

Robert at ID-Tasker
Legend
January 6, 2024
quote

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. 

 

Peter Kahrel
Community Expert
Community Expert
January 6, 2024

 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'?

Known Participant
January 6, 2024

I want to remove all digits from all text boxes in document Except the digits in text boxes that have certian Width and Height 

 

Robert at ID-Tasker
Legend
January 6, 2024

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.

 

Known Participant
January 6, 2024

And So Iam a not a Js

Thanks for your reply