Typing Out a Sentence

Hello,

I need a macro where I can enter a sentence, for example: "Can you write this down?" And when I use the macro, it types more and more words from the sentence. For example: First time: Can, Second time: Can you, Third time: Can you write, etc...

Does anyone have a suggestion for how I can create something like that?

These are a sequence of different calls to the macro ?

Or you trigger it once and it writes out a sequence of lines ?

Yes, every time you trigger the macro it will write out one more word of the sentence.

I've left the last action here as a "Display in window", which you could change to an "Insert By ..." (pasting or typing), but this may suggest one approach to incremental building of the string.

Here it generates a slightly longer line each time, so the output ends with a newline, but it occurs to me that you may want the macro to only write one word at a time, rather than a slightly longer line each time.

Either way, you should find some reusable elements here, I think.

See:

How to use custom array delimiter here:

manual:Variables [Keyboard Maestro Wiki]


The example below uses a space character as a custom delimiter, permitting indexed reference to particular words.


One more word each time.kmmacros (7.9 KB)

2 Likes

Thanks! That worked perfectly!
It might be too much to ask, but would you be willing to comment the steps and explain what they do?
I would really like to learn how you put this together.

Here's my attempt. There are actually two macros here.

The first macro prompts you to set the sentence. You might prefer some other input method; this is just one way of setting the global variable sentence for test purposes.

The second macro types each successive word every time it is triggered, until no words are left.

1) Sentence typer: set the sentence

Sentence typer- set the sentence.kmmacros (2.1 KB)

Keyboard Maestro Export

2) Sentence typer: type the next word

Sentence typer- type the next word.kmmacros (2.9 KB)

Keyboard Maestro Export

Edit...

It occurred to me that the second macro would not handle hyphenated words correctly. To fix this, replace both occurrences of (\w+)(.*) with (\b[\w'-]+\b)(.*) (this regular expression is copied from an answer on Stack Overflow) .

Keyboard Maestro can treat a variable as a numbered array of sub-strings, by:

  1. suffixing its name with [n], where n is some integer, and
  2. following the square-bracketed index with the character to be used as a segment delimiter.
  • The parts of the array (here, words) can be referenced with a 1-based index. Word one is at Variable%phrase[1] %"
  • The index [0] is magic and holds the total number of segments in the array. (The length of the array).

Hence:

Set Variable  local_WordCount to Text "Variable%phrase[0] %"

On each run of the macro, the following IF THEN ELSE block:

  1. finds or creates a wordIndex variable, and
  2. either increases its value by one (next word), or, if the variable has just been created, or is already pointing at the end of the sentence (i.e. wordIndex=local_WordCount), sets the value of wordIndex back to 1 (start of sentence)

Finally, the macro assembles a sub-sentence, consisting of the first N words (where N is defined by the incremented [or reset] value of the wordIndex variable).

local_wordsTaken is initially defined as an empty string, then:

for each index number in the range [1..wordIndex], the corresponding word, (followed by a space), is appended to local_wordsTaken.

The completed loop defines or constructs a sub-sentence of the length required on this run of the macro.

4 Likes