以编程方式从文本数据生成Keynote幻灯片

时间:2017-12-26 10:05:16

标签: pdf text applescript keynote

我写了一个用于处理教堂崇拜歌曲的数据库网络应用程序,我正在尝试添加一个模块来输出所选歌曲以用于投影歌词。我最初认为我的所有用户都在使用Powerpoint,它可以导入一个简单的文本文件,每行前面有0-5个标签(0-tab行成为新幻灯片的标题,一个或多个标签表示“项目符号点”)与对应多少个标签的级别)。因此,我的模块当前输出了这样一个文本文件,并且演示者将打开一个Powerpoint模板,其样式为他们想要的歌曲演示方式,插入文本文件作为新幻灯片的“轮廓”,并且瞧。以下是在Powerpoint中使用的文本文件结构的一小部分示例(使用下一级别的大纲,使用一个日文缩略图,使用较小的字体进行罗马化):

As the Deer
    As the deer panteth for the water
    So my soul longeth after Thee
    You alone are my heart's desire
    And I long to worship Thee
As the Deer
    You alone are my strength, my shield
    To You alone may my spirit yield
    You alone are my heart's desire
    And I long to worship Thee
鹿のように (As the Deer)
    谷川の流れを したう鹿のように
        tanigawa no nagare o shitau shika no yoo ni
    主よ、わが魂 あなたをしたう
        shu yo, waga tamashii anata o shitau
鹿のように (As the Deer)
    あなたこそ わが盾、 あなたこそ わが力
        anata koso waga tate, anata koso waga chikara
    あなたこそ わが望み われは主をあおぐ
        anata koso waga nozomi, ware wa shu o aogu

