Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to programmatically generate Keynote file

Hello, I would like to have Template Keynote file with placeholders for data I programmatically fill in and repackage. What is best way to do this? I tried updating PPTX slide xml (because Keynote can read PPTX format), but rezipped file doesn’t open properly (I tried mac os x zip utility with all types of compression, from 0 to 9). Looking at the Keynote format, it seems to be mostly JPGs, so can't programmatically edit. Any help you can provide is appreciated. Thanks.

Posted on Jun 25, 2015 9:27 PM

Reply
Question marked as Best reply

Posted on Jun 26, 2015 11:11 AM

I think I understand what you mean, having worked with Macromedia Director (now Adobe) for years - but back then, I would need to be pretty creative if I wanted to do what I think you want with PowerPoint.


Maybe if you could define the WorkFlow you NEED, in detail, we could grasp the problem better, then advise a course of action. (I learned long ago to not try to guess at stuff with "discovering" a LOT up front)

14 replies
Question marked as Best reply

Jun 26, 2015 11:11 AM in response to fmnewbie

I think I understand what you mean, having worked with Macromedia Director (now Adobe) for years - but back then, I would need to be pretty creative if I wanted to do what I think you want with PowerPoint.


Maybe if you could define the WorkFlow you NEED, in detail, we could grasp the problem better, then advise a course of action. (I learned long ago to not try to guess at stuff with "discovering" a LOT up front)

Jun 26, 2015 1:43 PM in response to ChitlinsCC

Here are additional details. Basically there is a Keynote presentation with numerical figures that is currently manually created today. I would like to generate the same presentation but have the dollar figures filled in from another data source, specifically a database. I know SQL and how to extract the data from the other data source, so that portion is covered. I would like to put placeholder tokens in the Keynote presentation template, such at $TOTAL, then use Java to read the slide data into a string, and dynamically replace the $TOTAL placeholder token with an actual amount, such as $20,000, and this is for data to be substituted on multiple slides (with various placeholders).


I extracted the Keynote file with zip utility and noticed most of the content is JPG files. This is why I mentioned PPTX, because I saw the slide information when unzipped looks like XML, so that should work for editing purpose. However, when I zip the modified XML back together, Powerpoint says it is corrupt and needs to be repaired. It repairs it, then when it is opened, it has exclamation marks following the text that was modified. When opening the modified PPTX in Keynote, the exclamation marks are not there, but rather spaces, so the data is out of alignment.


The goal is the generated presentation needs to be readable (and modifiable) in Keynote, so whether is it a modified pptx or modified Keynote directly, either way would work. Therefore, I am soliciting recommendations for best way to accomplish this.

Jun 26, 2015 2:29 PM in response to fmnewbie

I figured as much... let me stress again that I am a "high end presentation pro" - NOT a Keynote user - PowerPoint if it is an absolute requirement - BUT...


It seems that Apple, in its infinite wisdom, has adopted the whole "sharing" idea across the board. It would surprise me if Keynote.app would not "talk" with Numbers.app to some degree - like PowerPoint would with Excel. If it does, maintaining your external data in Numbers file stored on the cloud (lower case is by intent, although Apple may have other ideas) may be the answer to your prayers.


Neither app are my bailiwick, but I will see if another member here may have some ideas, having not seen this thread as he is in another hemisphere.

Jun 27, 2015 11:50 PM in response to ChitlinsCC

Hi ÇÇÇ,


Thanks for your smoke signal from the northern hemisphere to this discussion in Numbers for Mac: Re: Please help, conditional vlookup multiple pricing tables.


All I can say is that:

1. Numbers will link within a document, but will not link between documents;

2. I don't use Keynote;

3. Therefore, I don't know how to link documents between Numbers and Keynote.


Yours in the southern hemisphere 👿.


Regards,

Ian.

Jun 28, 2015 8:23 AM in response to fmnewbie

fmnewbie wrote:


there is a Keynote presentation with numerical figures that is currently manually created today. I would like to generate the same presentation but have the dollar figures filled in from another data source


I spotted the smoke signal from the northern hemisphere to the southern hemisphere, and followed it here. As I'm sure you're aware, the iWork apps are scriptable. You should be able to use AppleScript to place the data you've extracted from the datasource into a Keynote table. Lots of useful information at https://iworkautomation.com/keynote/table.html.


I don't use Keynote but if you post specifics of the structure of your Keynote document it may turn out to be easy to cobble together a script, especially if you have already done the work of extracting data from elsewhere.


SG

