9 Replies Latest reply on Oct 26, 2015 5:28 AM by Dick Kain

    How do I set alignment in a text frame from within a script?

    Dick Kain

      I have tried what I think are all options to get centered text in a text frame created within a script. What I'm trying to do is create a text frame with known dimensions and known contents that should be centered and the frame should be on the same page of the document as a selected image frame. In other words, I'm trying to put a centered caption below an image.I get so close -- I can get the correct frame size and position on the page, but it is located on the previous page! And whatever I do, the contents are always left-aligned. [I tried assigning a style with centering, and I tried assigning Justification,centerAlign to the justification attribute of the contents of the text frame. But the text is always on the left end - I can get it centered only by going in with the text tool and then choosing centering from the options above the page display.] I tried moving the frame by one page, but I cannot figure that one out (in particular, how to tell it to move the frame by exactly one page). The examples in the CS6 scripting document show examples (page 114) that do not work - I get a complaint about having "undefined" as the first parameter of move - that is explicitly pointed out in the scripting document! Does anyone have any suggestions about this? I spared you the coding attempts that I've made, but I could send them to you if that would help.

       

      Thanks in advance,,,,,

        • 1. Re: How do I set alignment in a text frame from within a script?
          Peter Kahrel Adobe Community Professional & MVP

          Do you explicitly place that caption on the same page as the image? Try this:

           

          app.selection[0].parentPage.textFrames.add();

           

          For the caption you'd best create an object style, and apply that style to the caption after you created it. Create a paragraph style for us in the object style and set the paragraph's alignment to centre.

           

          Peter

          • 2. Re: How do I set alignment in a text frame from within a script?
            Vamitul Level 4

            Do show your code! At the very least it shows you made an honest attempt at solving the problem (unlike a lot of other posters on this forum who just demand custom-made scripts).

            Now, for your problem, let's assume you already have the image frame selected.

            var doc=app.activeDocument;
            var imageFrame=doc.selection[0];
            var gbs=imageFrame.geometricBounds;
            //the page where the image resides:
            var pag=imageFrame.parentPage;
            //add the new textframe
            var captionFrame=pag.textFrames.add();
            //It sometimes happens with facig pages and some other weird setups
            //that the textframe is actually created outside of the page.
            //So.. this should fix those instances.
            if (captionFrame.parentPage!==pag){
              captionFrame.move(pag);
            }
            //Compute the bounds of the new text frame:
            var newGbs=[];
            //New Y0 is the same as Y2 of the the image frame
            newGbs[0]=gbs[2];
            //The X0 and X1 coords are the same so the width of the caption is the same as the image frame
            newGbs[1]=gbs[1];
            //The Y2 of the frame, for now:
            newGbs[2]=gbs[2]+25;
            //The X0 and X1 coords are the same so the width of the caption is the same as the image frame
            newGbs[3]=gbs[3];
            captionFrame.geometricBounds=newGbs;
            //make the frame "smart":
            captionFrame.textFramePreferences.autoSizingReferencePoint=AutoSizingReferenceEnum.TOP_LEFT_POINT;
            captionFrame.textFramePreferences.autoSizingType=AutoSizingTypeEnum.HEIGHT_ONLY;
            
            
            //Adding the text:
            captionFrame.contents='Call me Ishmael. '+
            'Some years ago—never mind how long precisely—having '+
            'little or no money in my purse, and nothing particular to interest me on shore, '+
            'I thought I would sail about a little and see the watery part of the world.';
            
            
            //Format it:
            captionFrame.parentStory.justification=Justification.CENTER_ALIGN;
            //or apply a paragraph style:
            //captionFrame.parentStory.appliedParagraphStyle=doc.paragraphStyles.item('MyParaName');
            
            
            //triger a recompose to make sure autofit does it's job:
            captionFrame.recompose();
            
            • 3. Re: How do I set alignment in a text frame from within a script?
              Dick Kain Level 1

              I wanted to start with a quick reply, because I appreciate what you sent, but I haven't had time today to work with it yet. Here's an example of what I was writing that didn't work:

               

              myTextFrame = myRectangle.parent.textFrames.add({geometricBounds:[myY1, myX1, myY2, myX2], contents:myCaption });

               

              The rectangle was the frame holding the image that needed a caption. Adding in a line for styles:

               

              myTextFrame.appliedObjectStyle = "Labels on Black"

               

              That style specifies centered positioning, but executing this had no effect - the text was still on the left. I had also tried placing that style specification inside the add parameter list, but that didn't make any difference. Then I tried specifying the text justification, as in

               

              myTextFrame.contents.justification = Justification.rightAlign

               

              I was simply trying out other options to see what worked. This also did nothing - still locked on the left.

               

              When I saw your script, I realized that you know about things that I never could find:

              1.  activeDocument

              2. parentPage -- you can see from my first line of code above that I was trying to use the parent of the frame in the role of the active page, which doesn't work.

              3. a page destination in move, as in your captionFrame.move(pag)

               

              My references have been several scripting documents and the large on-line document about the data types, etc. in ID CS5. From those I saw conflicting versions of things such as the justification specifications - (1) CENTER_ALIGN, and (2) centerAlign. It was also frustrating that apparently the document(s) that contain the complete examples for the scripting guides are no longer on the WEB.

               

              I also have the problem that my script editor lost its find capability many moons ago, and I've been unable to resurrect that important tool. I have "4.0.0.1 ExtendScript 4.5.5 ScriptUI 6.2.2" - is there a later version somewhere? Mine has the last copyright date as 2012.

               

              I'll check out your suggestions when I get a chance, but perhaps that's tomorrow night, after the birthday party for our great-grandson, who will be 2 on Wednesday! The suggestions do look like they'll get me on track towards my goal.

               

              Thanks.

              • 4. Re: How do I set alignment in a text frame from within a script?
                Peter Kahrel Adobe Community Professional & MVP

                > myTextFrame.appliedObjectStyle = "Labels on Black"

                 

                Use this:

                 

                app.selection[0].appliedObjectStyle = app.activeDocument.objectStyles.item ("Labels on Black")

                 

                It's safer always to use objects rather than strings when you assign a style (or any other object), because in some cases the string won't work where the object reference will. There are inconsistencies within but also across OSs: in some cases using a string works on Windows but not on the Mac.

                 

                parentPage: 'parent' sometimes returns a page, but not always. E.g. if a page item is inside a group, 'parent' returs the group. 'parentPage' always returns the page that the item sits on no matter whether its in a footnote, a cell, a group, etc.

                 

                CENTER_ALIGN and centerAlign are conflicting only in form, not in functionality: they are equivalent.

                 

                You have the latest version of the ESTK. Not sure where Find went. Did you try uninstalling and reinstalling?

                • 5. Re: How do I set alignment in a text frame from within a script?
                  Vamitul Level 4

                  To pile up on Peter's answer:
                  If you use object styles, they might not override the text formatting. Use myFrame.clearObjectStyleOverrides()

                   

                  "myTextFrame.contents.justification = Justification.rightAlign"

                  The "contents" property of a textframe (or any text-like object) is just a plain javascript string, and those have no justification. You are interested in the Text object, that represents a formatted indesign text. To get to that you have different routes, depending on the desired result:

                  • myFrame.parentStory will target the entire story (remember that a story can be contained in multiple threaded text frames or it can be overset, with just a portion of it showing in the frame)
                  • myFrame.paragraphs[0] will target only the first paragraph of the frame
                  • myFrame.texts[0] will target the whole text inside the frame (not the overset one or the one in linked frames)
                  • etc etc.


                  centerAlign vs CENTER_ALIGN: You've been looking at Dave Saunder's blog, haven't you? Anyway, all enumerations used by ExtendScript support both the UPPERCASE and camelCase form as (i guess) a matter of convenience. You can even use the numeric value of them if so inclined (for CENTER_ALIGN that would be 1667591796) since that's all a enumeration is - a wrapper to a numeric value. Personally i suggest using the UPPERCASE form since it makes it clear in the code that you are dealing with a constant enum, and not some obscure property of some object.

                   

                  Find/change ability: I'm scared to ask, but have you tried Ctrl/Cmd+F? Anyway, ESTK is a horrible editor. Use SublimeText to develop your scripts and only switch to ESTK for testing/debugging.

                   

                  For documentation, the ESTK-embedded "Object Model Viewer" is an abomination! I usually use InDesign ExtendScript API (11.0)

                   

                  Enjoy the party, enjoy the cake (mmmm... cake)

                  • 7. Re: How do I set alignment in a text frame from within a script?
                    Dick Kain Level 1

                    Sorry for the blank message... I was checking out a couple of things before composing a complete response. The simple stuff is about ESTK -- yes, I always use ctrl-f for find, that didn't work. Also, the find from the edit menu did nothing - both just returned me to where I was in the editing process. I think I did reinstall it with no effect. I'm not familiar with the editor that you mentioned. I'll look it up before doing more scripting.

                     

                    In general, thanks much for the script, which mostly worked as a solution to my problem. However, some notes:

                     

                    1. The text alignment problem is more complex, because while the paragraph style has centered justification within it, that doesn't work. The centerAlign also didn't work, but CENTER_ALIGN did - both in separate statements:

                     

                    myTextFrame.parentStory.justification = Justification.CENTER_ALIGN

                    myTextFrame.parentStory.justification = Justification.centerAlign

                     

                    ID group error.jpg2. Grouping (see my previous query on another thread) doesn't work. The error message is in the screen shot above. The three objects are the image, its enclosing rectangle and the text frame, which holds the caption. Without sending out to the public everything that I did, here are the lines that set those things up:

                     

                           myImage = app.selection[0]

                           myRectangle = app.selection[0].parent

                           myTextFrame = pag.textFrames.add({geometricBounds:[myY1, myX1, myY2, myX2], contents:myCaption });

                     

                    Any ideas? - In general I get easily confused about the levels of groups, etc, in data structures, but here there's something in the response about layers, which I thought I did't ever deal with in this script.

                     

                    I also tried putting the three references into a single parameter of the add function for the groups, as in

                     

                    var myGroup = app.activeDocument.groups.add([myImage, myRectangle, myTextFrame])

                     

                    but that doesn't work, either.

                     

                    Thanks, in advance, for your thoughts.

                     

                    Cake was good! Do you like chocolate cake? If so, try my improvement, which I freely share with people. For over 25 years I have made chocolate cakes (And my wife does this also) with a good cake mix, following their directions, but replacing 3/4 of the water with Kahlua. The alcohol bakes out, and people think that you made the cake from "scratch."

                    • 8. Re: How do I set alignment in a text frame from within a script?
                      Vamitul Level 4

                      var myGroup = app.activeDocument.groups.add([myImage, myRectangle, myTextFrame])

                      is somewhat correct. In the sense that groups.add requires an array of page items.

                      What is wrong with it is that the image is a child of the rectangle.

                      Try:

                      var myGroup = app.activeDocument.groups.add([myRectangle, myTextFrame])


                      • 9. Re: How do I set alignment in a text frame from within a script?
                        Dick Kain Level 1

                        Thanks, but that didn't do it. It didn't complain, but then it didn't link the two together. When I moved one of them, the other stayed in place. However, if I selected both afterwards and typed ctrl-g, they were indeed grouped and moved together. A mystery!

                         

                        I think I recall seeing somewhere that there was a way to invoke a menu command from the script. Perhaps that would solve this. I don't remember the invocation of that. I think I recall something starting with "ID....."