Find character/text in Firefox Input field, select/delete everything following this char in this line, and find next occurence after that, repeat

Hi all! Sorry for the long topic line but I don't know how to describe that in a short way.
I've got an input field that is similar to that:

screenshot_2024-02-19_17-35-11

I'm looking for a way to find the first occurence of the pipe '|' with one keypress. Everything after the pipe is selected (or deleted), the cursor will be put right there, I'll than change the content manually. On another key press, the next '|' is found, the content after it selected/deleted, curser is positioned after the pipe and so on. Is that possible in Firefox?

I was thinking about an 'Edit' - 'Find in page' Menu action but what this can't do is putting the cursor right where I want to see it (and it does not select the text after the pipe). The pipe is always at another place, so OCR is perhaps not an option here (or is it?).

The closest thing I've found in this forum is this but I don't think it will fit my needs.

I'd really love a solution for Firefox since this is the standard work environment.

Thanks a lot in advance for any help!

EDIT: I've also thought about a Firefox Extension called "Edit in Sublime Text" which allows me to edit the form in Sublime Text or Notepad++. Maybe this is the way to go?

I don't know exactly what you're trying to do, but could you capture that text to a KM variable, then work with it in KM, and paste back when done? If so, that'll be much simpler than trying to work with it directly in Firefox, I think.

The thing I don't understand about your instructions is this: Your first step deletes everything after the first pipe. But then you say you change the content manually and find the next pipe. But there won't be another pipe, because you deleted all of them. Or does the content you add also have pipes?

-rob.

Ah, sorry: What I want to do is to edit the content after the pipes line by line. So I put the cursor there, edit the content behind it (I'd prefer to select/delete every character before the next word break, but if the whole line after the pipe is selected I would not care) and then jump to the next occurence of a pipe and do the same in the next line, then jump to the next etc. And important: the pipe itself shall not be deleted, it's all about the content behind the pipe. (The pipe marks an OR condition here).

Lines are never equal, so "take everything behind a pipe and change that to %y" is not an option. This needs to be done by hand.

The content I add usually does not have pipes.

I'm still a bit unclear on "line by line, specifically, what do you do with lines with multiple pipes, as in your example?

-rob.

I want the cursor to jump to the position after the first occurence of a pipe in a line to edit the text after each pipe. It does not matter much if all characters after the pipe in the same line are selected/deleted or just those before another pipe in the same line (this is more a regex problem than anything else I guess?). Two pipes in one line do happen but are rather rare. And if we don't think Regex here, then OPT-Shift-Left Arrow (to select the next word) would be fine.

So:

1st step of the macro: find first pipe -> put the cursor right behind that -> OPT-Shift-Left Arrow -> then let me edit the text
and when I execute the macro again/2nd step: find the next pipe after the cursor/the last position -> put the cursor right behind that -> OPT-Shift-Left Arrow -> then let me edit the text
... and then I'll repeat that until the last line/pipe.

Is that understandable?

I was also crawling the Sublime Text docs in the meantime but didn't find a function that let's one select text/strings after a specific string.

Thanks for clarifying. The thing that makes this tricky is your desire to jump to the next pipe while editing, i.e. you need true interactivity while the macro is running. In addition, changes to the text have to be represented in real time.

I can't think of any way to do that, but I can see a way you could step through it, line by line, and have the final text assembled when done. Is it important that you be able to edit everything after the pipe, or just that you be able to insert new text there?

And I think I'm still unclear as to which of these you prefer:

β€’ After the pipe, select (or delete) all text to end of line
β€’ After the pipe, select (or delete) only text until the next word break.

Which one is it?

-rob.

I see.

β€’ After the pipe, select (or delete) all text to end of line
β€’ After the pipe, select (or delete) only text until the next word break.

In most cases this will be the same - just one variable in the form of %something% after the pipe. There are no "ordinary" word breaks as well since words are separated by an underscore.
I don't have a strong preference here but let's say "mark everything until the end of the line" would be a slightly better fit for my use case. And BTW: select is better than delete because sometimes the text will stay the same and I'll just skip that and jump to the next occurence. And it is important to be able to edit everything after the pipe: when I process these lines, the text after the pipe will either
a) remain as is or
b) be changed to something different.

