Changing macOS Editor Behavior?

The only thing I can think of is to write a macro that when ever you select a text it also hits Right arrow as well that way you can add text to the end.

On further reflection, the first thing that you are requesting is so "weird" and so contrary to the low-level Mac behavior that even within a specific Mac text editor, implementing a KM script to try and override this would be either impossible or a very bizarre script. As I would see it, basically, you would have to intercept every character that is typed on the keyboard and then establish whether some text is currently selected and then issue a Beep sound and override or reverse the standard behavior.

So my answer to your first question would be no.

When I do this to myself (and I do it a lot), I use a more colorful music phrase than a mere "beep" and immediately hit Command-Z to undo my mistake.

It didn't take long to train myself, but I was assiduous about it.

2 Likes

I like your question and your idea. I would love to be the person who solves this for you.

There may be an ambiguity to your question. For example, it's not just they keyboard that can replace selected text. That's because the mouse can also do the same thing. I suspect you're not too worried about that scenario. And there may even be other things that can trigger the deletion. Let's just try to solve this for the keyboard now.

I think there are three things we have to do to make this work. 1. Get a macro to trigger on every keyboard key, which can be done by using an option in the "This string is typed" trigger called "Regular Expression Match". (Yes, this intercepts and triggers on every key.) 2. Write an Applescript action that saves the currently selected text, if any. 3. Write another Appelscript action that checks if the selected text is now missing, and reinsert the text with a beep. Not too difficult.

As for your second question, there may be an easy solution because there's already a shortcut for end of line called CMD-RIGHT and one for end of paragraph which I think is OPT-DOWN. It sounds like you are just asking for a macro that alternates between those two characters. That would be a nearly-trivial macro. We may need a little clarification on what you mean because there's some ambiguity in your request. (The macro could reset to the first keystroke after 5 seconds, if that seems like what you want.)

It sounds good to me! Want to try it?

I've found some interesting stuff related to this on the web.
http://www.hcs.harvard.edu/~jrus/site/cocoa-text.html

I'm doing some research on AppleScript, and I came up with this thread on this website which discusses a very similar problem. It's more difficult than I thought, but it may still be doable.

Here's a test macro that's only partway to the solution but it gives you an idea of how the solution might work:

All it does is beep, but you will notice that it beeps a different sound depending on whether text is selected (this works for some apps, like Safari, but not some other apps. You didn't really say whether there are some apps that would satisfy you. Obviously no solution can work with all apps because some apps grab the keyboard in different ways. Even in Safari this macro only works some of the time, because if someone types too fast, they can beat the dynamically-created menu.)

EDIT: I got it working, but you may not like the approach I used, which requires a lot of macros, so I'm working on a simpler solution.

EDIT: I found more than one solution, but some of my solutions are not very reliable, and some of them are slow (and most of them caused the text to be de-selected.) I'm not happy with that and I don't think you would be either. I'm still trying to find a fast, reliable solution with very little code.

1 Like

Okay I have a solution that works. But let me explain the problems (some of which are fixable.)

  1. It works only 90% of the time. I'm not sure if the remaining 10% is caused by a timing issue or a macOS behavioral issue (eg, I'm making the GUI too busy) or a KM issue. Maybe one of the real experts on here can tell me what I'm doing wrong. For your purposes, I don't think a 10% rate of omission is a serious problem.
  2. Secondly, my solution is currently designed to work only with a certain app (eg, Safari or Notes). That's not because this solution can't be made system wide, it's merely for debugging purposes. I don't like it when my macro has a bug and then starts messing up my KM Editor. Once it's debugged it should be easy to make it system wide. So in the uploaded version you may see it works only with those apps. No big deal.
  3. Thirdly, no solution can work with any apps that don't play nice with the input device. No doubt there are a lot of apps, like games, which grab the keyboard some low-level way that will make this app fail.
  4. Fourthly, my app currently weighs heavy on the some system resources (it appears to be hard on a certain macOS app that handles the GUI.) That's not good, but it is entirely possible that I can fix that in the future (I can certainly reduce the load.) I thought AppleScript would have an easy way to tell if text is selected, but it doesn't. And the AppleScript examples online seem to solve this issue the same way I do - by reading and writing to the clipboard. So maybe there is no perfect solution.
  5. My solution causes the selected text to become de-selected. I don't think that's what you wanted, but I think I may have a solution to that. Since the solution involves AppleScript, I'm not going to fix it unless I've got the 10% reliability issue resolved.
  6. When I said my macro is 90% reliable, your results may differ. For example I tested it only on an M1 Mac, and you might have an Intel Mac which could give very different results. The odd thing about the 10% error rate is that it's not always independently random - sometimes the OS gets "moody" and fails 2 or 3 times in a row, then works for 20 times in a row.
  7. My current macro will trigger on the letters A to Z. I specifically did NOT include LOWERCASE a-z because I find this helps me to troubleshoot the macro. It's easy enough to add a-z, and 0-9, and the other missing keyboard characters. It's not worth it until the macro works 100%.
  8. This solution of mine seems to inhibit some other OS features like keyboard autorepeat. I agree, that's not good, but there's a chance that can be fixed so I'm not giving up on this solution yet.
  9. I have another solution that works 100% perfectly, (the OS does require a fraction of a second before it tells me that text is selected) but it requires 85 small macros, which I don't think you want. If we can't get my posted solution to work 100% of the time, we may have to revert to that.

