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.
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);
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);
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");
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.
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.
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);
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);
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.
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");
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.
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.
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.
Copy link to clipboard
Copied
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!