Best way to automate Keynote to PDF file saving

Hi there. I am pretty new to automation. I have used Hazel and simple Keyboard Maestro macros, but I have a new task that I would like to find the right tool for. I am a professor and I use Keynote to create short slide presentations used in some of my classes. At the end of the class I usually print the Keynote presentation to PDF and post it on the class LMS. The tricky part is that I prefer to post these notes in either the handout or grid format. If I am doing the grid, I like to do two slides to the page, but then need to reset the page orientation to portrait to maximize the size of the slides on the page. All this leads to quite a bit of fussing around before I can hit the final save as PDF function. I think that using either an Apple Script or Keyboard Maestro Macro with Apple Script in it would probably get me headed in the right direction to automate this process, but I am a bit hesitant on how to start. Can anyone point me to a similar workflow somewhere to learn from or make a suggestion as to a way to tackle this? Thanks for your thoughts!

Here’s how I’ve done it:

Applescript UI scripting tends to be fragile - let me know if you have trouble getting them running and I can probably help - I haven’t tried the scripts since the new version of Keynote came out last week.

Thank you - this looks most helpful!

So after playing with the applescripts, I ran into a couple of issues with the current edition of Keynote working with the scripts. Then I got busy and forgot about it for awhile. Then the Keyboard Maestro Field guide came out and I got wild with ambition to try doing this with Keyboard Maestro. Here is what I managed to get running.

Looks fragile - won’t the coordinates vary depending on the size and location of the window ?

It might very well be fragile, since the mouse clicks depend on measurements from the edge of the pop up window each time. I’ve tried it with the Keynote application window filling the full screen and half the screen. It works both ways for that on my computer. I usually snap the software I am working with to either full screen, right half, or left half of my 15” MacBook Pro. If you tried it with a different sized monitor I am not sure that it would work. I would be interested to know if that is indeed the case. So far for my machine, in my limited use case, it has held up, but I wouldn’t be surprised if it broke once it was tried on a different machine with a different screen resolution for instance.