0

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.

user3439894
  • 7,266
  • 3
  • 17
  • 28
Coti
  • 43
  • 1
  • 9
  • This is just an inference, but I suspect that bullets and numbering are part of the rich text object that text items (and other iWork items) refer to. AppleScript has never had a system for dealing with rich text effectively. You might be able to something tricky, like script a bulleted rich text object in Text Edit and then copy it into Keynote. I can look into that if you think it would be worth the trouble. – Ted Wrigley Jul 11 '19 at 20:31

1 Answers1

1

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.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
  • a big Thank You! It does work, any idea why Keynote might actually keep a mixture of Spanish and English names for list styles? I am currently using English as System Language, but Keynote keeps precisely the list styles in Spanish. I changed Number to Número and it worked fine as I say. Muchas gracias! – Coti Jul 22 '19 at 16:03
  • Keynote has a menu item — File->Advanced->Language & Region — that allows you to set a specific language for the presentation itself (potentially different from the defined system language). That's the only thing that jumps out at me. – Ted Wrigley Jul 22 '19 at 16:56
  • Oh, wait... You can also define custom list styles in that 'list' popover (by hitting the '+' button), and name those styles in whatever language you like. if this is a hand-me-down presentation, someone up the chain might have defined styles in a different language. – Ted Wrigley Jul 22 '19 at 17:01