Pragmatism in the real world

Automatically converting PDF to Keynote

I use rst2pdf to create presentations which provides me with a PDF file. When it comes to presenting on stage, on Linux there are tools such as pdfpc and on Mac there’s Keynote.

Keynote doesn’t read PDF files by default, so we have to convert them and the tool I use for this is Melissa O’Neill’s PDF to Keynote. This is a GUI tool, so I manually create the Keynote file when I need it which is tedious. Recently, with Melissa’s prompting, I realised that I could automate the creation of the keynote file which makes life easier!

I use a Makefile for this and this is the target & relevant variables:

# Name of PDF to create
pdf = my-presentation.pdf

# Aspect ratio of Keynote file
aspect_ratio = 1280 x 720

# Determine the name of the Keynote file
key = $(pdf:%.pdf=%.key)

# "make keynote": creates the Keynote file using "PDF to Keynote"
keynote: $(pdf)
	rm -f $(key)
	defaults write net.clawpaws.PDFtoKeynote presentationSize "$(aspect_ratio)"
	defaults write net.clawpaws.PDFtoKeynote autoSaveAfterOpen 1
	defaults write net.clawpaws.PDFtoKeynote autoOpenAfterSave 0
	defaults write net.clawpaws.PDFtoKeynote autoCloseAfterSave 1
	open -a /Applications/PDF\ to\\ to\ Keynote "$(pdf)"
	osascript -e 'tell application "PDF to Keynote" to quit'

The nice thing about PDF to Keynote is that it has preferences to automatically create the Keynote file after a PDF file opened and to automatically close the PDF file once saved. We can also programmatically set the aspect ratio. To do this, we use the defaults command line tool to set up PDF to Keynote the way that we want.

We then call open -a to open PDF to Keynote with the PDF file as the argument which then automatically creates the Keynote file and stores it into the same directory. The PDF file is automatically closed for us too.

Finally, we can use AppleScript via the osacript command to quite PDF to Keynote. I’m not sure if we need to wait for the conversion to happen before we quit, in which case, we can add sleep 3 if we need to.

That’s it. Automatically creating the Keynote file vastly improves my workflow and I no longer have to think so much about it.

Update: Note that I changed the default write commands for the booleans as they need to be 1 and 0, not “YES” and “NO”…