I don’t know if you saw it or not, but someone had created a script that accomplished something very similar to what you wanted to over a year ago. The script is a little bit more complex because it uses “Day One” CLI, but you can give it a good look. Here is the link. It was on the “Script Extensions” page of the Wiki. I believe that all of the posts that discuss code or scripts are eventually cataloged and posted there.
1 Like