Substitute in a List Lines of Text in Sequence, Two at the Time, Generating New Lists. (Updated for Clarity)

Updated for clarity

I’m trying to process a list, substituting in sequence all lines with ... (dots) except two lines, thus generating a new list, pasting the processed list, and repeat the process on the original list substituting all lines except the next two lines and pasting, unti the end of the lines.

Process and goal:

To take a text list of n lines and produce n lists, which are copies of the original list, but with lines substituted by ... using these rules:

  • the first list keeps the first original line and substitutes the rest.
  • the second list keeps the 1st and 2nd original line and substitutes the rest
  • the 3rd list keeps only the 2nd and the 3rd original line and substitutes the rest
  • the 4th list keeps only the 3rd and the 4th original line and substitutes the rest
  • the nth list keeps ony the nth-1 original line and the nth original line and substitutes the rest

Example

The following example represents all cases, in this istance the lines of the original list are 5:
Starting from this original list:

Line1
Line2
Line3
Line4
Line5

I would like the following result (5 lists):

Line1
...
...
...
...

(Optional tab keystroke)

Line1
Line2
...
...
...

(Optional tab keystroke)

...
Line2
Line3
...
...

(Optional tab keystroke)

...
...
Line3
Line4
...

(Optional tab keystroke)

...
...
...
Line4
Line5

I'm pasting the resulting lists in fields of a software, so ideally between each processed list I would simulate a Tab keystroke, so that the following processed list would go in the next field of the software.

You can see here a screenshot of what I need to do with the processed lists

I’m playing with the “for each” action but I suspect I need to store the initial text, substitute each line with ... and then substituting specific lines, in order, with the corresponding lines from the initial text.

I don't understand how to "address" specific lines (for example the 3rd and the 4th) in the original list

This is what I've been able to come up with, so far:

Can you help me set up this?

First, I'd like to thank you for the excellent requirements spec.
:+1:

This made a complex task easy to understand, and therefore to produce.

I believe this macro should do a good job of getting you started.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example Output

image

MACRO:   Create Multiple Lists from Source List [Example]

-~~~ VER: 1.0    2020-09-21 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Create Multiple Lists from Source List [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Create Multiple Lists from Source List

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. For testing, just trigger this macro.
  3. For Production, enable and disable the magenta Actions accordingly.
  4. Select the data that is your source list.
  5. Trigger this macro.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • Enable and disable the magenta Actions accordingly.
    • SET Source String -- DISABLE for Production
    • SET Text for Other Lines
    • DISABLE This Action for PRODUCTION
    • ENABLE for Production -- REPLACE Above Append Var with YOUR Actions to Process New List

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @Lists @Example @ForEach

REFERENCE:

Topic Title: Substitute in a list lines of text in sequence, two at the time, generating new lists. (updated for clarity)

URL: Substitute in a List Lines of Text in Sequence, Two at the Time, Generating New Lists. (Updated for Clarity)

Post Date: 2020-09-21
Posted By: kbdmstlily

First of all, thank you so much, I would have never figured out how to count and refer to specific lines in the input.
I still need to study the macro to understand how it works, but it works!

I didn't really understand the Title of the last group action in the macro you provided:

REPLACE Above Append Var with YOUR Actions to Process New List

My guess is:

you (user) REPLACE the Above action, (which is "Append Var") with your action, to Process New List.

Test drive

I needed to make a couple of addition to make it work for production, please let me know if this is correct.

I enabled the last group but then the macro didn't paste the processed lists, it pasted the original list, n times, where n was the number of lines in the original list.

Then I noticed the Paste action at the end and I thought it might have been pasting from SystemClipboard which still contained the original list.

So I added an action to put the processed list in a named clipboard and another action to paste from it.
it works now.

You can see the paste action from your version disabled and, in green, the actions I added.

Please let me know if my line of thought is correct or I missed something to set the macro up for production, and thanks again.

If I'm correct I or you could upload a updated version and mark it as solution

I tried to expand the scope of the macro including a title line that would be repeated in all processed lists

Source list:

Title line
Line01
Line02
Line03
Line04

Processed lists:

Title line
Line01
...
...
...

Title line
Line01
Line02
...
...

Title line
...
Line02
Line03
...

Title line
...
...
Line03
Line04

My method:

First I added the green action:

  • to take the first line (and empty lines) and put it in a variable.
  • to remove that fist line and process the list as per original macro.

Then I tried to prepend the result of each iteration to generate processed lists with the variable containing the title.

The strange part is that appending that title works:

The result of this (prepend function active):

is:

but the result of this (append function active):

is:

where you can clearly see the Title line added at the end of each processed list

I really don't understand why...

This is the macro:
ClozeOverlappingFromList__Prep with header.kmmacros (21.9 KB)

The below update does two things:

  1. Handles the "Title line" properly
  2. Is setup as best I know to work in your production workflow
    • Note: You do NOT need to use a Named Clipboard to store plain text results.
    • In this case, a simple Local Variable will work fine. You just use the Insert Text by Pasting action.

Example Output

(uses BBEdit to simulate your target app)

image

Note that the TAB, which in your production app moves to the next field, shows up as a TAB character in BBEdit.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Create Multiple Lists from Source List With Title Line [Example]

-~~~ VER: 1.1    2020-09-22 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Create Multiple Lists from Source List With Title Line [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Create Multiple Lists from Source List

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Open your Target App, and click in the FIRST field to received the pasted list
  3. Select the data that is your source list.
  4. Trigger this macro.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .
  • ==REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:==
    (all shown in the magenta color)

    • Enable and disable the RED Actions accordingly.
      • CHANGE to App Where You want to Paste Results
      • SET Text for Other Lines
      • Pause for 0.2 Seconds
        • Adjust as Needed to ensure Page is ready to receive paste.

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @Lists @Example @ForEach

1 Like

Thank you very much, it works perfectly and provides with lots of stuff to study.

Can I ask you why do you think my approach (quoted below) didn't work?

It looked like a bug, honestly. Prepend and Append should behave in the same general way, only where text is added changes...

Sorry, don't have time right now. You might compare your marco with mine to see if that offers any insight.