Skip to main content
Known Participant
October 24, 2015
Answered

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

  • October 24, 2015
  • 2 replies
  • 4626 views

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,,,,,

This topic has been closed for replies.
Correct answer Vamitul

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();

2 replies

Vamitul
VamitulCorrect answer
Legend
October 24, 2015

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();

Dick KainAuthor
Known Participant
October 24, 2015

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.

Peter Kahrel
Community Expert
Community Expert
October 25, 2015

> 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?

Peter Kahrel
Community Expert
Community Expert
October 24, 2015

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