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

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

Engaged ,
Jan 06, 2024 Jan 06, 2024

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

TOPICS
Scripting
851
Translate
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

correct answers 1 Correct answer

Community Expert , Jan 06, 2024 Jan 06, 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 
...
Translate
Community Expert ,
Jan 06, 2024 Jan 06, 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.

 

Translate
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
Engaged ,
Jan 06, 2024 Jan 06, 2024

And So Iam a not a Js

Thanks for your reply

Translate
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 ,
Jan 06, 2024 Jan 06, 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'?

Translate
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
Engaged ,
Jan 06, 2024 Jan 06, 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 

 

Translate
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 ,
Jan 06, 2024 Jan 06, 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.

 

 

Translate
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
Engaged ,
Jan 06, 2024 Jan 06, 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 ?

 

Translate
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 ,
Jan 06, 2024 Jan 06, 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. 

 

Translate
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
Engaged ,
Jan 06, 2024 Jan 06, 2024

No change occur

Nothing happened

Translate
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
Engaged ,
Jan 06, 2024 Jan 06, 2024

Any Help Here

Translate
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 ,
Jan 06, 2024 Jan 06, 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;
    }
  }
}
Translate
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
Engaged ,
Jan 06, 2024 Jan 06, 2024

Great Thanks For you Peter

Script works like charm

Thaaaanks

 

Translate
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 ,
Jan 06, 2024 Jan 06, 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.

 

Translate
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
Engaged ,
Jan 06, 2024 Jan 06, 2024
LATEST

Thanks for your reply

My file is simple , doesn't contain any paragraph syle or char styles

Thanks for your advise dear

Translate
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