Welcome @spacehorse
This is actually easily accomplished via a smart rule with no programming required.
- This excludes subgroups so it doesn’t match already filed items.
- Scan name matches the asterisk and ignores everything else.
- Control-click the text field in the File action and choose Insert Placeholder > Document String to use the matched string.
These documents were filed with this smart rule into their respective groups…
I tried to get chatGPT to write an Applescript but it didn’t seem to work
While this may seem reasonable, I would recommend against it, especially as you essentially learn nothing from it and is likely incorrect or not optimized.