(So I'll most likely never just add anything to the text after the pipe.)

Please excuse me if I explain things more complicated then they might be. English is not my first language and I'm not quite sure about the right approach to tackle this task. I'm grateful to you for your time and effort, Rob!

Very probable that I am only not understanding the task at hand here, and not by my Mac to test it out now, but to me it sounds a bit like like:

Find Next β€˜pipe’, select all text after selected β€˜pipe’ until end of the line, (edit text here), before searching for next β€˜pipe’ after this, and so on

I am probably misunderstanding and thinking this too simplistically, but could this not be largely solved through these operations:

cmd+f β€” searching for β€œ|” or β€œ | β€œ
right arrow β€” (once or twice), to step in after β€˜pipe’
cmd+shift+right arrow β€” to select until end of the line
edit text here
cmd+g β€” to go to next found β€œ|” or β€œ | β€œ

Thank you, Alexander. I tried something like this (the 'Find' menu in Firefox). The problem is that I don't have a cursor or cannot just search within the Input field. "right arrow" is not working here. This works "visually" but I'd have to click in the right position to be able to edit the text.

Okay, I see, then I was indeed thinking it too simplistically. It quickly gets more complicated then

Unfortunately. It's also not possible to jump to the next position. This function seems to be made for reading in Firefox, not editing.

But I mentioned the "Edit with Sublime Text" extension in the first post. I didn't try this approach there, I think it should work in an ordinary text editor and since the extension can AFAIR be activated with a keyboard shortcut, it should be automatable. I'll try that tomorrow! But I'm also curious about the workflow @griffman has in mind. :slightly_smiling_face:

1 Like

Color me surprised, but this actually works and didn't feel nearly as janky as I thought it would :).

Download Macro(s): _Firefox line processing.kmmacros (19 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 13.6.4
  • Keyboard Maestro v11.0.2

Though it does come with a number of caveats, as it's more a proof of concept than something that will just drop right into your Firefox macro (but it shouldn't be too hard to integrate).

One of the issues is that various editors respond differently to various word/letter selection shortcuts. I decided to use BBEdit (which has a free version, if you don't have it yet), as it has very robust support for such shortcuts. So that's Caveat #1: This macro requires BBEdit, and may not work without editing in other editors.

Relative to BBEdit, I didn't write any logic into this demo macro at all, which leads to the related BBEdit Caveat 1a, which is that you need an open blank BBEdit document before launching the demo. You could easily add some logic to handle this, but I didn't do it in the demo.

The way the macro works is that it takes the chunk of text (from Firefox in the "real world," from a variable in this demo), and reads it a line at a time. If the line doesn't contain a pipe symbol, then the line is just added to the "edited output" variable.

If it does contain a pipe, it's pasted into BBEdit, and the remaining characters from after the pipe until the end of the line are selected. The macro now waits for you to press the Escape key. Type whatever you want to edit the text, but don't hit Escape until you're done.

When Escape is pressed, the edited text is added to the variable containing the full edited text, and the loop repeats with the next line, until all lines have been processed. Somehow, I actually managed to write something that does select each and every "next pipe" with just one keystroke :).

Caveat #2 is that this macro is not ready to use in the real world. It assumes you have the text from Firefox already on the clipboard, and it doesn't try to paste the edited text back into Firefox at the end (but it does put it on the clipboard).

Caveat #3 is that it's only been tested in Keyboard Maestro v11, but it should work fine in 10 as well. It also may be somewhat computer speed dependent, so if things don't seem to be working, you might have to insert some Pause actions in various spots.

And finally, Caveat #4 is that the macro doesn't do much to clean up after itself. It leaves the BBEdit window open, for instance. I do, though, clean out the clipboard as it's being used, so it won't fill your clipboard history file :).

Overall, I'm pleasantly surprised at how this turned outβ€”it feels pretty smooth to use, even though you're editing a line at a time. It will take some work to integrate into a total solution macro, but it should get you started (if you'd like to go this route).

-rob.

2 Likes

Excellent! Thank you very much. I'll dig through this in the coming days and see what I come up with. The mentioned caveats shouldn't pose any high hurdles, #3 is not even one at all since I'm already on v11.

Maybe I can also combine the extension with your approach. I'll post here once that is done. (And if anyone has an idea that doesn't require an external editor, i'm all ears!)

From what you described, you can't do much of anything in the Firefox text input box, which seems to make it nearly impossible to do anything without an external editor of some sort. I assume the page isn't public, so we can't see it?

Edit: You can test the demo as-is, it runs as a standalone. Integration only needed if you want to use that approach.

-rob.

I tested your macro this week and got it to work! The individual steps worked very well and I can imagine the final workflow very well. I also have the impression that it could be implemented with my favorite editor Sublime Text. It's an amazing approach! One drawback is that I only see one line of the final code at a time and the repeated opening and closing of the search window is quite jarring. (I'm not entirely sure, but I think at least that could be avoided with Sublime Text, since the search field is part of its main window.) This is of course due to the fact that Firefox does not allow direct editing in the input field in this way.

Today I had an idea for a completely different approach. The HTML title of the website contains some information that I need for the input field. Perhaps there are ways of converting these into variables using regular expressions - the title of the window can be easily read out with KM - and filling the input with corresponding shortcuts in this way. I may be able to reach my goal just as quickly, and the advantage is that I can stay directly in the input field. I will experiment with this a little.

I always find it exciting how many different ways macros can be implemented. KM has the right tools for an infinite number of approaches. It is an application that is absolutely unparalleled and it never ceases to fascinate me.

Thank you for your time and effort on this problem, Rob! I'm still not sure how I'll solve it in the end, but I've already learned a lot again and got to know functions that were previously unknown to me.

1 Like