Jun 28, 2015 5:38 PM in response to SGIII

Hello SG, no I was not actually aware the iWork apps are scriptable. This seems promising, however from my skimming, the examples looked more geared to creating new slides and content as opposed to replacing. Is there an example of opening an existing Keynote presentation (i.e. the template file), and getting references by some means to placeholder strings like $TOTAL, replacing the placeholders with actual data like $20,000, and then saving a copy of the updated file?


As a concrete example, suppose for simplicity the template Keynote presentation had three slides. The first and third slides are all static content we don't want to modify. The second slide had various textual strings and colors, and it also had the $TOTAL placeholder somewhere on the slide and we want to update it to an actual number. Specifically, it would be helpful to perform the following steps in a script:


0) (Preprocessing step not in script) Use Java to get data from external database, and store placeholder token to value pair. For example, it would store $20,000 with the token $TOTAL. This part I know how to do.

1) Make a copy of the template file. For example, suppose template file (the file that has placeholder strings) was named "template.key". This step would create a copy named "report-06292015.key" as an example, where 06292015 would be the current date the script was run

2) Open "report-06292015.key", navigate to slide 2, get a handle to textbox or textarea that had $TOTAL in it. Replace $TOTAL with $20,000. (Not sure how it would get the data from Java - for now, let's assume we just want to replace $TOTAL with $20,000 and ignore Java for now. I am interested in how the replace would be done).

3) Save updated "report-06292015.key".

Jun 28, 2015 6:55 PM in response to fmnewbie

This doesn't appear difficult. Steps 1 and 3 can be gleaned from examples at the iworkautomation.com site. The details can be worked out if this approach looks as if it will be efficient for you.


Step 2 is less obvious.I created a three-slide document. Slide 2 looks like this:


User uploaded file


It turns out that Keynote treats this as two 'text item' objects on slide 2 (the title at the top is text item 1, the bullet list text item 2. So we need to make the replacement in text item 2's property called 'object text' (I got the name of the property from the Keynote dictionary accessed via Script Editor, in Applications > Utilites).


So, in AppleScript's typically somewhat verbose fashion, the find-replace is easily made with a script like the one below.


To run the script, copy-paste into Script Editor, make sure the target document is frontmost, and click the triangle 'run' button.


If you're placing a value in a cell in a table on a slide (as opposed to making a replacement in text) then the code would be more succinct.


Anyway, it does appear to be quite straightforward.


SG




tell application "Keynote"

tell front document

tell slide 2's text item 2

set tt to its object text

set tt to my replaceTxt(tt, "$Total", "$100")

set its object text to tt

end tell

end tell

end tell



to replaceTxt(tt, f, r)

set AppleScript'stext item delimiters to f

set tt to tt's text items

set AppleScript'stext item delimiters to r

set tt to tt's text items as text

set AppleScript'stext item delimiters to ""

return tt

end replaceTxt

Jun 28, 2015 9:47 PM in response to SGIII

The approach seems like it would work in theory. I am thinking I could have Java dynamically create the AppleScript with the substituted values, then run the AppleScript from the command line. Let me know if there is better suggestion for that part.


However, I am not following the replaceTxt method. I don't see reference to text item delimiters in the Help reference at "https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/App leScriptLangGuide/introduction/ASLR_intr…". Can you explain a little more about how that method is working?

Jun 30, 2015 1:32 PM in response to fmnewbie

fmnewbie wrote:

I am thinking I could have Java dynamically create the AppleScript with the substituted values, then run the AppleScript from the command line.


Do a 'man osascript' to see how you can run AppleScript (or JavaScript) from the command line. Certain gui aspects of scripts get a little funky, and there's the headache of 'quoting'.


Note that there are ways of putting an AppleScript "wrapper" around your preferred language, so that AppleScript (or JavaScript) does the interfacing with the App but you're doing most of the work in your language.


SG

Jun 30, 2015 1:55 PM in response to fmnewbie

fmnewbie wrote:


Is there a way to dynamically edit a chart with AppleScript? I see add chart command in the dictionary, but no way to edit an existing chart (such as changing title, changing row/column data, etc).


I'm not very familiar with Keynote but, unlike in Numbers, it seems that a Keynote chart is not linked to an external data table that is easily manipulated via AppleScript.


However, as you can see here, it is possible to read the values of a table into AppleScript lists, and use those lists when making a Chart. So after making a change in a table you could have AppleScript make a new chart and delete the old one.


That's for the data. As for title, not sure if there any control via AppleScript.


SG

How to programmatically generate Keynote file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.