Path Resolution and Variable Persistence in Nested For-Each Loops

Hi everyone,

I am working on a macro that dynamically discovers a directory structure and passes absolute file paths to an external Swift script for PDF processing. I am encountering issues with variable assignment and path resolution within my nested loops.

Ellen: Genie tries many different varieties of these nested loops. I get the feeling that Genie does not understand how they work. Since some folders are absolute and others change, it is confusing.

THE WORKFLOW:

  1. Loop 1: Iterates through a constant parent directory to find a dynamic 'Book Folder', assigning the path to Local_BookFolder. EllenM: The book folder has a permanent path. It contains another folder which always is given the title and author of the book.

Screenshot 2026-04-20 at 21.22.43

  1. Loop 2 & 3: Iterate through %Variable%Local_BookFolder% to identify a specific .pdf and .txt file, intended to be assigned to Local_FilePath and Local_toc respectively. Ellen: CleanTOC.txt is a permanent name. The pdf and the folder names change with different books.
  2. The Script: An "Execute Swift Script" action pulls these variables from the environment to process the "chunks".

THE PROBLEM:
The local variables are not resolving to the correct absolute paths. In my testing, Local_FilePath is frequently returning the parent directory path instead of the file path, or the variables are being "swapped" or lost during loop termination. I suspect my Break From Loop placement or the use of nested "If All Conditions Met" containers is causing the execution to skip the variable assignment or terminate prematurely.

TECHNICAL DETAILS & ERRORS:

Engine Log Error:
Action 100986979 failed: Execute a Swift Script failed with script error: ERROR: PDF file not found. Check if Local_FilePath is set correctly.

2026-04-20 21:29:55 Action 100986979 failed: Execute a Swift Script failed with script error: ERROR: PDF file not found. Check if Local_FilePath is set correctly
2026-04-20 21:29:55 Execute a Swift Script failed with script error: ERROR: PDF file not found. Check if Local_FilePath is set correctly. Macro “02 Chunker_w/ swift Ellen” cancelled (while executing Execute Swift Script).

Variable Debugging (Current Output from Display Window):

  • Local_BookFolder: /.../TOC_container/Clinical Guide Klein
  • Local_FilePath: /.../TOC_container (Incorrect - resolving to parent)
  • Local_toc: /.../TOC_container/Clinical Guide Klein/BookName.pdf (Incorrect - resolving to PDF instead of TXT)
    Folder: /Users/ellenmadono/Downloads/workspace_chunk/TOC_container/Clinical Guide Klein PDF: /Users/ellenmadono/Downloads/workspace_chunk/TOC_container TOC: /Users/ellenmadono/Downloads/workspace_chunk/TOC_container/Clinical Guide Klein/CLINICAL FOCUS GUIDE VOLUME 1 (LOUISE KLEIN R.S.HOM.).pdf

I have attached the .kmmacros file.02 Chunker_w- swift Ellen.kmmacros (21.1 KB) Could someone review the nesting of my If/Then containers and the Break From Loop logic to ensure the variables capture the absolute paths correctly before the script executes?

Thank you for your help!

You haven't nested your loops -- they are three different, sequential, loops.

Nested loops would look like:

Here they are as a macro that also display the output. Run the macro and compare the output to the Actions in the macro to follow the logic of the nested loops:

Nested Loops Demo.kmmacros (4.9 KB)

Why are you looping? Are you planning to point this macro and process every unprocessed TOC file in one go? Or are you just trying to find the first unprocessed TOC then pass that to your Swift script?