Keynote Javascript to export to 4 slides to pdf?

Hi Everyone,

I have this Javascript someone shared from the old Facebook group (thank you whoever you are!). It will create a 3 slide handout pdf from a keynote file, does anyone know how to modify this to export 4 slide handout pdfs?

On a related note, does anyone know how to modify this to export as grid of slides in the pdf and change the number of slide per pdf?

I use this to automatically create handouts for my students, one student asked if she could have more slides per page to save paper.

function convertToPDF(kn, input) {

var inFile = Path( input );

var outFile = Path( input.toString().replace(/\.[^\.]+$/, '.pdf') );

var document = kn.open( inFile );

kn.export(document, {to: outFile, as: 'PDF', withProperties:{exportStyle:'Handouts', slideNumbers:'yes'}});

kn.close(document, {saving: 'no'} );

return outFile.toString();

}

function run(input, parameters) {

var kn = Application("Keynote")

var result = [];

var documentList = input instanceof Array ? input: [ input ];

for (var i in documentList) {

result.push( convertToPDF(kn, documentList[i]) );

}

return result;

}

I would do this through UI scripting and the print dialog. You could set it to Layout > 2 pages per sheet which would get you 6 / page. Or you could UI script the handout layout dropdown. But it’s not exposed to AppleScript as far as I can tell. Which is typical of Keynote ApplesScript – there are a lot of little omissions that force UI scripting for some tasks.

Here’s how I have approached this: https://github.com/derickfay/keynote-to-pdf

Actually it looks like the Keynote print UI has changed a bit since I wrote that.

If you first create a print layout with 2 slides per page, and save it as 2x, this should work for you to print 6 slide / page handouts:

    -- Keynote 6 version

tell application "System Events"
	tell application process "Keynote"
		set frontmost to true
		
		repeat until window 1 exists
		end repeat
		
		-- Hide Inspector if visible	
		if menu item "Hide Inspector" of menu 1 of menu bar item "View" of menu bar 1 exists then
			keystroke "i" using {command down, option down}
		end if
		
		-- Print; wait until the sheet is visible	
		click menu item "Print…" of menu 1 of menu bar item "File" of menu bar 1
		
		repeat until sheet 1 of window 1 exists
		end repeat
		
		set thePopUp to first pop up button of sheet 1 of window 1 whose description is "Presets"
		click thePopUp
		click menu item "2x" of menu 1 of thePopUp --replace if desired with your preferred preset
		
		if value of (radio button "Selection" of sheet 1 of window 1) is 0 then
			click (radio button "Selection" of sheet 1 of window 1)
		end if --set to print selected slides only -- you can use the same technique to adjust other checkboxes as desired
		
		click checkbox 5 of sheet 1 of window 1 -- replace to suit your needs: 5 =  Handout
		click menu button "PDF" of sheet 1 of window 1
		click menu item "Save as PDF…" of menu 1 of menu button "PDF" of sheet 1 of window 1 -- Save as PDF...
	end tell
end tell