Move Cursor to a Specific Character?

Hi,

I'm wondering if it's possible to do this with Keyboard Maestro and if so how to do it. I'm new to scripting and this has me stumped:

I'd like to have KM move the cursor to the right of a specific character in selected text. Precisely, I'd like to select some text in the iTunes Get Track Info window, and then trigger KM to move the cursor to the right of the first semi-colon in the selection.

Is this possible at all?

Thanks.

Unfortunately, this isn’t possible unless the app in question has some seriously good AppleScript support.

Bummer, but thanks Dan for clearing that up.

1 Like

Actually, I think that could be done.

Rough outline of a macro:

  1. Copy the selection
  2. Find the position of the target character (semicolon in this case) within the string
  3. Calculate the position from the start of the string
  4. Do a Type Keystroke of LEFT-ARROW to move the cursor to immediately before the first character of the selected string
  5. Do a Repeat Action, with the count set to the value from #3, using a Type Keystroke of RIGHT-ARROW

This should position the cursor after the first semicolon.

You may have to do some adjustments on the counts.

Give it a shot, and if it doesn't work for you, post the macro you have (image and file), and we'll try to help.

What I don’t understand is how to do 2. and 3.? The position of the semicolon will be different depending on the text I will select (not always the same string, but always contains a semicolon where I need the cursor to be moved). How do I make KM calculate the position of the semicolon in a particular string?

Thanks.

This is dead simple using a script. If I can't find a non-script KM Action to do this, then I'll write the script for you. NBD.

Give me a day.

Thank you so much. Please don’t give yourself too much trouble over this, but it will certainly be greatly appreciated.

Hey Martin,

This task is a little easier if you think outside the box.

What the macro does:

  1. Copies the selected text.
  • Replaces the first semicolon in the text on the Clipboard with itself and a newline.
  • Pastes.
  • Arrow-Up.
  • Presses the Forward Delete key.

Voilà! No character counting required.

-Chris


Move Cursor to Right of Given Character in Selected Text v1.00.kmmacros (3.6 KB)

3 Likes

I've posted a method to count characters here:

I’m always amazed with the creativity people have on this forum. When I see potential solutions like yours, Chris, it just makes me smile. I never would have thought of your solution.

I need to remember to post more questions, to see if there’s better ways of doing things. In this forum, multiple heads truly are better than one.

1 Like

@nadeama, I think this should do the job for you.
Please test the below macro, and let me know if it works for you.

Turns out that there is no easy way to find the position of a substring within a string using non-script KM Actions. So, I wrote a general purpose script to do this.

1 Like

Hey Chris, great thinking outside the box.
Very clever.

However, there may be some limitations and undesired consequences to this approach:

  1. I don't think it would work in a single-line text field.
  2. Adding a linefeed might trigger some other effects in some apps
  3. Since the paste is plain text, it might remove the formatting of styled text in the original document.

Hi everyone,

Thanks so much for your solutions. You guys are great!

Chris, your inventive solution worked like a charm in TextEdit, but as Michael pointed out it was problematic in iTunes (where I want to use it) because of the single-line fields of the Get Info dialog.

Michael, your script did the trick. You just saved me hours and hours of manual text input when editing tags in iTunes (I do a lot of that). Thank you!

Keyboard Maestro is really amazing. I can’t believe I haven’t used it before for anything but launching apps with a keystroke.

2 Likes

Very nice, Chris!

I have used this technique to create this macro:


Position cursor behind entered character.kmmacros (11.4 KB)

Demo:
1

1 Like

Nice, @ALYB. I got a little dizzy looking at your list of triggers, though, and wrote a different version that uses typed string trigger. You trigger it by typing two semi-colons and then the character in question, with no spaces, like so: ;;v, etc.

ALYB.kmmacros (6.0 KB)

1 Like

Yes, @cfriend I don’t like this long list of hotkeys either, and it costs a lot of hotkeys that are no longer available to other macros. Ideally I would press: one hotkey, character. But afaik this isn’t possible.

I will try this suggestion: Initiator + Multiple Keystroke Trigger - #3 by noisneil

Edit: Works okay.

Thanks! It worked for me too!

I'm sad to say that @JMichaelTX will not be able to enjoy your response...