Conditional With Look-Ahead RegEx

Hi, I'm trying to create a macro that will look in a doc at text to the right of the cursor and then take a given action -- executing one or the other keystrokes based on the pattern it matches. This is to attempt to normalize behavior of keyboard shortcuts for moving the cursor in a text document (in IA Writer, punctuation and spaces seem to be ignored when advancing the cursor and in other editors they are not).

Is there a way to invoke a given behavior when a macro is executed based on a pattern match to the right of where the cursor is placed?

Example sentence:
"Test sentence -- with punctuation.

In IAWriter, the keyboard shortcut option + right arrow advances the cursor to the end of a word and if it encounters punctuation, it will skip it and advance to the end of the next word. There is another shortcut (command + arrow)to advance just one character and this works also if the character is punctuation. In the eg, using the option command, cursor would advance to the end of "test", then to the end of "sentence", then all the way to the end of "with". I want to change that behavior so that if there is a punctuation mark, the cursor will advance only to the end of that punctuation and not past it. This could be achieved by determining that the character ahead is punctuation and invoking command + arrow instead of option + arrow.

Sounds like you want the substring to the right of the cursor and then it's just normally ReGEx processing and so on.

Hi, Could you explain how to get the substring to the right of the cursor? I see an action "get substring of clipboard or variable" but didn't see how to use that to capture text within the current file.

Hey @sottovoce,

You really can't do what you want, because the macOS doesn't offer hooks into the system to allow you to detect the cursor and text in an editing context.

You can do this in BBEdit, because it's highly AppleScriptable – but most apps aren't.

You'd have to:

  • Emulate Cmd-Shift-DownArrow to select all the text from the cursor to the end of the document.
  • Copy the selected text.
  • Emulate Right-Arrow to return the cursor to the starting point.
  • Process the text to find your end anchor.
  • Move the cursor at cumbersomely slow speed to that anchor point.


Is there a way to invoke a given behavior when a macro is executed based on a pattern match to the right of where the cursor is placed?

If your problem is just related to IAWriter, the following Keyboard Maestro might help. It is not terribly fast. It is predicated on moving through the text looking at each character until it finds a space or an end of line. Then it stops and moves back one character (i.e. in front of the space that it found).

You were not entirely clear what your desired behavior was when it ran into a punctuation mark. Did you want the cursor to end up after the "n" in punctuation? And then, after the next invocation, end up after the . (period)? Or did you just want it to immediately end up after the period that follows the word punctuation? The macro that I have provided results in this latter behavior. It would have to be modified if that was not the behavior that you wanted.

þ is just a random unusual character that is not likely to occur in your text.

MoveToEndWord Macro (v9.2)

MoveToEndWord.kmmacros (7.1 KB)

Test sentence -- with punctuation.
The dog, jumped. Over the lazy fox. Who dared.

Test| sentence| --| with| punctuation.|
The| dog,| jumped.| Over| the| lazy| fox.| Who| dared.|

The macro above will put the cursor in the locations indicated by "pipes".

If you want the cursor to be in these locations:

Test| sentence| --| with| punctuation|.|
The| dog|,| jumped|.| Over| the| lazy| fox|.| Who| dared|.|

It would be possible to construct a macro to do this second version but it would be more complex and, IMO, require RegEx.

A third version with the result below, would also require RegEx so it would be possible but more complex. I was not entirely clear what the OP wanted.

Test| sentence| --| with| punctuation|.
The| dog|, jumped|. Over| the| lazy| fox|. Who| dared|.

Rlivingstone, I really appreciate you sharing that macro. I tested it and it works as advertised and solves my problem. As you indicated, it is slow so I'll need to see if that is viable for my workflow or if I need to find an alternative editor. To your question, the desired behavior, technically, is to advance the cursor to the end of a word and then to the end of a punctuation mark. But your solution works well for the majority of my cases.

CCStone, Thank you for the context. I'll have a look at BB Edit as an option. If the AppleScript ends up making it slow, it probably won't be worth pursuing. But maybe it's workable as-is (unscripted). FYI, the feature I liked in IA Writer is "typewriter scroll" and this is absent in others I've tried.

Hey @sottovoce,


I downloaded the IA Writer demo and fooled with it for a while.

In my opinion you're never going to get this working in a way that works consistently with cursor movement or word/character selections.

BBEdit's native behavior is what you describe in Post #1, so you might want to take a look.


CCStone, You were spot on: what I needed was a more configurable text editor. I settled on "Sublime Text" (for now) which is easy to configure although a little slower than I'd like. My hope is that when I upgrade my older hardware the slowness won't be an issue.