Subroutine to Extract a Number of Lines from the Start of a List of Items

Purpose

This subroutine extracts a specified number of lines (chunk size) from the start of a list held in a text file. Those lines are returned to the calling macro.

Additionally, those lines are removed from the start of the list and, optionally, they are appended at the end of the list after which the list is written back to the file.

Diagrammatically

This shows the list (a, b, c, d, e, f, g) before and after a single call with a chunk size of 3.

Starting List Consume: 'No' Consume: 'Yes' Returned Chunk
a d d a
b e e b
c f f c
d g g
e a
f b
g c

Inputs

Local__ListFile - The path to a text file containing a list.

Local__ChunkSize - The number of lines to be grabbed from the list (the "chunk size") each time the subroutine is executed.

Local__ConsumeList - Whether the input list should be consumed.

Output

  • The chunk size number of lines from the start of the list.

Operation

Each time the subroutine is executed, Local__ChunkSize number of lines are removed from the beginning of the list. If Local__ConsumeList is set to No or N those lines are appended at the end thus creating a "circular" list; otherwise if Local__ConsumeList is set to Yes or Y the list becomes shorter with each call and is "consumed".

Those Local__ChunkSize number of lines are returned to the subroutine caller and the new list is written back to the original file.

Note: if this subroutine is called repeatedly with Local__ConsumeList set to Y or Yes, the list will eventually be completely consumed; this will not generate an error but the returned string will be empty and that can be tested for in the calling macro.

Error Checking

Error checking is performed on the input parameters. Should an error be detected a message is displayed in a text box and the macro execution cancelled.

Credits

Inspiration for the structure of this subroutine and the regex within it came from Peter Lewis's posting to the KM Forum at: Remove the top line of a text file or variable - #6 by peternlewis

This subroutine was derived from a solution presented in the KM Forum at [Solved] How to Open X Number of URLs at a Time in Order From a Master List? - #3 by tiffle

Subroutine here:
Download Macro(s): [SUB] Get Chunks From List .kmmacros (52 KB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

Testing
Here is a macro designed to enable you to test the usage of the above subroutine:
Download Macro(s): Test [SUB] Get Chunks From List.kmmacros (40 KB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

The test list is here:
Test [SUB] Get Chunks From List.txt.zip (738 Bytes)

2 Likes