4 Replies Latest reply on Jul 24, 2013 8:31 AM by Jim Birkenseer

    ScriptUI Multiline StaticText Frame Too Large

    Jim Birkenseer Level 1

      Hi Folks,

       

      Has anyone here had an issue (and, hopefully come up with a workaround!) where multiline static text frames that are defined with a non-default width become taller than they should be? This seems to happen in all versions of InDesign. FWIW, this problem does not seem happen in Illustrator (using the same code).

       

      The longer the static text, the more unnecessary white space appears at the bottom of the text frame--and the taller the window becomes. To me, it seems like the height of the frame is being calculated for a narrower frame width than what the script defines.

       

      Here's sample code that displays a dialog box that shows the issue. (NOTE: The size of the static text's frame is filled with red to show what's happening). I would expect that the red area would stop at (or very near) the bottom of the static text.

       

      var MsgBoxTest = new Window('dialog',undefined,undefined);
      buildWindow();
      MsgBoxTest.show();
      
      function buildWindow(){
          MsgBoxTest.alignChildren = ['left','top'];
          MsgBoxTest.orientation = "column";
      
          // Properties for MsgBoxTest.MsgGroup
          MsgBoxTest.MsgGroup = MsgBoxTest.add('group',undefined);
          MsgBoxTest.MsgGroup.alignment = ['left','top'];
          MsgBoxTest.MsgGroup.maximumSize= [650,600];
          MsgBoxTest.MsgGroup.alignChildren = [' ','top'];
          MsgBoxTest.MsgGroup.orientation = "column";
          MsgBoxTest.MsgGroup.graphics.backgroundColor = MsgBoxTest.MsgGroup.graphics.newBrush(MsgBoxTest.MsgGroup.graphics.BrushType.SOLID_COLOR,[1,0,0],1);
      
          // Properties for MsgBoxTest.MsgGroup.StaticText1
          MsgBoxTest.MsgGroup.StaticText1 = MsgBoxTest.MsgGroup.add('statictext',undefined,"Nonserita demquat issimus ex eatiust, occatur, ut ommod expliquodi ratusam, que vendest dolorepro coreium volorempere doluptat as exceaquam quo qui\r\rBus reped mos exere od quodior porum?", {multiline:true});
          MsgBoxTest.MsgGroup.StaticText1.alignment = ['left','top'];
          MsgBoxTest.MsgGroup.StaticText1.maximumSize= [325,600];
          MsgBoxTest.MsgGroup.StaticText1.preferredSize.width = 325;
          MsgBoxTest.MsgGroup.StaticText1.preferredSize.height = -1;
      
      }
       

       

      Are there any workarounds for this issue?

       

      TIA!!!

       

      -- Jim

        • 1. Re: ScriptUI Multiline StaticText Frame Too Large
          Jim Birkenseer Level 1

          Hi Again,

           

          FWIW...

           

          In order to get around this issue, I wrote some code that counts the characters to be displayed and estimates the number of lines needed to display it in the dialog box. Using this information, the code explicitly sets the statictext frame to the estimated size. Not an ideal solution, but it seems to work for my needs.

           

          -- Jim

          • 2. Re: ScriptUI Multiline StaticText Frame Too Large
            Marc Autret Level 4

            Hi Jim,

             

            Yes, that's exactly the workaround I was about to suggest.

             

            Multiline statictexts reveal something weird in the way preferredSize is computed within the layout manager. Usually the characters property (if supplied before you hit preferredSize) will do a good job, provided that you already know how many characters a line is supposed to support:

             

            var w = new Window('dialog'),
                gp = w.add('group'),
                st = gp.add('statictext', undefined, "Nonserita demquat issimus ex eatiust, occatur, ut ommod expliquodi ratusam, que vendest dolorepro coreium volorempere doluptat as exceaquam quo qui\r\rBus reped mos exere od quodior porum?", {multiline:true});
            
            st.characters = 50; // WARNING: do not hit st.preferredSize before you set the characters property!
            
            with( gp.graphics ){ backgroundColor = newBrush(w.graphics.BrushType.SOLID_COLOR,[1,0,0],1); }
            
            w.show();
            

             

            The code above will usually work well. BUT if you need to specify a 'preferred' width instead, then things become complex because st.preferredSize=[MY_PREFERRED_WIDTH,-1] does not lead the layout manager to properly anticipate the height of the widget. In fact, I suspect the preferred size is always computed relative to an explicit or implicit characters number. So you have to provide the optimal number of characters that match the constraint. AFAIK the internal ScriptUI processing is based on the metrics of the 'X' character. So my workaround is to do as follows:

             

            var w = new Window('dialog'),
                gp = w.add('group'),
                st = gp.add('statictext', undefined, 'X', {multiline:true}),
                X_WIDTH = st.preferredSize[0], // Here I grab the X width
                PREFERRED_WIDTH = 600;     // Your preferred width, in px
            
            with( st )
                {
                // Reset the preferred size so that the
                // characters property will cause an update
                // ---
                preferredSize = [-1,-1];
                
                // Estimate the optimal character number
                // considering PREFERRED_WIDTH and X_WIDTH
                // ---
                characters = ~~(PREFERRED_WIDTH/X_WIDTH);
                
                // Will force the preferred height to be computed
                // again given the implied width and characters prop.
                // ---
                preferredSize[1] = -1;
                }
            
            // Finally, set the actual text
            // ---
            st.text = "Nonserita demquat issimus ex eatiust, occatur, ut ommod expliquodi ratusam, que vendest dolorepro coreium volorempere doluptat as exceaquam quo qui\r\rBus reped mos exere od quodior porum?";
            
            with( gp.graphics ){ backgroundColor = newBrush(w.graphics.BrushType.SOLID_COLOR,[1,0,0],1); }
            
            w.show();
            

             

            Not sure that works on all platforms though.

             

            @+

            Marc

            • 3. Re: ScriptUI Multiline StaticText Frame Too Large
              Trevorׅ Adobe Community Professional

              Hi Marc

               

              Great stuff as usual.

               

              Tested on Windows 7, CS5 and CC

               

              Trevor

              • 4. Re: ScriptUI Multiline StaticText Frame Too Large
                Jim Birkenseer Level 1

                Thanks Marc!

                 

                I briefly tested the first snippet (the one not specifying a line width), and it works fine in Mac IDCS3, IDCS4, IDCS6, and IDCC.

                 

                I tested the second snippet on the same versions and it worked fine in everything except CS3. The result in CS3 is a tiny dialog box with no text in it.

                 

                I haven't had a chance to test the snippets in other versions or on Windows yet. I'll report back if I discover anything of interest.

                 

                This is great information, and I really appreciate your help!

                 

                Cheers!

                 

                -- Jim