AppleScript to add empty line to the end of Presenter Notes in Keynote file on every slide

we have a requirement from a client to add an empty line at the end of the presenter's notes on each slide in Keynote.

I tried to do it with AI but something always goes wrong, for example the script below adds an empty line at the end of the presenter's notes on each slide but deletes all the current styles in the text. And no matter how much I try to fix it, nothing works. Maybe someone can help?

use framework "Foundation"
use scripting additions

-- Bring Keynote to the front
tell application "System Events" to tell process "Keynote" to set frontmost to true

-- Iterate through each slide in the Keynote document
tell application "Keynote"
    tell document 1
        set slideCount to count of slides
        repeat with i from 1 to slideCount
            set currentSlide to slide i
            my addEmptyLineToPresenterNotes(currentSlide)
        end repeat
    end tell
end tell

-- Function to return the index of the first document window
on firstDocumentWindowIdx()
    tell application "Keynote" to set documentNames to name of documents
    tell application "System Events" to tell process "Keynote"
        repeat with windowIdx from 1 to count of windows
            if name of window windowIdx is in documentNames then
                return windowIdx
            end if
        end repeat
        return 1 -- if none matches, return first window
    end tell
end firstDocumentWindowIdx

-- Function to find the scroll area most likely to be presenter notes and return its frame parameters
on getPresenterNotesFrame(currentSlide)
    set frontWindowIdx to my firstDocumentWindowIdx()
    tell application "System Events" to tell process "Keynote" to tell window frontWindowIdx
        set areas to every scroll area of splitter group 1
        -- Select area farthest to the bottom, which should now be presenter notes
        set highestY to 0
        repeat with areaIdx from 1 to the count of areas
            set pos to position of item areaIdx of areas
            if item 2 of pos ≥ highestY then
                set highestY to item 2 of pos
            end if
        end repeat
        repeat with areaIdx from 1 to the count of areas
            set pos to position of item areaIdx of areas
            if item 2 of pos = highestY then
                return item areaIdx of areas
            end if
        end repeat
    end tell
end getPresenterNotesFrame

-- Function to add an empty line to the end of presenter notes of the given slide
on addEmptyLineToPresenterNotes(currentSlide)
    tell application "Keynote"
        set presenterNotes to presenter notes of currentSlide
        set presenterNotesText to presenterNotes as text
        set presenter notes of currentSlide to presenterNotesText & return
    end tell
end addEmptyLineToPresenterNotes

Below are different ways AI suggested to add empty line to the end of presenter notes:

-- Function to add an empty line to the end of presenter notes of the given slide
on addEmptyLineToPresenterNotes(currentSlide)
    tell application "Keynote"
        set currentNotes to presenter notes of currentSlide
        set presenter notes of currentSlide to (currentNotes & return)
    end tell
end addEmptyLineToPresenterNotes
-- Script also resets the text styles!


//////////////////////////////////////////////////////////////////////////////////////
-- Function to add an empty line to the end of presenter notes of the given slide
on addEmptyLineToPresenterNotes(currentSlide)
    tell application "Keynote"
        set currentNotes to presenter notes of currentSlide
        set currentNotesText to currentNotes as text
        
        -- Check if the last character is already a newline
        if currentNotesText does not end with return then
            -- Append a newline while preserving formatting
            set currentNotes to currentNotesText & return
        end if
    end tell
end addEmptyLineToPresenterNotes

-- Script did not generate any errors, but also does nothing.


/////////////////////////////////////////////////////////////////////////////////
-- Function to add an empty line to the end of presenter notes of the given slide
on addEmptyLineToPresenterNotes(currentSlide)
    tell application "Keynote"
        activate -- Bring Keynote to the front
    end tell
    
    -- Get the index of the frontmost Keynote window
    set frontWindowIdx to firstDocumentWindowIdx()
    
    tell application "System Events"
        tell process "Keynote"
            -- Activate presenter notes field
            set notesArea to my getPresenterNotesFrame(currentSlide)
            if notesArea is not missing value then
                perform action "AXRaise" of notesArea
                set focused of notesArea to true
                
                -- Simulate pressing the down arrow and Return key to add a new line
                key code 125 -- Down arrow
                key code 36 -- Return key (Enter)
            end if
        end tell
    end tell
end addEmptyLineToPresenterNotes

-- script do preserve text styles but adds empty lines after every paragraphs in presenter notes but not on the end of the presenter notes, and works only on the current slide.

Upvotes: -2

Views: 17

Answers (0)

Related Questions