Exporting multiple plaintext files from the content of one existing text file

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:

Screenshot 2025-06-25 at 2.27.26 AM
Screenshot 2025-06-25 at 2.28.00 AM

Any advice is greatly appreciated!

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:

Test Script for saving text files 2.kmmacros (3.2 KB)

Image

2 Likes

( see also command line split )

1 Like

Thank you for the tip about the "How to Post/Upload" link β€” I tried looking for that before posting, but couldn't initially find it.

I've expanded the sample text to more accurately reflect the data I'm working with, since it's more than two lines per item:

β˜†β˜†β˜†

Number 1

  • Subtitle 1.1
  • Subtitle 1.2
  • Subtitle 1.3

Notes
"Some more notes"

  • Item 1.1
  • Item 1.2
  • Item 1.3

β˜†β˜†β˜†

Number 2

  • Subtitle 2.1
  • Subtitle 2.2
  • Subtitle 2.3

Notes
"Some more notes"

  • Item 2.1
  • Item 2.2
  • Item 2.3

β˜†β˜†β˜†

Number 3

  • Subtitle 3.1
  • Subtitle 3.2
  • Subtitle 3.3

Notes
"Some more notes"

  • Item 3.1
  • Item 3.2
  • Item 3.3

β˜†β˜†β˜†

Number 4

  • Subtitle 4.1
  • Subtitle 4.2
  • Subtitle 4.3

Notes
"Some more notes"

  • Item 4.1
  • Item 4.2
  • Item 4.3

β˜†β˜†β˜†

Number 5

  • Subtitle 5.1
  • Subtitle 5.2
  • Subtitle 5.3

Notes
"Some more notes"

  • Item 5.1
  • Item 5.2
  • Item 5.3

β˜†β˜†β˜†

The rest of the code in the macro is the same:

macro

Here's the macro:

Test script for saving text files.kmmacros (5.2 KB)

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. :blush:

@Nige_S posted a macroβ€”did you try that one?

-rob.

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:

image

...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)".

What isn't working when you run it yourself?

1 Like

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!

Do you know that macOS has Unix utility csplit, which is dedicated to this kind of tasks?

Just for notice other possibility.

2 Likes