首页 > 解决方案 > 有没有办法将 AppleScript 与 Keynote 一起使用,这样我就可以为文本对象激活数字项目符号和列表?

问题描述

我想自动化 Swift 中从 Xcode 复制并粘贴到 Keynote 文本字段中的代码格式。格式确实被保留了,但我想更改字体大小(我已经这样做了),并且我想进一步添加行号(这可以使用数字类型的项目符号和列表手动完成)。

我写了一个 AppleScript 程序,它只是改变字体的大小。

我的代码如下所示:

tell application "Keynote"
    activate
    set ts to the current slide of the front document
    tell ts
        set theTextItem to its first text item
        tell theTextItem
            set the size of its object text to 32
        end tell

    end tell
end tell

此代码将文本对象的大小更改为 32,但我没有找到激活行号的方法(即激活 Bullets 和 Lists.xml 中的数字格式)。

标签: applescriptkeynote

解决方案


TI 证明 iWork 中的项目符号和编号是富文本格式的一部分,AppleScript 无法直接访问这些格式。但是,您可以通过 GUI 脚本编写一些工作来完成此操作,如下所示:

tell application "Keynote"
    activate
    tell front document
        tell current slide
            set theTextItem to its second text item
        end tell
        -- this selects the text item
        set selection to theTextItem
    end tell
end tell

tell application "System Events"
    tell process "Keynote"
        tell first window
            -- this makes sure the panel is set to the 'Text' tab
            tell first radio group's radio button "Text"
                if its value = 0 then
                    click
                end if
            end tell
            tell scroll area 1
                -- this finds the correct button, then clicks it to open the popover
                set popoverButton to (first button whose help is "Choose a list style.")
                tell popoverButton
                    click
                    tell first pop over's first scroll area's first table
                        -- this finds the table row in the popover for creating a numbered list
                        -- and selects it. You can substitute in 'Bullet', 'Image', 
                        -- 'Lettered' or any other label in the popover. 
                        select (first row whose first UI element's first text field's value contains "Numbered")
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

它不漂亮,但它完成了工作。


推荐阅读