Skip to main content
victorcarbajo
Inspiring
March 21, 2025
Question

Overlapping text

  • March 21, 2025
  • 0 replies
  • 158 views

A script for Illustrator that selects text objects containing a single character that are in exactly the same position on the page, one on top of the other, overlapping.

function selectOverlappingTextObjects() {
	var doc = app.activeDocument;
	var textFrames = doc.textFrames;
	var selectedItems = [];
	for (var i = 0; i < textFrames.length; i++) {
		var tf1 = textFrames[i];
		if (tf1.contents.length === 1) {
			var tf1Bounds = tf1.geometricBounds; 
			var tf1Position = {
				x: (tf1Bounds[0] + tf1Bounds[2]) / 2,
				y: (tf1Bounds[1] + tf1Bounds[3]) / 2
			};
			for (var j = i + 1; j < textFrames.length; j++) {
				var tf2 = textFrames[j];
				if (tf2.contents.length === 1) {
					var tf2Bounds = tf2.geometricBounds;
					var tf2Position = {
						x: (tf2Bounds[0] + tf2Bounds[2]) / 2,
						y: (tf2Bounds[1] + tf2Bounds[3]) / 2
					};
					if (Math.abs(tf1Position.x - tf2Position.x) < 1 && Math.abs(tf1Position.y - tf2Position.y) < 1) {
						tf1.selected = true;
						tf2.selected = true;
						selectedItems.push(tf1, tf2);
					}
				}
			}
		}
	}
	return selectedItems;
}
selectOverlappingTextObjects();

This script works, but it selects both text objects. The idea is to leave only one, so it should select only one of each pair.