Thanks @tiffle. I haven't dug into @ccstone's post enough yet to answer your question. He does have a link to the Late Night Software forum as a resource for AppleScript, which may prove to be invaluable.
While I imagine I may eventually have wide ranging uses for being able to automatically modify or generate KeyBoard Maestro macros, my immediate desire is this:
I typically have 21 desktops between my MacBook Pro monitor and my external monitor. Email, Facebook, Money and Insurance, Health, Project Planning, Research, Video watching, Archiving, as well as several dedicated to specific projects, sometimes two for a multifaceted project.
KBM has totally solved my issue of being able to have enough hotkeys to switch between them. Using the free app CurrentKey Stats I assign hotkeys to my most commonly used ones (I suppose I could use KBM, but CK was there first) and for everything else I a KBM hotkey that pops up a menu of the desktop names.
Virtually every task management app that I have tried, One Note, Evernote, Trello, OmniPlan, etc. has a severe drawback in this environment in that when I have to reboot the system, all the open TMA windows end up on the first desktop after a reboot. So originally I was thinking of ways to automate moving them all back where they belong. But that's not necessary at the moment.
I recently discovered that TextEdit windows will automatically be reopened in their original desktops after a reboot, so I'm now working on having Status and To Do files in TextEdit in most desktops to keep track of what's in progress and what's next in the various contexts.
I am looking at various housekeeping ideas where the same macro would have a different effect, depending on which desktop was active when it was invoked. For instance, my Desktop Status macro would determine which desktop I was on and open the appropriate TextEdit file if it was not already open, and move it to the appropriate position on the screen.
Since I might need the triggered macro to choose between 21 possibilities, there are likely to be blocks of KBM code that get repeated 21 times with only minor variations. Having a macro to do those edits might be very handy.
Since I sometimes rearrange the order of desktops to make moving files back and forth simpler, and the names sometimes change and my usage evolves, I can't count on the ways to tell the difference between desktops being fixed in stone. If they need to be hardcoded at any level, and think they might be, I would like to be able to in some way automatically massage them to update them as needed. At the very least, I'd like to run the existing macros through a comparison with the current list of desktop names to determine which macros need updating, and where.
For instance, just today I change the name of my Projects desktop to Project Planning. I need to update my KBM hotkey menu to match. I'd love to be able to run a macro to do that updating. All the info needed is right there, accessible to macros through scripts and CK's AppleScript hooks, but for now I have to do it by hand, taking dozens of thoughtful steps.
This is all future planning. Most of this hasn't been built yet, but the more I enjoy having different Status and To Do lists available on each desktop, the more I expect to want to be able to automate massaging macro definitions.