Hi all,
Short version: I want to split a large PDF into chapters by calculating page ranges from a text file, using native Mac/KM tools instead of Python. Is this possible?
Longer version:
I have a working PDF chunking system built with Python and shell scripts inside Keyboard Maestro. It does exactly what I need, but I'd like to rebuild it using native Mac tools and KM actions where possible β following advice I've gotten here before about preferring KM actions over shell scripts.
The context: I work with large reference books (500β1400 pages) in PDF format. Each book is organized into chapters, one per subject. My goal is to extract each chapter as its own PDF file, named after the chapter title.
What I am asking:
The step I don't know how to do natively is: 1. taking a source PDF and extracting a specific page range (say pages 105 to 203) into a new named PDF file.
In Python we used the pypdf library for this. Is there a KM action, an AppleScript approach, or a Mac-native tool that can do the same thing β given a source file path, a start page number, an end page number, and an output file name?
Can you suggest tools from KM or that are native to Mac.
Here's what the system currently does:
I prepare a Table of Contents text file in advance. Each line looks like this:
[[Chapter One]] ((45))
[[Chapter Two]] ((112))
[[Chapter Three]] ((203))
The double brackets contain the chapter title. The double parentheses contain the page number from the book's printed table of contents.
The challenge is that printed TOC page numbers don't match physical PDF page numbers. A book typically has front matter (title page, foreword, preface) before Chapter 1, so the physical PDF pages run ahead of the printed numbers. This gap β which I call the offset β is consistent across the entire book.
I find the offset by opening the PDF in PDF Expert, navigating to the first chapter, and reading the physical page number it shows there. I enter that one number into a KM prompt at the start of the macro.
What the macro does:
- Prompts me for the offset (one number)
- Reads the TOC text file line by line
- For each chapter: calculates start page = TOC page + offset, end page = next chapter's TOC page + offset β 1
- Extracts those pages from the source PDF and saves as a new PDF named after the chapter title
- Outputs all chunks into a CHUNKS_FINISHED folder
Currently steps 2β5 are handled by a Python script called from a shell script in KM. The KM macro handles the prompt and passes the offset to the shell script.
I also have safety checks: verify the TOC file exists, verify there is exactly one source PDF in the folder, verify the offset was actually entered.
To be concrete about where we are technically:
Steps 1β3 I believe I can handle with KM native actions, based on a working macro I already have:
- Reading a text file line by line β For Each Item in a Collection (lines of file)
- Parsing each line for title and page number β Search Variable Using Regular Expression, capturing two groups
- Calculating physical page numbers β Set Variable to Calculation (TOC page + offset)
Repeating my question:
The step I don't know how to do natively is step 4: taking a source PDF and extracting a specific page range (say pages 105 to 203) into a new named PDF file.
In Python we used the pypdf library for this. Is there a KM action, an AppleScript approach, or a Mac-native tool that can do the same thing β given a source file path, a start page number, an end page number, and an output file name?
Thank you,
Ellen Madono




