General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion How to set a Range to a Selection

  • jsmart

    How to set a Range to a Selection

    Participants 0
    Replies 1
    Last Activity 13 years, 4 months ago

    The Documentation mentions you can have a Range that never changes. What is the macro sequence to set a Range to Selection, then move around document, then return to the Range
    and reselect.  Given a Range does not move until Range.Delete.
    Example:
    Set Rng to Sel
    Move to Bottom of text and select some text.
    Return to the Range and reselect.
    Derek Posted some clues, can he amplify.
    Looking for a method to navigate inside a document which is invisible to the User.

    Reply

    Derek Read

    Reply to: How to set a Range to a Selection

    You have several options for this. The easiest if you don't want to deal with (invisible) Range objects is to do the following:

    [code]//XMetaL Script Language JScript:
    ActiveDocument.FormattingUpdating = false;
    {call various Selection.x methods to move around and manipulate document methods here}
    ActiveDocument.FormattingUpdating = true;[/code]

    Using that allows you to work with manipulations visibly implementing everything using Selection methods and then simply wrap all of that in the FormattingUpdating properties to turn it on and off. If however, your script fails at any point and never turns formatting back on updates to the document will remain invisible to the author. Watch out for that and code appropriately to catch errors (JScript try…catch is always good to use in any script).

    When you move a Range object around the user does not see that, but the Range itself can be moved. Here's a simple example that should work with the Journalist demo (File > New > Journalist tab > Article template):

    [code]//XMetaL Script Language JScript:
    //create a range object
    var rng = ActiveDocument.Range;
    //find the next Para element
    rng.MoveToElement(“Para”);
    //select everything inside the Para
    rng.SelectContainerContents();
    //insert some text
    rng.TypeText(“hello world”);[/code]

    There is a limitation to this though, and that is that if you in any way change what was originally selected that may destroy the original selection and you will usually not be able to return to it, because the original Range will often not exist. This is particularly true if the original Range is a selection as opposed to an insertion point (ie: a bunch of selected text vs your cursor just sitting there flashing). In this case there is a workaround for that, which is to store the value for Range.Collapse(sqCollapseStart) and also Range.Collapse(sqCollapseEnd) as separate variables, then perform all your manipulations. If those two points still exist then you can extend one of the Ranges up to and including the second one using Range.ExtendTo() and then make that newly extended Range visible using Range.Select().

    In some cases you may find that manipulating the document using Range still causes things to occur visibly to the end user. In this case you may need to resort to using the FormattingUpdating property.

    A third method for manipulating documents is to us DOM calls. There are basically standard DOM manipulations implemented like most other tools do with some additional extensions.

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support