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

TextLoc works once, then stops working

New Here ,
Feb 07, 2014 Feb 07, 2014

Copy link to clipboard

Copied

I have this simple code:

 

var doc = app.ActiveDoc;

var t1 = new TextLoc();

var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

t1.obj = firstPgf;

doc.AddText (t1, "Hello");

When I ran it the first time from the ES toolkit code window, it printed "Hello" on the first line of the active Fm document. But when I ran the code again a moment later on the same doc, nothing happens. There are no visible errors and it prints "Result: [object TextLoc]" in the JavaScript console.

I can't get it to run again. This happened a few days ago and I gave up. Then just now I decided to try it again and it did the same thing - printed "Hello" once in the Fm doc, then it would not do it again. I've tried restarting Fm and the toolkit.

 

Any ideas what is causing this?

 

Thanks,

Mark

TOPICS
Scripting

Views

736

Translate

Translate

Report

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 1 Correct answer

Community Expert , Feb 07, 2014 Feb 07, 2014

Hi Mark, Apparently there is a problem with the order of the commands. If you try it this way, it appears to work:

var doc = app.ActiveDoc;

var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

var t1 = new TextLoc(firstPgf,0);

doc.AddText (t1, "Hello");

Rick

Votes

Translate

Translate
Community Expert ,
Feb 07, 2014 Feb 07, 2014

Copy link to clipboard

Copied

Hi Mark, Apparently there is a problem with the order of the commands. If you try it this way, it appears to work:

var doc = app.ActiveDoc;

var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

var t1 = new TextLoc(firstPgf,0);

doc.AddText (t1, "Hello");

Rick

Votes

Translate

Translate

Report

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 ,
Feb 07, 2014 Feb 07, 2014

Copy link to clipboard

Copied

Yay! That works. Thanks Rick.

Votes

Translate

Translate

Report

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
Advocate ,
Feb 08, 2014 Feb 08, 2014

Copy link to clipboard

Copied

Hi Mark (and Rick),

I want to correct something in your understanding of the TextLoc object.

The order of commands does not have anything to do with the problem that the code only ran once after restart and then stopped. The issue is that the TextLoc contains a pointer to the object and an offset, and both must be initialized. If you add one line to the code, as shown below, the script works fine every time.

var doc = app.ActiveDoc;

var t1 = new TextLoc();

var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

t1.obj = firstPgf;

t1.offset = 0;

doc.AddText (t1, "Hello");

If you omit setting the offset, it will accidentally have the value 0 when it is first used, but it gets changed to something undefined when adding text - there is no way to tell what FM makes of it. If the value is outside of the range of the object you are pointing to, FM will simply refuse to add the text.

The reason why Rick's code worked is that he used another method of creating the TextLoc object, which takes the object and an offset as reference explicitly. That of course works anytime.

Kind regards

Jang

Votes

Translate

Translate

Report

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 ,
Feb 08, 2014 Feb 08, 2014

Copy link to clipboard

Copied

Great points Jang. Thanks for clarifying. --Rick

Votes

Translate

Translate

Report

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 ,
Feb 10, 2014 Feb 10, 2014

Copy link to clipboard

Copied

Ah! That's nice. Thanks Jang, you are making me smarter.  Mark

Votes

Translate

Translate

Report

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 ,
Feb 10, 2014 Feb 10, 2014

Copy link to clipboard

Copied

LATEST

By the way, I realized where my original flawed code came from: The FrameMaker 10 Scripting Guide, p.6. It gives this sample:

var doc = app.ActiveDoc;

var tl = new TextLoc();

var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

tl.obj = firstPgf;

doc.AddText (tl, "Hello");

doc.AutoChangeBars =1;

FrameMaker ExtendScript tech writers please take note that this sample needs t1.offset = 0; added or it only works one time.

Votes

Translate

Translate

Report

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