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

Scripting Flattersatz

Community Beginner ,
Jun 13, 2023 Jun 13, 2023

Hey there,

 

I saw a script for creating rythmic left-aligned text frames.  The thing is that the script is only working for InDesign 2021 but not 2023. Is there anyone who can maybe help me to get the script back to work?

 

Script:

 

#target "InDesign";

(function () {
  var layerName = "R_R_Guides";

  if (app.documents.length === 0) {
    alert("Bitte öffnen Sie zuerst ein Dokument.");
    return;
  }

  var doc = app.activeDocument;

  init();

  var len = app.selection.length;
  if (len === 0) {
    alert("Bitte wählen Sie zuerst einen Textrahmen aus.");
    return;
  }

  var flatterzone = 15; // %
  var randomness = 0; // %

  var w = new Window("dialog", "Einstellungen");
  w.orientation = "row";
  w.alignChildren = "top";

  var main_group = w.add("group");
  main_group.orientation = "row";

  var settings_group = main_group.add("group");
  settings_group.orientation = "column";

  var flatterzone_group = settings_group.add("group");
  flatterzone_group.orientation = "row";
  flatterzone_group.alignment = "right";
  flatterzone_group.add("statictext", undefined, "Flatterzone:");
  var e = flatterzone_group.add("statictext", undefined, flatterzone + "%");
  e.characters = 4;
  var slider = flatterzone_group.add("slider", undefined, flatterzone, 0, 100);
  slider.onChanging = function () {
    e.text = slider.value + "%";
    flatterzone = parseInt(slider.value);
  };

  var randomness_group = settings_group.add("group");
  randomness_group.orientation = "row";
  randomness_group.alignment = "right";
  randomness_group.add("statictext", undefined, "Zufälligkeit:");
  var e2 = randomness_group.add("statictext", undefined, randomness + "%");
  e2.characters = 4;
  var slider2 = randomness_group.add("slider", undefined, randomness, 0, 100);
  slider2.onChanging = function () {
    e2.text = slider2.value + "%";
    randomness = parseInt(slider2.value);
  };

  var btn_group = main_group.add("group");
  btn_group.orientation = "column";
  btn_group.add("button", undefined, "OK");
  btn_group.add("button", undefined, "Abbrechen");

  if (w.show() == 1) {
    // OK
  } else {
    // Abbrechen
    return;
  }

  flatterzone *= 0.01;
  randomness *= 0.01;

  var myLayer = doc.layers.itemByName(layerName);
  if (!myLayer.isValid) {
    var myLayer = doc.layers.add({ name: layerName });
  }

  for (var n = 0; n < len; n++) {
    var sel = app.selection[n];

    if (!(sel instanceof TextFrame)) {
      alert("Die Auswahl ist kein Textrahmen. Ignoriere es.");
      continue;
    }

    var x = sel.geometricBounds[3];
    var y = sel.geometricBounds[0];
    var width = x - sel.geometricBounds[1];

    var cmToPointsRatio = 0.0352778;
    var pointsize = sel.insertionPoints[0].pointSize * cmToPointsRatio;
    var leading = sel.insertionPoints[0].leading * cmToPointsRatio;

    if (leading === Leading.AUTO_VALUE) {
      leading = pointsize * 1.2;
   

 

Would be very helpful!!

 

Best regards,

Severin

TOPICS
Scripting , Type
922
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 2 Correct answers

Community Expert , Jun 13, 2023 Jun 13, 2023

At the end of the script, add a closing brace } to match the opening brace after AUTO_VALUE)

Translate
Community Expert , Jun 13, 2023 Jun 13, 2023

Ok. Was macht die helpers.js-Datei?

Hast Du die noch? Die braucht es auch für InDesign 2023!

 

Siehe ganz am Anfang im Code:

 

 

 

#include "../_common/helpers.js";

 

 

 

Die #include-Zeile verweist auf das Verzeichnis _common, das oberhalb des Verzeichnisses liegt, das Deine Skriptdatei enthält. Im Verzeichnis _common muss die helpers.js-Datei liegen, mit zusätzlichem Code, der in den Hauptcode Deiner Skriptdatei per #include eingebracht wird.

 

Beispiel; Du hast Dein Skript im Benutzerverzeich

...
Translate
Community Expert ,
Jun 13, 2023 Jun 13, 2023

Hallo Severin,

was genau funktioniert denn nicht im InDesign 2023? *

Bist Du auf macOS oder Windows unterwegs?

 

* Ist es vielleicht ein Bug mit ScriptUI ?

( Blanke Vermutung. )

 

Gruß,

Uwe Laubender
( Adobe Community Expert )

 

 

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 Beginner ,
Jun 13, 2023 Jun 13, 2023

Lieber Uwe,

 

Vielen Dank für deine Antwort!

 

Anbei sende ich dir einen Screenshot, der mir eine Fehlermeldung anzeigt. Habe etliche versuche unternommen, das Skript mit Hilfe von ChatGTP zu verbessern. Ich kenne mich im Bereich Coding leider wirklich nur wenig aus.

 

 

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 ,
Jun 13, 2023 Jun 13, 2023

At the end of the script, add a closing brace } to match the opening brace after AUTO_VALUE)

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 Beginner ,
Jun 13, 2023 Jun 13, 2023