Obviously an imperfect solution is unlikely to be acceptable, but solutions sometimes need to be collaborative. Some people will simply criticize my imperfect macro, but some might try to spot what the problem is and fix it.

I will upload the macro into the next post below. This post is already long enough. Bear in mind that my solution contains two macros and these two macros will be in a group and this group will have an an attribute that lets the macro run only when Notes or Safari is active. As I said above, that helps with debugging. I've never uploaded a "macro group" before, so I hope I don't have difficulty doing that.

Okay here's my test macro to solve your problem. It says it works with both Safari and Notes but I noticed that it works only with Notes as is. That's okay, it's just for demo purposes anyway.

This group contains two macros, one which constantly copies the selected text in the Notes window, and the other which intercepts any uppercase letter and then won't let the user overwrite the selected text (it currently does this by replacing the text. This approach might be changed.)

Constantly Intercept capitalized Text Macro (v9.2)

Constantly Intercept capitalized Text.kmmacros (5.2 KB)

I'll paste images of the macros below:

and...

As you can see they are relatively short and simple. But some of the actions would take time to explain. This code is still far from perfect, but maybe someone can look at it and say they can do better. Your original question is more complex than it first appeared.

Even though this solution is still flawed, it's brevity makes it more pleasing than my 85 macro solution which has more like a 99% success rate at meeting your requirements.

Wow, you really worked hard on this! Thanks!!

Unfortunately I can't test it now. I'm ~1,000 miles away from the computer and it dropped offline last night :frowning: I hope it comes back soon...

Thanks again!

Thanks for the thanks, but I help people because I actually enjoy it, so I thank you too. And I thought your question was very interesting, too. So it was my pleasure.

Im expecting you to reject this solution, but if you do, then remember I have an 85 macro solution that works much better, perhaps perfectly. But you might reject such a large solution.

i got back online and tried the brief solution, thanks! when i type a capital letter it inserts this:
%SystemClipboard%
so i guess i need to upgrade my keyboard maestro from 6.4?

I finally got home and tested it! It seems to have some problems. Perhaps I could try the bigger solution?

Thanks so much!!
Pen

Aha, I noticed why the simple solution wasn't working - the second macro didn't download. I tried typing it in but all it seems to affect is using the clipboard - I have trouble pasting, and also it doesn't beep when I type over selected text. Maybe it's because I don't have a macro group Test?
I'd still be interested in getting either solution to work.

Any particular reason why you are using Version 6?

I didn't need to upgrade... but now I have.

2 Likes

I'm so sorry I lost track of this!! Could I try your 85 macro solution?

I really do much appreciate the help.

1 Like

Have you recovered @penhelm? You must have gone into shock jumping from Keyboard Maestro 6 to 10. :flushed:

1 Like

@penhelm, could you give some examples of where this has bitten you in the past? It might be easier to change the selection behaviour, eg if it's when you are tabbing through dialogs, than such low-level over-typing behaviour -- and certainly a lot less resource intensive than constantly monitoring input/selection state.

1 Like

It's bitten me when I didn't realize the focus was in a TextEdit window with some text selected. I hit a key and it overwrote the text. It happens from time to time. I've lost some stuff.

1 Like

Are you trying to use single-key shortcuts?
If yes, you can use a "Short Tap do Letter, Long Tap do something else" macro. There are a couple of these macros in the forum.

But tbh sounds a little bit difficult to pull off in your case, without resorting to activating and deactivating macro groups.

I'd suggest you to instead try macros using a modifier key such as Control, plus the key. The Control key is not used a lot on OSX, so it's a good candidate to use it for shortcuts.

Example:
Ctrl-S to open Safari

1 Like