I'm having a problem with a script that had been working. I've tracked down the problem with the "For Each Item in a Collection Execute Actions" with no actions being done. I've included the script that I based mine off of.
I'm running KM 9.2 and OS X 11.4
TIA
Paste Random.kmmacros (3.5 KB)
Hi, Dave
Picking the random line whilst inside the For Each
loop seems unreliable to me - wouldn't it be better to pick the line number first?
I was slightly surprised KM's amazing Filter
command didn't have a way to pick particular lines, so I think it does need a loop. But I'm hoping someone else can paste a more efficient way using KM...
... though it might be simpler (and faster for a large source file) to do this with a shell command (the second example below)!
Best wishes,
Steve
Alternative KM-style version:
Paste Random line.kmmacros (6.4 KB)
Or, as a one-liner:
Paste Random line.kmmacros (2.0 KB)
There is a shorter way to get a line from a list of lines by using AppleScript instead of a KM for loop.
Here the list LocaltheList
is set up and then the desired line Localtheposition
is set up. The AppleScript script is then executed and it returns the desired line in the variable Localtheresult
.
If you know AppleScript you can change the names of the variables to suit your own needs. Here are the 3 actions:
Get Line from List Actions.kmactions (1.6 KB)
Hope you find that useful.
2 Likes
OK, working with the suggestions here, I found the problem was actually in the first set variable to text action. I'm not sure if "Source" became a reserved word, but when I changed it to "localSource" it worked.
Thanks,
Dave
1 Like
Glad to hear that, Dave. That's odd - as my script also used a variable called 'Source' and didn't have that problem.
Btw, whilst I'd still recommend the 'sort -R
' solution for a large file, I've just realised that a loop isn't necessary for a native KM solution if you access the data using array notation. Just another alternative!
Paste Random line - array.kmmacros (4.2 KB)
To add to the mystery, your macro doesn't work on my computer unless I change the variable name, I just add "local" to the front of all of them and then they work.
I like this array method, but I have a question. What is the purpose of the "Join the source lines with a bar..." action?
Thanks,
Dave
That is odd!
(In case it's relevant, I'm using KM 9.2 on macOS 11.4)
I joined the lines as I couldn't see a way of using KM's "treat a string as a separated list of array elements" unpacking (in the "Extract" step) when the separator character was an (escaped) new-line character. As ever with KM, I'm assuming there's a cunning solution somewhere!