applescriptkeynote

Is there a way to use AppleScript with Keynote, in such a way I can activate Number Bullets and Lists for a text object?


I woud like to automate the formatting of code in Swift copied from Xcode and pasted into a text field in Keynote. Format is indeed carried over, but I want to change the font size (this I have done) and further I would like to add line numbers (this can be done manually with Bullets and Lists of type Number).

I have written an AppleScript program that just changes the size of the font.

My code looks like this:

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

This code changes the size of the text object to 32, but I have found no way to activate line numbering (that is, to activate Numbers format in Bullets and Lists.


Solution

  • TI turns out that Bullets and Numbering in iWork are part of the rich text format, which AppleScript does not have direct access to. However, you can accomplish this through GUI-scripting with a bit of effort, like so:

    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
    

    It ain't pretty, but it gets the job done.