Macro Tutorial - Inserting Text

September 24, 2012

An extensive documentation and tutorial is available here. The tutorial below is outdated.

Today's tutorial will focus on inserting text into the current page using Macros. If you haven't still gone through the main tutorial that explains the general structure of macros, I encourage you to go here and check it out first.

Back to inserting text. You may want to insert a certain text from time to time, maybe with minor modifications each time and typing it every time is cumbersome. You can create a macro that inserts the text you want to the place you want.

Inserting text at the cursor position

Most commonly you will want to insert the text at the cursor position. The part of the page that is under the cursor is considered as selected. So you can build a macro that modifies the text that is selected. To do that let's start the macro editor using the New Macro button add the following:

Insert text macro screenshot

Run the macro and it will add "I want more tutorials" at the cursor position.

Inserting formatted text

We saw that it is easy to insert some text. What if we want to have some formatting on this. Let's say we want the word "want" in red and bold. We need to simply find the word "want" and change its formatting options. Note that the page may contain the word "want" in other places, so we want only the one we just inserted. Differentiating the one we just inserted relies on a key observation, which you should have seen when you ran the first form of our macro: The inserted text is fully selected. Since we didn't touch the "selected" property, the previous selection, which was just the cursor itself, is expanded to contain the text we inserted. Here is how it should be looking on the page:

I want more tutorials

Now that we know this we can take advantage of the fact that the text we are looking for is selected:

Insert text macro screenshot

Here the second part reads: Find the first text that is selected and has value of "want", change it to red and bold, which will find the "want" we just added and modify its format. Here is what you should see when you run this macro:

I want more tutorials

Note that when I tried this it didn't work due to a bug in Onetastic, so I had to go fix it and update the download page with the latest version. This minor update won't prompt you to update if you have already downloaded before this post. In that case you can go download the latest version if you want to try this. As always, if you hit similar issues, don't hesitate to report, so that it can get fixed.

Changing cursor position after inserting text

The current form of our macro inserts some formatted text, however it is inserted as selected. In most cases, you don't want it to be left selected, instead you want the cursor position to move to the end of the inserted text so that you can keep adding more stuff. Doing so is easy, we just need to remove the selected property (or rather set it to false), but you will see that this will cause the cursor to disappear or show up at some random position as there is no selected text left. Instead we will do a small trick. When we insert the text, we can insert a space character at the end and leave that one as selected:

Insert text macro screenshot

So I changed the first inserted text to "I want more tutorials " with a space at the end. And added the last For group which will remove selection from everything but the last space. This will end up with inserting the text and selecting the space at the end. From there, you can start typing. Here is the result of the final macro:

I want more tutorials 

Macro XML

If you want to import this macro in your collection, you can simply copy the XML below and use the Import button in the Macro Editor to load it.

<?xml version="1.0" encoding="utf-16"?>
<Macro name="Add Text" category="Auto text" description="Add auto text">
  <For first="Text">
    <That hasProp="selected" op="eq" value="true" />
    <ModifyProp name="value" op="set" value="I want more tutorials " />
  </For>
  <For first="Text">
    <That hasProp="selected" op="eq" value="true" />
    <That hasProp="value" op="eq" value="want" />
    <ModifyProp name="fontColor" op="set" value="red" />
    <ModifyProp name="bold" op="set" value="true" />
  </For>
  <For each="Text">
    <That hasProp="selected" op="eq" value="true" />
    <That hasProp="value" op="eq" value="I want more tutorials" />
    <ModifyProp name="selected" op="set" value="false" />
  </For>
</Macro>

Comments

Name
Comment
Omer Atay - 2014-12-30
Andreas, I added a new tutorial to the blog about how to insert a table.
Andreas - 2014-12-23
How can i insert a simple table as an exmaple with 3 columns (A, B, C as content for each cell i the first row)?
Omer Atay - 2014-02-27
Ion Cannon, the macro language got some updates since this post. You can use the Insert command to add more lines. See a sample macro below:

<?xml version=\"1.0\" encoding=\"utf-16\"?>
<Macro name=\"Insert two lines\" category=\"Insert\" description=\"\" version=\"6\">
  <For first=\"Outline\">
    <Insert type=\"Paragraph\">
      <ModifyProp name=\"text\" op=\"set\" value=\"First line\" />
    </Insert>
    <Insert type=\"Paragraph\">
      <ModifyProp name=\"text\" op=\"set\" value=\"Second line\" />
    </Insert>
  </For>
</Macro>

Other Posts

Show all posts