Insert cursor before word

Good morning,

I have what I hope is a simple request, but for which I so far have not been able to find a simple solution:

I use a number of RSS feeds to populate social media posts for some charities I work with. The formatting, however, is terrible when the feeds insert the posts into the various social media accounts.

I have created a KB macro that will fix the formatting as I need it to be, but only if I can get the cursor to move to just before the beginning of the URL of the article that is being posted. The rest of the workflow I have written will then put the URL and hashtags on their respective lines, approve the post and move on to the next post to be edited.

Try as I might, though, I just can't seem to find a way to get KB to place the cursor just before the http part of the URL.

At first I tried a simple CMD+F action, but this only highlights the link. I can't then get the cursor to come to rest just in front of the URL.

I'm using the Chrome browser on a MacBook Pro. The social posting tool is called SocialBee. My KB version is 6.4.8.

Can anyone think of a way to get this to work? I would very much appreciate it.

Best,
Will

I take it this is harder than I first thought?
Will

1 Like

Is the URL in the regular window or in some kind of list? If it is in the window you can do
Cntrl + L
will take your cursor into the URL window or whatever they call it. and then
Command + left arrow
will take you to the beginning. So you can just automate those key strokes into your macro. Using the "Simulate Keystroke" action. It is diffcult often to place a cursor. If you can highlight something though Cmmnd + left arrow works. I think I might not fully understand your problem. I don't use any RSS feeds now.
There might be a regex way of picking out and transforming URL's in a list type arrangement that is preferable to using the cursor. I don't know the apps and sites you are using. Perhaps you can modify this to take you to the beginning. If you don't know regex though it can be intimidating. I don't know it well enough to be sure I can make one that will work for you without mistakes in it or not cause you problems. This might help though

https://wiki.keyboardmaestro.com/Regular_Expressions?s[]=regex

Getting the cursor onto a very precise particular positon is very difficult to do on Keyboard Maestro I have found. I had a macro that involved getting it into a large side window, that worked ok since there was a broad target. Usually I do it indirectly as it were, by simulating keystrokes. I have a spellcheck macro that works quite well other than I can't get the cursor back to where it left off, long story ask @JMichaelTX he souped the macro up for me and then some!

Thanks for your reply, tudor_eynon, but I think you've misunderstood what I'm asking.

I'm not asking how to get the cursor into the URL bar of a webpage. Rather, I am asking how to find the beginning http part of a URL in a line of text in an RSS feed posted to social media tool called SocialBee.

The RSS feed adds a post to a queue and the post contains both a URL and hashtags. When opening the queue window, a prospective post looks like the attached screenshot.

I need to be able to precisely place the cursor just before the http in the URL. The rest of my macro (already works) will move the URL to a new line and then the two hashtags to their own new line.

I have a workflow that if I put the cursor anywhere in the word before the URL, it will jump to the URL and perform the rest of the actions, but if the word before the URL is enclosed in parenthesis or has a quotation mark at the end of it, it wrecks the formatting.

So, if there is an easy-ish way to put the cursor at the beginning of the URL, that would help me enormously.

Thanks,
Will

1 Like

I realized you probably had a text file of some kind. I think the way to go is regex. To put URL s onto the clipboard, format them there and paste them back or some similar workflow.

"Find and Replace with Regex" is the action in that case. I think you might find a regex string that will pick out the position at the start of a string containing 'https" if you search on this forum.

2 Likes

Thanks. I'll have a look. Much appreciated.

Will

Could I add that if you get the macro working fully it might be a very useful one to post up for the rest of us to use. I could use a modified version in fact I am pretty sure, it could be really useful.

So when you get it working I would love to see it on the macro list. I guess you already have some regex working somewhere in your workflow that is picking up "?" as a metacharacter?

I agree with @tudor_eynon that RegEx is probably the best method.
If you can select all of the text in the RSS feed, copy it, and set a KM variable to the clipboard, then this RegEx will extract the URL and hashtags:
(http(?:s?):[^\h]+)\h+(.+)

For details, see regex101: build, test, and debug regex

Here's what it might look like in KM:

image

If you need more help, just let us know.

2 Likes

Ah I see, that is neat, I will bookmark that.
On this one I was reminded of my spell check macro. It works fine for me since I work mostly from the bottom of the text so a simulated keystroke will take me back to the bottom of the doc. More or less where I was, exactly where I was quite often. However I couldn't find a way to get the cursor reliably back to where it started from; certainly not if you were correcting or editing in the middle of the document. I use the macro a lot and thanks for your additions again. It wasn't much use without them really.

I understand that placing the cursor can be tricky. I tried an elaborate regex method but it wasn't worth the trouble really, in the sense that I am still a newbie and, you know, I have to allocate some time to what I am supposed to be doing too!! :grinning:
@Will1 's solution might be interesting. How much of an issue is cursor placement? the native commands take care of most of it for me. Sometimes I would just like a macro to be 'perfect' you might say.

Thanks, Tudor.

I appreciate all your help.

Have a good weekend.

Will

Sorry, guys. I'm answering this all out of sequence due to a hectic workload today.

Thanks, JMichaelTX. That looks really interesting. I'll experiment with that tomorrow. I would never have figured out how to do all that! Regex is definitely not my thing, sadly.

My workflow is very simple and uses native keyboard commands, but I'm not on that machine at the moment, so can't post it at the moment. Hopefully I'll get around to it tomorrow.

Hope you both have a good weekend. As always, your help is very much appreciated.
Will

1 Like

I tend to look for actual examples for regex. I am not reliable making my own. Mostly I find something more or less what I want sometimes a minor tweak is all I need. Partly because my needs are quite limited and 'standard'. It means my advance with regex is slow though.

