Keyboard Maestro Scroll question

I have looked through the KM commands and I have not discovered a solution to my problem. I want to tell the document I am editing to scroll to the top of the screen, according to the location of my cursor. This is assuming that my selection is more than a page down in the document.

I have searched in KM using the terms page, up, and scroll, and I have found no results that would help.

I have a macro that I use often. When I am writing it helps my thinking process to select some text, usually at least a paragraph. The KM macro copies the text, the key left arrow is simulated so that the text is no longer selected, and KM speaks the clipboard.

As my writing section is read aloud I follow along and make edits. I also have a macro to stop all macros that I can use to kill the macro and make longer edits if need be.

I am writing in Scrivener. When I hit the macro the selected section always scrolls to the center of the page unless it is already on the first page when it scrolls to the top. This is a bit annoying as I have to visually hunt to find the part I am editing. I want the selected section to start as the top line at the top of the screen while I am focusing on it.

I cannot do this using mouse position or scrolling position as the starting point can be anywhere in the document so a specific measurable for the distance to scroll or where to position the mouse would not work.

I cannot use the home key as that scrolls to the top of the document which is not where the selected section is. To restate, this selection and read aloud editing can occur at any place in the document, last paragraph, page 4 - 8 of a 40 page document, etc. What I am looking for is for my selected section to scroll to the top of the screen for easier visual location of the section I am editing. This is defined according to the cursor being at the start of the selected text to edit.

Your problem is tricky, but almost nothing is impossible with KM. My approach would be to calculate the amount of scrolling required by using the Find Image action to locate the selected text. Or to put it another way, have KM scroll the page until it sees the top of the selected block near the top of the screen. You could do this in a single action inside a while loop. Very simple. I'm not sure how reliable it would be until I can see the screen with my own eyes.

However I can't see your screen and I don't have Scrivener so that makes it impossible for me to prepare a macro for you. I might be able to help if you upload a video of the Scrivener app and show me what it looks like when you select text.

There might be better approaches. For example a better approach would be to find out if Scrivener supports AppleScript interfacing. Then you could probably get a system that works 100% guaranteed. But I'm not skilled enough with AppleScript to be able to write that for you.

I am not sure that this is a problem that you will need Scrivener to test. I tried copying the same text into an NVAlt test document and the same thing happens. It scrolls to the middle of the page before it starts.

I tried testing in a Nisus document and the results are slightly different. In Nisus if I am above the selected section it scrolls the selected line to the bottom of the screen so I have to start scrolling right away to follow. If I select from below it will scroll to the top of the screen - which is what I want.

Just to mention it, yes I will sometimes select a section and then continue to look above the section to make sure it it all of what I wanted to review and to edit. Similarly, I will sometimes select a section and end up far below the selection, scrolling the page, not continuing to select.

Also, I sometimes make good use of non contiguous text selection to review how two ideas are flowing together as I edit them to make sure my point is clear.

All in all and at the end of the day I was hoping that there was a macro command in KM that could recognize the cursor position in a block of text and send the line that the cursor was in, to the top of the screen. If I select any text, and simulate a left arrow, then the cursor will always be where this macro could move the line that contained it to the top of the screen.

I looked at the image section a little but I don't understand what this does. Do you think this would recognize a selection of text as an image? If so this could indeed be a solution. I could perhaps have the macro recognize a selection of text as an image and then, scroll to the top of that image (selected text block), then hit the simulated left arrow key. Hmmm .. I wonder. I will have to test this. Thanks.

For this type of action, KM usually relies on AppleScript to get the information.
Unfortunately, this is very application dependent. Some apps provide a scripting dictionary that will allow AppleScript to get and set the text cursor position. But most apps don't.

Having said that, if all you want is to select and copy the line (paragraph?) where the text cursor is located, go to the top, and paste, that can be done. Note this is moving the text cursor to the top, which is different than scrolling to the top (which leaves the cursor in place).

Here's an example macro to get you started. Adjust as needed.
Tested using TextEdit.

MACRO:   Copy Current Paragraph to Top of Document [Example]

**Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/b/2/b22ffcc944589a8229eb1b9369e6e0ccdfb6dad7.kmmacros">Copy Current Paragraph to Top of Document [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


<img src="/uploads/default/original/3X/6/a/6a3689ef24c6f39aaa59c7391ebcc80803f62c5e.png" width="465" height="648">
1 Like

I have a macro that I use often. When I am writing it helps my thinking process to select some text, usually at least a paragraph. The KM macro copies the text, the key left arrow is simulated so that the text is no longer selected, and KM speaks the clipboard.


When I hit the macro the selected section always scrolls to the center of the page unless it is already on the first page when it scrolls to the top

I think there's something else going on here, because as you've described it, scrolling shouldn't happen.

I tried this macro with TextEdit, and it didn't scroll:

The only thing I can think of, and it seems unlikely, is that you might need to wait for the "Copy" command to finish. Like this:

Yes, the Find Image action can do that type of thing. I consider myself an expert at that action (it might be more accurate to say I'm very experienced with it.) But whether it will work for you depends upon some issues. Not only would I need to see your screen, but we might actually have to change some aspects of your screen to make this work. For example it might work better in dark mode than light mode. Or it might work better if you used a certain font size. These are details I can't address without seeing screenshots.

So for example when you select text there's usually a coloured corner section that looks like this: (notice the upper right corner of this image)


So it would be fairly easy and reliable for KM to search for the upper corner of the selected region, which means the code to solve your problem would look like this:

Now this little sample of code is just an example. It actually doesn't quite work because it will scroll until the top of the selected text is above the top of your screen, so it would require a slight modification. It's meant to show the kind of magic that can be done with the Find Image action. As a matter of fact I actually do this approach in one macro and it works with 100% reliability. So I'm comfortable in saying it will work for you too, with the appropriate error handling and corrections.

The wizards here on this forum are trying to explain how AppleScript would be better (which is what I predicted above.) It's hard to argue with them, but from my personal experience, if we can get a good corner image, we can get this to work approximately 100% of the time. And look at the simplicity of my code. Wow.

1 Like

The problem with pasting is that it destroys the formatting I had already. If I wasn’t clear, this is about selecting odd shaped amounts of text, often going across a few pages. When the selected text is read aloud, I wanted the beginning of the selected text to move to the top of the screen. As it is, I can find it in the middle of the screen, but it would be better if I could figure out a way to have the start of the selected text, that is, the selection point or, where the cursor is when the selection becomes unselected, move to the beginning or, to the top, of the screen.

I don't know what the shape of the text selection is as it is always going to be something different.

1 Like


I am trying this but I am not understanding what you used as the image in the image box in your example? It does scroll up nicely and this could prove to be the solution. But I don't understand your example of how to set this image.

Testing to see if I can do anything with the image macro but that will allow me to set a found image to a variable. Your example does not allow for a variable to match the condition.


Yes, I see. I should have realized that my explanation was not perfectly clear. Sorry. I just do this technique so often that I forget what's not obvious to other people. Do you know how to take a screenshot from your screen? Are you familiar with how to take a screenshot of a specific area on the screen using CMD-SHIFT-5? I will assume you are familiar with that. In that case, what you have to do is use that method to take a screenshot of this part of your screen: (the upper corner of some selected text with some white area around it.)


Notice the red box. You must take a screenshot of the upper corner of the blue area including a substantial part of the white area. I'm assuming that your screen is in bright mode, not dark mode. I'm also assuming that your editor uses the colour blue when selecting text. Since you haven't shown any screenshots, I'm forced to guess certain details like this.

In your example you used the Find Image action before the UNTIL loop. I'm not sure why you are doing that. That's not what my example showed. If you are trying to follow my example, delete that action. but if you want to create your own solution, I don't mind.

You are supposed to put that small image, with the white and blue, inside the small box at the bottom of the UNTIL action. Here:


In the code you uploaded you neglected to paste that image inside that box. That's the main thing you omitted.

Do you see that blue and white shape inside the action? That's where you put the small image that you create with CMD-SHIFT-5.

As I said, this solution isn't 100% perfect. We will have to make some adjustments to it if you can get it to work. But this is the key part, so we start with this.

I also said that the AppleScript solution is probably better, but I'm not good with that approach. I'm not even sure if it will work for this problem.

1 Like

Genius idea. I hope it works!

Thanks, Now I understand your example a little better and I will test this. Until I can, here is where I have been trying for a solution:

I see that there is a condition to look for if the text is in a condition of titled or not. It would be nice if this condition also included the condition of the text being selected or unselected. This could solve my problem (I think).

I tried the following, with no better results. I was thinking that, if I could identify the starting phase in my selection, and then have a condition set to when the scroll reached the true condition, this might solve the problem. Sadly, no joy here.

Set System Output Volume to 84%

Type the ⌘C Keystroke

Get Substring of System Clipboard

  • Subrange from character 1 length 14.
  • Store result to Variable “StartingPoint”.
  • Notify on failure.

Set Variable “SpokenSelection” to Text

  • %SystemClipboard%

Store result to Variable “StartingPoint”.

  • Type the ⌘A Keystroke

Type the ⌘C Keystroke

Type the Left Arrow Keystroke

Execute the Following Actions

  • Simulate Scroll Wheel Down 1
  • Notify on failure.
  • Until All Conditions Met
    • The text “%CurrentClipboard%” contains “%StartingPoint%”

Set System Clipboard to Text

  • %SpokenSelection%
  • Notify on failure.

Speak Text With Default Voice “Alex” at Normal Rate

  • %CurrentClipboard%

Post Script : As I work through this, with the kind help of those on this forum, I realize that an addition I would like to make to this script is to set an if/then/else condition to interpret the condition if something is plugged into my audio port or not, i.e., if something is plugged into my audio jack, play at volume X, if not, play at volume Y.

This is because I use my MacBook Pro for my desktop and when I do I use a plug in audio amplifier and speakers so the volume is set one way to be just right for my desktop setup.

When my laptop is away from my desk and only the natural sound level of the laptop is output, the output volume needs to be increased.

I am not seeing a way of identifying if something is plugged into my audio jack or not. Ongoing appreciation for the assist.

Sadly, I am not getting results using the screen copy image of the upper right corner of a selected text area. Using this example as you have kindly laid out for me to look at, the screen scrolls very, very, slowly at 1. Increasing it to 12 is still slower than when I have seen it automatically scroll up. But even so, this keeps scrolling no matter how long I leave it. It will keep scrolling until I kill the macro. So no, unfortunately, this is not recognizing the image as it is not stopping scrolling when the image should have been located.

I was just thinking, shouldn’t there be a way in Scrivener to calculate how far down a page the cursor is from the top of the document? If so, there should be a way to turn that value into the correct amount of scrolling, shouldn’t there? If so it would be straight forward to left click after the selected text is copied to the clipboard, discover the distance to the top of the page, compute the amount of scrolling needed and, et voila, we have arrived.

It occurs to me that I have been spoiled working with an iPad for doing this sort of editing. With an iPad in iOS the text not only follows along, but it is highlighted as it is read. While that would be very nice to see in macOS, I am not expecting that to change anytime soon.

But, as the point of this thread is all about, it would be very nice to at least start my selected editing session at the top of the page as that is after all, what I am focusing on at that moment in time.


Yes. I am going when I have time to go back to my spelling macro and this might be relevant to getting the cursor back to where it was? I think Apple might just fix the focus issue with spell check though. However the palette you devised will remain useful enough to use it even then.

Oh, that's too bad you are giving up on this idea, because this actually works for me.

I can tell you - probably - why it didn't stop scrolling. Because you had the KM Editor open and the KM Engine was finding the blue/white image inside the KM Editor window instead of inside the Scrivener window. There are even bugs in this approach that I haven't even told you about yet. But we can take only one step at a time because this is a tricky thing to do. I can't explain all the issues at once.

As for your question about "detecting if something is plugged into your audio port", since that is best addressed in another thread, I won't answer that here. The forum owners prefer that all unique questions go into different threads.

I think your approach of using AppleScript with Scrivener is probably "better" in the sense that, if it can be made to work at all, it will probably be made more reliable. So feel free to press the AppleScript experts here, but I'm not one of them.

I had the KM editor on a separate desktop so my macro was only aware of the Scrivener selection. One thing I am curious about however, you example used a medium blue highlight value. I was using a lighter lavender selection color. I am curious to try using a darker color or more intense highlight color as you used in your example. Perhaps this is causing it not to work in my example?

It just occurred to me that, since the default now is that the section always starts at the center of the screen, there might me an easy solution at hand to just tell it to scroll a set amount to the top of the screen from the center of the screen to where the screen always is located,

Fair enough about the audio port. I will put this question in a new thread. I will report if this idea of setting the middle screen to top screen as a known, fixed quantity has resolved the issue or not. Thanks.

PS: I know next to nothing about devising AppleScript sollutions, AppleScript is very useful but I have never felt comfortable using it's logic. With KM, most of the time, I don't have to use AppleScript, thankfully. If someone comes up with an AppleScript solution then I am happy to use it. Its just too much for me to puzzle out an AppleScript solution on my own.

I understand. but to be honest, I wasn't trying to make your macro work, I was trying to make my macro work. Your macro uses a totally different approach than mine.

Oh, and using multiple screens doesn't prevent the bug I spoke about, because the Find Image action, by default, looks at all screens.

Hehe. Yes, you are supposed to use the colour on your screen, in your application, not the colour from my screen! This is very screen specific. If I could see a screenshot from your screen then that would make this much easier to solve. But so far you haven't sent any screenshots.

I don't mind if you want to change my approach. But my approach hasn't even started working for you yet. Until my approach works, you need to follow my instructions. But feel free to go off on your own at any time, that's perfectly okay.

:white_check_mark: No, I was saying that I was using a lighter color, not that I was using your color. I copied the corner from my screen, not from your example. That didn’t work either but at least it did stop the endless scrolling.

Ah well, that is enough time spent on this for one day. I worked out a very simple solution for the short term. I use the AppleColorEmoji fonts for special colored characters at times. One of them is a bright green checkmark. All I had to do was place the character in a "type as" and the macro inserts it. At least in this the way, I am visually tracking where my macro started from. Naturally I have to delete the character when I am done but hey, this is all about editing anyway.

I would still like to arrive at a better solution as we have discussed for for now, good enough is, good enough for now

Much thanks.


If you're happy with your solution that's the main thing. If you want to get adept with KM's Find Image action that's a secondary thing. I didn't realize that inserting an emoji would be satisfactory, in fact I've almost never used emojis in the past (I guess I'm just too old) but I'll take that emoji tip as a great tip because it might help me with my own problems with Find Image in the future. Thanks.

In fact I know from experience that Find Image works best when three colours are picked as a target. Your emoji has green and white. I'm sure there must be emojis with two colours (plus white) which will make three colours which will be the best ones to use.

Hmm. Most emojis are limited to two significant colours, like the one you are using. I scrolled through some of the options on this website and found a dozen that should work pretty well because they have three or more colours, like: :face_vomiting: :heart_eyes: :sunglasses: :gift_heart: :ok_man: :sunflower: :avocado: :fire_engine: :vertical_traffic_light: :rainbow: :parasol_on_ground: :framed_picture: :pill: :costa_rica: I think the rainbow would probably work the best because it's the one with the most colours in it.

Hey, how many times do you get to write a message analyzing the compatibility of emojis with KM?

I use PopChar from the developer of Typinator. It opens a whole range of these characters, many which seem to have more than two colors, flags for example. :afghanistan::united_arab_emirates::senegal: :house_with_garden::dango::beach_umbrella:

And BTW, just to plug one of my favorite apps that I would not like to ever do without - besides KM of course, is Typinator. It expands text of course, but it also can use scripts. I used TE for years. It was a bit unstable at the time. Typinator is very stable and well supported. And, it is not a subscription based product - which I like.