I ended up writing this script with @ryanjamurphy’s comment as a sketch. It’s not exactly what you want since it doesn’t even use the front matter but I thought I’d post it here nonetheless since it was inspired by this thread. I also wrote it as an Alfred workflow instead of a plain AppleScript.
You can easily take the AppleScript from the workflow and modify it to your needs.