Yes, I tried this as well but this doesn't work either. Then a next error occurs:


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 ,
Jun 13, 2023 Jun 13, 2023

Same problem. To match the opening brace after for (. . ., add another closing brace at the end of the script.

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 Beginner ,
Jun 13, 2023 Jun 13, 2023

Still doesn't work. I trouble-shot these kind of things already but there's always a new error. I assume someone needed to check the whole code quickly.

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 ,
Jun 13, 2023 Jun 13, 2023

Hallo Severin,

da fehlt noch etliches am Code, den Du bereits gepostet hast.

Zum Beispiel die Funktion init(), die ziemlich bald im Skript aufgerufen wird.

Das hat erstmal gar nichts mit ScriptUI zu tun.

 

Poste also bitte den vollständigen Code, der in InDesign 2021 noch funktioniert.

 

Mach' bitte kein "Moving Target" daraus. Will sagen: Poste den Code in einer neuen Antwort und verändere den Eingangspost von Dir nicht; falls das überhaupt möglich ist. Und lass bitte erst mal chatGPT aus dem Spiel. Das geht eher nach hinten los.

 

Danke!

 

Gruß,

Uwe Laubender
( Adobe Community Expert )

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 Beginner ,
Jun 13, 2023 Jun 13, 2023

Der Code, welcher noch eine Version älter ist (2019 glaube ich), lautet wie folgt (ich kann den Anfangs-Post nicht mehr editieren):

 

#strict on
#target "InDesign";
#include "../_common/helpers.js";

(function() {
	function init() {
		store_measurement_units(doc);
		redraw_off();
	}
	function done() {
		redraw_on();
		restore_measurement_units(doc);
	}

	if (app.documents.length === 0) {
		alert("open a document first");
		return;
	}

	var script_name = remove_file_extension( get_filename($.fileName) );
	var doc = app.activeDocument;

	init();

	var len = app.selection.length;
	if (len === 0) {
		alert("select a text frame first");
		return;
	}
	
	var flatterzone = 15; // %
	var randomness = 0; // %
	var w = new Window("dialog", "settings");
		w.orientation = "row";
		w.alignChildren = "top";

		var main_group = w.add("group");
		main_group.orientation = "row";

			var settings_group = main_group.add("group");
			settings_group.orientation = "column";

				var flatterzone_group = settings_group.add("group");
				flatterzone_group.orientation = "row";
				flatterzone_group.alignment = "right";
					flatterzone_group.add("statictext", undefined, "flatterzone:");
					var e = flatterzone_group.add("statictext", undefined, flatterzone+"%");
					e.characters = 4;
					var slider = flatterzone_group.add("slider", undefined, flatterzone, 0, 100);
					slider.onChanging = function() {
						e.text = slider.value+"%";
						flatterzone = parseInt(slider.value);
					}

				var randomness_group = settings_group.add("group");
				randomness_group.orientation = "row";
				randomness_group.alignment = "right";
					randomness_group.add("statictext", undefined, "randomness:");
					var e2 = randomness_group.add("statictext", undefined, randomness+"%");
					e2.characters = 4;
					var slider2 = randomness_group.add("slider", undefined, randomness, 0, 100);
					slider2.onChanging = function() {
						e2.text = slider2.value+"%";
						randomness = parseInt(slider2.value);
					}

			var btn_group = main_group.add("group");
			btn_group.orientation = "column";
				btn_group.add("button", undefined, "OK");
				btn_group.add("button", undefined, "Cancel");

	if (w.show() == 1) {
		// ok
	} else {
		// cancel
		return;
	}
	
	flatterzone *= 0.01;
	randomness *= 0.01;
	
	create_layer(doc, script_name);

	for (var n = 0; i < len; n++) {
		var sel = app.selection[n];

		if (sel.constructor.name != "TextFrame") {
			alert("selection is not a text frame → ignoring it");
			continue;
		}
		
		var x = sel.geometricBounds[3];
		var y = sel.geometricBounds[0];
		var width = x - sel.geometricBounds[1];

		var pointsize = sel.insertionPoints[0].pointSize;
		var leading = sel.insertionPoints[0].leading;
		if (leading == Leading.AUTO) {
			leading = pointsize * 1.2;
		}
		
		for (var i = 0; i < sel.lines.length; i++) {
			var line_y = y + (0.5 * pointsize) + (i * leading);
			var flatter_width = (width * flatterzone);
			var variance;
			var line = sel.parentPage.graphicLines.add({layer: script_name});
			if (i % 2 == 0) {
				variance = randomness * flatter_width * Math.random();
				line.geometricBounds = [line_y, x, line_y, x - variance];
			} else {
				variance = randomness * flatter_width * ((Math.random() - 0.5) * 2);
				line.geometricBounds = [line_y, x, line_y, x - flatter_width + variance];
			}
	
			line.textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;
			line.strokeColor = doc.swatches.itemByName("Paper");
			line.transparencySettings.blendingSettings.opacity = 0;
		}
	}

	done();
})();

 

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 ,
Jun 13, 2023 Jun 13, 2023

Ok. Was macht die helpers.js-Datei?

Hast Du die noch? Die braucht es auch für InDesign 2023!

 

Siehe ganz am Anfang im Code:

 

 

 

#include "../_common/helpers.js";

 

 

 

Die #include-Zeile verweist auf das Verzeichnis _common, das oberhalb des Verzeichnisses liegt, das Deine Skriptdatei enthält. Im Verzeichnis _common muss die helpers.js-Datei liegen, mit zusätzlichem Code, der in den Hauptcode Deiner Skriptdatei per #include eingebracht wird.

 

Beispiel; Du hast Dein Skript im Benutzerverzeichnis Deines Skriptebedienfeldes liegen, dann muss die Ordner/Dateistruktur so aussehen:

 

_common/helpers.js

Scripts Panel/Skriptdatei.jsx

 

Die helpers.js-Datei enthält also notwendigen Code, um die Skriptdatei.jsx korrekt ausführen zu können.

Die beiden Punkte in "../_common/helpers.js" heißen nichts anderes als: steige eine Verzeichnisebene höher (wie damals im alten DOS-Betriebssystem, falls sich noch jemand erinern will). Ok. Dann wieder runter in das _common-Verzeichnis und füge den Code ein, der dort in der helpers.js-Datei ist.*

 

Siehe auch die Dokumentation von #include auf Seite 337 hier:

https://javascript-tools-guide.readthedocs.io/_/downloads/en/latest/pdf/

Und Seite 56 im Abschnitt 15.1.1 Absolute and relative path names im gleichen PDF.

 

Noch wichtig zu wissen: Falls Du den Code mit dem ExtendScript Debugger für CS-Code VS-Code testen möchtest, musst Du anstatt des #-Zeichens für den include das @ -Zeichen benutzen.

Diesen Fall hatten wir hier neulich im Forum.

 

* Sollte allerdings die helpers.js-Datei weitere #include - Anweisungen haben, fehlen Dir allerdings noch weitere Code-Schnipsel in weiteren Zusatzdateien. Ich hoffe mal, das ist nicht der Fall.

 

Gruß,

Uwe Laubender
( Adobe Community Expert )

 

EDITED 2x

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
New Here ,
Dec 24, 2024 Dec 24, 2024
LATEST

Hello! Can I get this script?

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