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

Script to Move Insertion Point & Focus to First Paragraph of Document

Contributor ,
Feb 26, 2024 Feb 26, 2024

Copy link to clipboard

Copied

Does anyone know a way to move the insertion point and focus to the first paragraph of a document using ExtendScript?  When one of my script ends, the insertion point and focus (page displayed) is wherever the insertion point is located in the document when it finishes.  I'm looking for code that moves the insertion point to the beginning of the first paragraph in the document and also displays the first page on the screen similar to how "Go to First Page" in the Page Status tool at bottom of FrameMaker does.  I could close and reopen the document, but I don't want to automatically save the changes until the editor has reviewed them.  I've searched, but can't seem to find anything about this on the forum. 

TOPICS
Scripting

Views

164

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 5 Correct answers

Explorer , Feb 26, 2024 Feb 26, 2024

Please try the following script.

var doc = app.ActiveDoc;
var pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
var beg = new TextLoc (pgf, 0);
var end = new TextLoc (pgf, 0);
var range = new TextRange (beg, end);

doc.CenterOnText (range);

 

Votes

Translate

Translate
Community Expert , Feb 27, 2024 Feb 27, 2024

In addition to yatani's code, you could store the current TextRange before the script starts.

var startRange = doc.TextSelection;

Then use this at the end of the script:

doc.CenterOnText (startRange);

Votes

Translate

Translate
Community Expert , Feb 27, 2024 Feb 27, 2024

I am not sure I understand exactly what you are doing. But when you add text, it requires a TextLoc, not a TextRange. Remember that a TextRange consists of two TextLoc objects: TextRange.beg and TextRange.end. So to add test at the beginning of a TextRange (assuming that textRange is your TextRange variable and doc is your Doc variable:

doc.AddText (textRange.beg, "Izzy");

Votes

Translate

Translate
Community Expert , Feb 27, 2024 Feb 27, 2024

I decide to search my script library of several thousand scripts and snippets and I haven't used CenterOnText once! There is another one that is similar that I have used:

doc.ScrollToText (doc.TextSelection);

But even that one, I only used less than 10 times! Thanks to yatani for CenterOnText.

 

Votes

Translate

Translate
Community Expert , Feb 28, 2024 Feb 28, 2024

Here are a couple of videos that explain TextRange and TextLoc objects in FrameMaker ExtendScript. If there is enough interest, I will post more videos and make a playlist.

https://youtu.be/6ywYWU4VC7g

https://youtu.be/fH5giAcDp6Q

Votes

Translate

Translate
Explorer ,
Feb 26, 2024 Feb 26, 2024

Copy link to clipboard

Copied

Please try the following script.

var doc = app.ActiveDoc;
var pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
var beg = new TextLoc (pgf, 0);
var end = new TextLoc (pgf, 0);
var range = new TextRange (beg, end);

doc.CenterOnText (range);

 

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 27, 2024 Feb 27, 2024

Copy link to clipboard

Copied

In addition to yatani's code, you could store the current TextRange before the script starts.

var startRange = doc.TextSelection;

Then use this at the end of the script:

doc.CenterOnText (startRange);

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
Contributor ,
Feb 27, 2024 Feb 27, 2024

Copy link to clipboard

Copied

Thanks so much.  I've struggled to find this solution for over two years, and turns out to be so simple.  CenterOnText was the piece I was missing. Combining both of your scripts, I can literally toggle to top of page and back to my original text selection.  One last question...how do I add text at the cursor after I've moved the cursor and focus to beginning of document or after jumping back to the startRange?  I've tried doc.AddText (range, " Izzy ") and doc.AddText (" Izzy ") after the range is defined and after doc.CenterOnText and nothing happens.

 

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 27, 2024 Feb 27, 2024

Copy link to clipboard

Copied

I am not sure I understand exactly what you are doing. But when you add text, it requires a TextLoc, not a TextRange. Remember that a TextRange consists of two TextLoc objects: TextRange.beg and TextRange.end. So to add test at the beginning of a TextRange (assuming that textRange is your TextRange variable and doc is your Doc variable:

doc.AddText (textRange.beg, "Izzy");

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 27, 2024 Feb 27, 2024

Copy link to clipboard

Copied

I decide to search my script library of several thousand scripts and snippets and I haven't used CenterOnText once! There is another one that is similar that I have used:

doc.ScrollToText (doc.TextSelection);

But even that one, I only used less than 10 times! Thanks to yatani for CenterOnText.

 

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
Contributor ,
Feb 27, 2024 Feb 27, 2024

Copy link to clipboard

Copied

Text ranges are still a bit of a mystery to me.  However, this little exchange has really helped with my understanding.  Makes sense why AddText won't work on a range of text.  As to why I need this, most of my scripts background highlight editing issues or delete & insert text in track changes.  When the scripts finish, the insertion point and focus (hope I'm using this term correctly) are on the last page the script found an issue. Some of the document are 100 pages or more and an entire book is often over 500 pages. 

 

It saves the editors time if I can move the insertion point and focus to the start of the document so they can immediatly begin locating the track changes or highlighted text.  I'm guesing you likely have a more elegant way of doing the same thing.  I can shoot you one of my scripts if you want to see what I'm talking about. 

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 28, 2024 Feb 28, 2024

Copy link to clipboard

Copied

Here are a couple of videos that explain TextRange and TextLoc objects in FrameMaker ExtendScript. If there is enough interest, I will post more videos and make a playlist.

https://youtu.be/6ywYWU4VC7g

https://youtu.be/fH5giAcDp6Q

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
Contributor ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

LATEST

Rick...sorry for taking so long to get back to you.  I watched both videos and they were a great help in understanding text ranges, locations, and objects.  I would be very interested in more videos and possibly a playlist as you mention.  This is the "Missing Frame Scripting Manual."    Thanks for posting!

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