@peternlewis I'd like to have either a Filter option, or a Token, to return the "nth" line from a multiline variable.
If you already have something like this, please let me know, however I did do a lot of searching before I posted this, because it seemed a fairly obvious thing.
But, assuming you don't already have this, I'm guessing not many need it or they would have already asked for it. Still, if you're bored sometime...
Yes, but I suppose I've never come across that situation in my Macros.
But if I did (and only because I am scared of Regex) I would probably do a series of Keyboard Maestro search are replaces one after the other:
First one to replace commas with some weird unused character like ★, second search to replace line returns with commas. Then I could get the ★ sign back to a comma when accessing the nth bit of data.
But I'm sure there is a more elegant Regex or JSON solution. Either way it's a trick I've used often - usually the other way around (the Variable is comma separated but I want to display it as multi-line).
Let me tell you my experience with regular expressions (which I will now abbreviate as "regex").
I'm retired now, but I started programming professionally in 1979. But even with all that experience, I stayed away from regex until, I don't remember exactly, but around 6 years ago.
I could slap myself for waiting so long.
I think just about everyone agrees that the key to learning regex is to try to use it as often as possible. So I had to make a commitment that I would try to figure out how to do things in regex as much as possible. And it worked.
So I encourage you to do he same. It's head-bangingly obtuse at the start, but it gets better.
But you can use any character you like since you’re just creating a variable array with a custom delimiter. I usually use a § which is probably a character I’ve never seen under normal circumstances. You can then continue to use the KM method of array addressing with no need for regex at all.
I do agree though that a filter built-into KM would be much more convenient.
I'm going to be making a lot of use of this. Especially as sometimes it is more useful to have the
Variable "Array" delimited by | instead of commas.
one|two|three|four can work in a Custom User Prompt Dropdown List when one,two,three,four doesn't. By having the custom delimiter as | I won't have to keep converting the Variables from one form to another.
You guys with your 3-action solutions. Lazy programmers do it in a single line of code, like this:
It's a matter of opinion whether a "line" of text includes the trailing \n, but in this case I assumed it does. If you want to exclude the \n, I'm sure you can modify this statement.
P.S. I have used this technique in macros before.
P.S. I should have posted the macro in this thread. But since I've deleted it, I'll retype the important line for everyone here:
I think you already spend too much time here. (Kidding. I think.) I mean, I'm spending a lot of time here right now, but I'll be getting back to real life as soon as I finish up one more KM project. (Suuuuure...)
Oh boy. Fire me. Wait, you can't. I'm sure I've done this hundreds of times before. I just had a mental block.
A few days ago I was explaining the difference between a calculation and a string to someone. I said I make this mistake once in a while. Today is such an example.