Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

How to fit a paragraph into a text frame by scripting

Return to Forums

  • Author
    Posts
    • #65632
      Anonymous
      Inactive

      I’m trying to script the creation of text frames with some text inside so that the text frame automatically expands to the necessary height. This works, but not the same way as with the UI. What I want to achieve is that the paragraph in the text frame has a margin of 15mm on all sides. But this works only for the left and right indent, not for the spacing before and after the paragraph.
      When you run the script below, you’ll see that the text frame has exactly the height of the paragraph within. But when you now open the “Object Styles” dialogue, click on “Text Frame General Options” and on “Preview”, you’ll see that the “Inset Spacing” presets are applied, while they are not applied during the script execution.
      So the question is: How can I by scripting create a text frame with inner margins on all sides and fit the text frame size including the margins to the content?

      <script>

      // close all your unsaved documents before you run this script
      for (var i = 0; i < app.documents.length; i++) {
      app.documents[i].close(SaveOptions.NO);
      }

      var myDocument = app.documents.add(true);
      var currentPage = myDocument.spreads.lastItem().pages.lastItem();

      myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
      myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

      myColorWHITE = myDocument.colors.add({name:”WHITE”,model:ColorModel.process,colorValue:[0,0,0,0]});

      paragraphStyleBox = myDocument.paragraphStyles.add({name:”box”});
      with (paragraphStyleBox) {
      spaceAfter = “15mm”;
      spaceBefore= “15mm”;
      leftIndent = “15mm”;
      rightIndent = “15mm”;
      }

      var objectStyleBox = myDocument.objectStyles.add({name:”Box”});
      with (objectStyleBox) {
      appliedParagraphStyle = paragraphStyleBox;
      fillTransparencySettings.blendingSettings.opacity = 80;
      fillColor = myColorWHITE;
      textFramePreferences.insetSpacing = [“15mm”,”15mm”,”15mm”,”15mm”];
      }

      var myObject = currentPage.textFrames.add();
      myObject.applyObjectStyle(objectStyleBox);
      myObject.geometricBounds = [“25mm”,”25mm”,”50mm”,”120mm”];

      myObject.contents = “Milicae is. Ortemnem mium host poteatius lis. At perentilinc tum non diem et quons interet; nonfex milicta demque num. Eci publiae tes re porestem que movena, nos culessi etilissidiem utem. Publis. Fultorum mo vid con verdientime cervivis cri, sena, publictam quit, ur, quemena tamquist grac ret; habena, siderfe conlocr evitio adhui cortuidemeis consum andam aciam ma, fuis ore aucterei ius manum pubis. Fuliceristam octu vignonsulium cum sent, us C. Mus entebatque et videt; horeo, conem avoltor enatus. Scierox mus ommovero, quam inatropotius occi prae et; nos ad me prore cotam teribut audeatquam in demus, abus bonsidem cus sentem dum mo et qua publis? Forit intum inu vis, etem mei senatilis iam eoribut octum idi, sendam aus elut viventeat. Opiorbissi inatur aci fautem ommorac tatuus crem ia? Hosulium perbemurbit.”
      + “Il tatum abem sulius pubis perfir la re ius vivir unirmis, popos, oribuntim hocchi, quam veresultuam invo, que facien hocciendet videt? Ficae ad pror quem iam nu virid imus abus num nihil hocupplintem acchicivis, senditua quam etore queremur aderibus iaet? Ximodit rudemus addum nononsus et; horementi, se con Etra, que atum ta, se teniris essoltustia erum tem fuem defatus clem peris horicaedeesi ina, utum facientem atu quodiora te in Itam qua re me ori, nes conihi, supplica publicae conc or aurbist abefex satam es nihi, pratienatum auciste beffren deriti serrionficia et vigit quitiac turnihil vidium foraequam in pori peciviu es! Ximus bonveraes imoena, mus niquid popubis ad nonsu modius, cus iam con in tea occidici ipieris ulicute nonclud emuressi ina, crem, neris,”;

      myObject.paragraphs[0].applyParagraphStyle(paragraphStyleBox,true);
      myObject.fit(FitOptions.FRAME_TO_CONTENT);

      </script>

    • #65639
      David Blatner
      Keymaster

      I’m not a scripter, so I’ll leave that question for someone else. But you know that InDesign CS6 and CC have Auto-Fit for text frames now, right? In the Text Frame Options dialog box.

    • #65641
      Anonymous
      Inactive

      Thanks for the hint. I should have mentioned that I work with InDesign CS5 and see no reason to upgrade (except maybe for this feature).

    • #65642
      Anonymous
      Inactive
Viewing 3 reply threads
  • You must be logged in to reply to this topic.
Forum Ads