It is well worth getting into though I have found. My path is the same as yours, from palettes and simulated keystrokes to regex. I find to my delight that Keyboard Maestro is a good regex environment. It makes it very useful for me in a way I hadn't anticipated. Thanks JM and
You have a good weekend too Will and thanks.

Good morning, Tudor and JM.

Ok, I've tried a number of things using the Regex stuff you guys kindly provided and I've managed to get the posts copied to the clipboard, but really have no idea how to split things out.

Being both lazy and Regex incompetent, I decided to pursue things from a different angle: Since the vast majority of the posts fit on one line when I open the editing window and since when I click anywhere in the window it places the cursor by default at the end of the line, I've just modified my workflow to move the cursor backwards along the line to the start of the http bit of the URL.

It can then push the URL and hashtags to a new line, jump the cursor forwards and then push the hashtags to their own line.

In the event that the post spills over onto two lines, I've got a second version of the workflow that just moves the cursor back one more alt+left-arrow spacing before running the rest of the workflow. So far it seems to work. :slight_smile:

It's a lazy fix, but I know I could spend hours trying to figure out how to get the Regex side of things to work and still not get it right.

And I'm pretty sure you guys have better things to do on a Saturday. :smiley:

Thanks again to you both and all the best,
Will

1 Like

@Will1 That sounds fine to me, post it up for us to see? I am a big fan of solutions like yours.

I don't think we could quite see what you had in front of you or the apps you are using. Regex can be tricky to get to work right as a tiny detail will throw you.
I like solutions like yours and thanks for sharing it with us: as I said, why don't you post it up at some point or on this thread? You can upload a picture of the macro using Keyboard Maestro itself. It is in File > Share > Keyboard Maestro.

@JMichaelTX ahhh I see. You could do this with "Find and Replace" too? Using $1 etc. in the replace part? Really then $1 etc. are variables that regex engines uses internally as it were?
In the example you give how would you get the Variables "Local_URL" and the other one back into the document for example. Do you see what I mean? I am sure it is obvious but I am still struggling with these but nearly there. This one in particular would be really useful.

@tudor_eynon Ok, here is my working macro that covers the vast majority of the posts I need to edit.

Once I've clicked on a given post and the editor window opens, I just click anywhere in the window and the cursor, by default, goes to the end of the line. I then activate the macro and it does the rest.

I have a "wait for user interaction" bit at the end so that I can check everything is alright and if so, I just hit the down arrow and the updated post is saved back to the queue. Job done. :slight_smile:

But I do agree. It would be much better to have a Regex solution that will find the URL, move the cursor to the start of the URL (http part) and then carry out the rest of my actions. I have tried many times in the past to get inside Regex, but I'm afraid the subtleties of the logic are lost on me. More's the pity, really. Like you, I tend to dig around until I find a near example and then just hack through it until I get it to work. But in this instance, I just couldn't afford the time for something that I could more or less do with something cruder and less efficient.

All I've managed to do with the excellent info provided by @JMichaelTX is use his screenshot to create something that copies the post text (as it appears when I open the editing window) into the clipboard, but I really can't figure out how to then move the cursor to the start of the URL and execute the rest of my macro. I'm sure the both of you would have no problem with this, but again, please don't waste a perfectly good Saturday kicking this around. You must have better and more entertaining things to do. :slight_smile:

Cheers,
Will

Format SocialBee Feed Post.kmmacros (7.6 KB)

1 Like

@tudor_eynon and @JMichaelTX Here is what I've managed to do so far with JM's screenshot.

As you can see, it just copies the data to the clipboard and displays it to screen to show that at least that much of it has worked and then moves on to click the approved and saved buttons in the post editor.

Cheers,
Will

2 Likes

Looks good to me. So does this mean your problem has been solved?

Just in case this is not clear, RegEx has NOTHING to do with cursor movement. It simply takes some source text as input, and extracts text from that, storing the results of each Capture Group in a KM Variable.

Let us know if you need further help.

1 Like

@JMichaelTX More or less. If a post only has 2 hashtags in it and just clicking anywhere in the editing window puts the cursor at the end, my workflow seems to cover everything - unless there are 3 hashtags in a post. :frowning:

So, in this case, I've made a duplicate macro that accommodates this and I just have to use that.

It would, of course, be better if there were a way to find the beginning of the URL, regardless of any other content in the post, and move the cursor there and then execute the rest of the workflow, but it's not life or death.

Otherwise, I would assume using a regex solution that each element (the body of the post, the URL and the hashtags) would each have to be separated out into individual variables, the contents of the post deleted, and then each variable re-pasted (on their own lines) into the editor window.

If there is a way to do that, that would be great as it would mean only one macro would work for all posts, regardless of the number (or absence) of hashtags, but only if you really feel motivated to work it out and it's not difficult for you, a regex user, to achieve. I honestly haven't got a clue and would feel I'm asking quite a bit for someone to script this up for me.

All the best,
Will

Hi Will,

I'm not 100% sure this macro will work for you since your version of KM is several versions older than the newest one that I used to make this, but at least in my testing it seems to format the post how you want and paste it all at once. Once you've clicked in the text field, run this macro and it should select all the text (body, URL, and hashtags), copy it to the clipboard, use regex to sort each part of the text into variables just as you theorized, then assemble the post with the body, URL, and tags on their own lines regardless of how many tags there are following the URL, and paste the whole thing back into the text field in one go:

10)m WHC - Format SocialBee Feed Post (Regex).kmmacros (4.6 KB)
image

Results
26%20AM

And don't worry, I don't think this took me more than 20 minutes including the time to write this post, so it was far from an onerous request :slightly_smiling_face:

1 Like