I've got a handful of large plaintext files that include dozens of entries each, and I'd like to export each entry to its own individual text file.
In the sample file I posted below, there are five entries separated by star icons β another portion of the KM script creates these as a kind of delimiter to break up the file.
Unfortunately when I run my script, it only exports one file for the first entry and then stops. If I display the text in a pop-up window, five windows do appear, but they all have the same contents (that same first entry).
So it seems that my regular expression search is finding five distinct entries, but I'm lacking something in my script that is keeping the loop from completing as it should, and it's simply iterating on the first entry over and over.
Here's the formatting of the text:
βββ
Number 1
Subtitle 1
βββ
Number 2
Subtitle 2
βββ
Number 3
Subtitle 3
βββ
Number 4
Subtitle 4
βββ
Number 5
Subtitle 5
βββ
Here is the Keyboard Maestro script I'm using, in its current state:
Upload your actual macro, not screen shots -- you'll find instructions at How to Post/Upload your Macros and Scripts. Not only does that save us from having to type out wacky regexs (lots of opportunities to make a mistake!), we get to see any options you may have set in the actions.
While complicated regexs are fun, multiple simpler actions are often easier -- in this case I'd probably use a "For Each: separated by βββ" to get each chunk, then process.
All that said -- the problem here is that your "Search" action inside the "For Each" is searching the System Clipboard again and again, finding the first match. You actually want to search the loop's current item, the variable fullText.
FWIW, here's my version -- as set it'll write the files to the Desktop:
I played around with the two suggestions made so far β "For Each: separated by βββ" and using the Split function β but neither brought me success, since I've obviously still got something misconfigured.
Your macro is still searching the System Clipboard on every "For Each" loop -- you need to search the fullText variable instead.
But your regexs are wrong -- tested on regex101.com against your new text there are no matches for the "For Each" to work on. Probably because
((.+)(?:\n)(.+))
...limits you to "one line of text, a newline, one line of text" between your star-and-newline delimiters.
My macro works fine on your new data -- it doesn't care how many lines are in each "chunk", just that the first line is "title" to use in the file name:
...where the token can be read as "Treat the text in Local_chunk as an array whose elements are delimited by \n, and give me the first element (the first line)".
Your macro did work, thank you! When I tried integrating it into my larger script, I think I had a typo in the file name field of the Write Variable call, or had perhaps left out the \n section. Working great now β thank you so much!