但是我教会的牧师(他的计算机用于投影)以及除了我之外选择歌曲的另外两位崇拜领袖都使用Mac。他们总是谈论Powerpoint的幻灯片,所以我认为他们正在使用它们。但就在我完成输出标签文本的代码时,我发现当他们说“Powerpoint”时,他们的确意味着 Keynote ,它无法使用纯文本文件。旧版本的Keynote内部存储了XML格式的幻灯片数据(https://www.xml.com/pub/a/2004/01/07/keynote.html),但较新的Keynote版本逐渐使用更不透明的格式(http://justsolve.archiveteam.org/wiki/IWork)。除了Keynote创建或编辑Keynote演示文稿外,Apple显然不需要任何其他内容。

所以我正在寻找有关如何处理此问题的建议。我想要一个不涉及我创建Powerpoint文件的过程,以便他们转换为Keynote。我不仅应该每周都在循环中,而且转换中存在一些行间距问题。

我知道如何使用LaTeX从数据中生成PDF(我目前只为打印的chordsheets而做,但我确定我可以弄清楚如何进行幻灯片式布局),但Keynote显然只导入一页一次PDF格式 - 一个典型的星期日崇拜集将是30-50左右的幻灯片,所以这将是非常烦人的。另外,我希望崇拜领袖/牧师能够在必要时调整主题演讲 - 如果它来自PDF,那将是不可能的。因此,我的第一选择是以某种方式将Keynote“模板”与某种文本(XML,JSON,标签文本或其他)结合起来。

This conversation似乎暗示可以用Applescript(听起来像iWorks相当于Office的VBA),但由于我没有Mac,这需要长时间借用别人的电脑学习语言并开发/测试脚本(除非它很简单,你们其中一个人愿意为我做点什么)。想法?

编辑:在CJK的回答和评论之后,我意识到最终结果的一个例子可能有助于想象我正在尝试做什么。以下是来自Powerpoint的两张幻灯片,选择日语幻灯片示例,因为如果我可以使用它,英语幻灯片很容易。一周前,当我试图将这个Powerpoint导入Keynote时,最顽固的造型是日语(一级“子弹”)和罗马化(二级)的行间距。

(如果你想知道,是的,这是底部的标题框 - 我知道这是非常规的,但由于上限较低,我们幻灯片的上半部分是优质房地产。)

enter image description here

在下一个非季节性版本的例子中,我添加了Powerpoint的文本文件导入不支持的内容,因为CJK的方法激发了我如何使用脚本中的富文本来完成它:两种不同的文本格式标题框。 (之前我曾计划将版权信息放在每首歌曲的最后一张幻灯片上,使用“子弹”样式的3级或4级歌词下方。但我更喜欢标题。) enter image description here

编辑2:尝试从CJK的脚本跳板,这里尝试使用现有的主幻灯片,如“标题和项目符号”和段落样式(完全未经测试 - 只是来自示例的位线上)。我将能够在两天内测试它,但我把它放在这里,所以CJK可以看到我到目前为止所做的事情:

-- *** I'd like to use relative path so it would be portable, but (path to home folder as text) gave errors ***
property SambiDBTextFile : "/Users/Rachel/Desktop/Songs.txt"

property masterSlideName : "Lyrics" -- custom master slide based on "Title & Bullets"

-- ** If I can use paragraph styles, I won't need these ***
property TextSizes : {32, 28, 20}
property TextColours : {"white", {63222,57568,41634}, {63222,57568,41634}}
property TextFonts : {"Hiragino Kaku Gothic Pro", "Arial Italic", "Hiragino Kaku Gothic Pro"}

-- ** This is what I really want to use, but I don't know if I can ***
property TextStyles : {"Main Lyrics", "Romaji Lyrics", "Song Credits"}

set AppleScript's text item delimiters to tab
set notes to paragraphs of (read SambiDBTextFile)

tell application "Keynote" to tell current document

    -- *** Check for master slide existence, and substitute if absent ***
    set masterSlideList to the name of every master slide
    if masterSlideName is not in masterSlideList then
        -- *** Create master slide? Nah, probably not possible ***
        display alert ("Master Slide") message "Master slide '" & masterSlideName & "' not found; using 'Title & Bullets' instead."
        set masterSlideName to "Title & Bullets"
    end if

    -- Create slides with content from Keynote text file
    repeat with i from 1 to number of notes
        if item i of notes is "" then exit repeat -- EOF

        -- Get the text (without tabstops) and the level of indentation
        set [TextContent, TabValue] to [last text item, number of rest of reverse of text items] of item i of notes

        if TabValue is 0 then -- Indicates title of new slide
            set current slide to make new slide with properties {base slide:master slide masterSlideName}
            set object text of the default title item to TextContent
        else -- TabValue is not 0, indicating lyrics
            if TabValue > 3 then set TabValue to 3

            -- *** I have no idea if this will work, but the point is to append
            set object text of default body item to object text of default body item & TextContent & return

            -- *** Style the line just added ***
            -- *** Plan A: use paragraph styles (not sure if I can do this) ***
            set paragraph style of paragraph ((count of paragraphs of default body item) - 1) of default body item to item TabValue of TextStyles

            -- *** Plan B: hardcoded styling (uncomment if above line doesn't work) ***
            --tell paragraph ((count of paragraphs of default body item) - 1) of default body item
                --set its color to item TabValue of TextColours
                --set its font to item TabValue of TextFonts
                --set its size to item TabValue of TextSizes
            --end tell
        end if
    end repeat
end tell

1 个答案:

答案 0 :(得分:2)

今天使用 Keynote 和AppleScript玩了一下,我认为下面的脚本会产生你想要的东西。它包括根据要解析的文本文件中每行的制表级别(1-5)设置不同文本设置的选项。

    property KeynoteTextFile : "/Users/CK/Desktop/Keynote.txt"

    property PresentationTitle : "My Presentation"
    property _W : 1024 -- The width of each slide
    property _H : 768 -- The Height of each slide

    -- Text properties for the cover title and each slide title
    property CoverTextStyle : {font:"Arial Bold", color:"white", size:96}
    property TitleTextStyle : {font:"Arial Bold", color:"white", size:48}

    -- Spacing above and below the title of each slide
    property TitleMargins : {top:30, bottom:100}
    -- Spacing between lines in the body of each slide
    property VerticalSpacing : 75

    -- Text properties for the body of each slide for
    -- each level of tabulation
    property Tabulations : {0.1, 0.2, 0.3, 0.4, 0.5}
    property TextSizes : {32, 28, 24, 20, 16}
    property TextColours : {"white", "blue", "green", "magenta", "orange"}
    property TextFonts : {"Arial", "Arial Italic", "Times New Roman Bold", ¬
        "Times New Roman Bold Italic", "Times New Roman Italic"}


    set AppleScript's text item delimiters to tab
    set notes to paragraphs of (read KeynoteTextFile)

    -- Create new presentation with cover slide
    tell application "Keynote" to tell (make new document with properties ¬
        {document theme:theme "Black", width:_W, height:_H})

        set MyPresentation to it

        set base slide of current slide to master slide "Blank"

        tell the first slide to ¬
            set CoverTitle to make new text item ¬
                with properties {object text:PresentationTitle}

        set properties of object text of the CoverTitle to CoverTextStyle
    end tell

    -- Create slides with content from Keynote text file
    repeat with i from 1 to number of notes
        if item i of notes is "" then exit repeat -- EOF

        -- Get the text (without tabstops)
        -- and the level of indentation
        set [TextContent, TabValue] to ¬
            [last text item, number of rest of reverse of text items] ¬
                of item i of notes

        if TabValue is 0 then -- Indicates title of new slide
            tell application "Keynote"

                tell (make new slide at end of slides of MyPresentation) to ¬
                    set Title to make new text item ¬
                        with properties {object text:TextContent}

                set properties of object text of the Title to TitleTextStyle
                copy position of Title to [_x, _y]
                set position of Title to [_x, |top| of TitleMargins]

            end tell
        else -- TabValue is not 0, indicating slide content
            if TabValue > 5 then set TabValue to 5

            tell application "Keynote" to tell current slide of MyPresentation
                set n to number of text items

                set T to make new text item with properties ¬
                    {object text:TextContent}

                tell object text of T
                    set its color to item TabValue of TextColours
                    set its font to item TabValue of TextFonts
                    set its size to item TabValue of TextSizes
                end tell

                set position of T to ¬
                    [(item TabValue of Tabulations) * _W, ¬
                        VerticalSpacing * n + (|bottom| of TitleMargins)]

            end tell
        end if
    end repeat

    -- Go to first slide of presentation and bring Keynote
    -- into the foreground
    tell application "Keynote"
        set current slide of MyPresentation to first slide of MyPresentation
        activate
    end tell

以下是我的 Keynote.txt 文件中的一些行:

This is a Title
    This is indented by 1 tab
    So is this
        This is indented by 2 tabs
            This is 3 tabs
    Back to 1 tab

产生了这张幻灯片:

A Keynote slide made using AppleScript

请记住,实际幻灯片上的缩进级别不是由文本文件中的选项卡确定,而是由脚本顶部定义的属性tabulations的值确定。因此, only 效果解析文本文件中的选项卡的存在将决定在幻灯片上呈现时应用于文本的特征集(字体,颜色,大小,和缩进独立于其文本文件缩进)。

作为最后一点,我应该指出文本文件中的行可以从0-5个标签开始。将6个或更多选项卡视为仅有5个选项卡。但是,重要,该行的其余部分包含文本中的任何标签。如脚本所示,这将产生一些奇怪的结果。可以调整脚本以满足需要在文本中包含选项卡的行,但我目前没有看到需要。