Batch Process Lines in Variable?

Am I taking the scenic route here?

Let's say I have a variable that contains a list of URLs I want to do something with, a few at a time. Is this the most efficient way to do that? I'm sure I must have missed something simpler along the way. In particular, the Search and Replace actions (in the purple group) seem like a long-winded way to remove a line from a variable, mid-process.

Batches test.kmmacros (27 KB)

Macro screenshot

Why clear them at all? Why not just count your way down the list, by either loop or "For Each", just as we did in the "list to columns" exercise?

You're always removing the top line of Local__URLs, right? Instead of a search and replace, I'd do it with sed, like this

It doesn't really reduce the length of the macro, but it may make what you're doing more obvious.

Also, I'd strip out the blank lines before starting the loop.

Let's say it opens the first three, then does something with them, how would it continue from the fourth?

I'll give that a shot. Thanks!

Any advantage to using sed over regex?

↑Edit: I can answer that myself; regex fails to remove the final line for some reason.

Think of it like working your way through a box of index cards. You could take the first card (or 3 cards) out, use them, discard them, then go back to the box -- that's your approach above, always referring to the frontmost card(s). Or you could flick through the box leaving your finger in to keep your place every time you stop to process - that's the indexed approach I'm suggesting.

Both approaches are valid in different situations. When you are only using a list (array) once like here then removing items is good if that's supported. But KM doesn't seems to have a simple, native, way of "popping" items from lists/arrays, which is why I'm suggesting using an index.

Enough waffle -- demo time!

Indexed Batches test.kmmacros (23.8 KB)


Glad to know I'm not a total dunce, and that this does require a bit of thought!

Your macro works great! I think I understand your logic, but I'm trying to get my head around the use of square brackets and and the inclusion of the separator here:

Presumably the square brackets refer to a line in an array, like [1], [2], [3], and [4] do when accessing coordinates from a token or variable, but what I don't get is this is no longer a multi-line variable, so... How does that work and why does ,KMSEP, need to be included explicitly when the rest of that entry (the URL itself) doesn't?

Me too, if I'm honest!

As I understand it:

  • You can access elements by index, using [aNumber] -- so accessing element 3 is [3]
  • The default item delimiter is , but if you are using something else you put it after the ] -- if we were using a .-delimited array, accessing element 3 would be [3].
  • You can't use Return as an array delimiter (here) so we replace every Return in the original list with something that will not be found in the text otherwise -- ,KMSEP,
  • So, after replacing the Returns in our list, accessing element 3 would be [3],KMSEP,

Putting that altogether with our variable name and the variable, Local_i, that holds our incrementing counter gets us:


So each turn of the loop accesses "element i of the ,KMSEP,-delimited list held in Local__URLs".

At least, I think that's what's going on!


Looks like you're on the money!!

I can imagine that's going to be quite handy in future. I might save a few actions as a favourite. :+1:t3:

One of the many posts I've read in an attempt to go more KM-native instead of snuggling up in my AppleScript security blanket. Meanwhile, @tiffle's trying to tempt me back to my old ways!

When @noisneil opened up the question

I very nearly piped up about using KM arrays with custom delimiters but then it got really complicated with CSVs, JSON, JavaScript... so it's nice to see the subject come up again but this time from the (busy) desk of @Nige_S :wink:

1 Like

In my (limited) experience with Applescript, I've found KM to be a fair bit snappier. Doesn't matter a lot of the time, but I've often wondered why that is. Could be just UI interaction that's given me that impression, as that's mostly what I've used AS for.

1 Like