Applescript code for inset a rectangle box in Keynote document

Hi Friends,


I would like to insert a new object (it may be rectangle, text frame, or oval shape) in to the slide.


For that I write the below applescript code, but it is not working, can you please guide me to get solution?


tell application "Keynote"
    -- Check if there is a Keynote document open
    if (count of documents) > 0 then
        set theDoc to the front document
        
        -- Loop through each slide in the document
        repeat with slideIndex from 1 to count of slides of theDoc
            set currentSlide to slide slideIndex of theDoc
            
            -- Insert a rectangle shape (correct approach)
            try
                -- Create a rectangle
                set newRectangle to make new rectangle at currentSlide
                
                -- Set position and size separately
                set position of newRectangle to {50, 50}  -- Position of the rectangle
                set size of newRectangle to {200, 100}    -- Size of the rectangle
                
                log "Rectangle inserted successfully on slide " & slideIndex
            on error errMsg
                log "Error inserting rectangle on slide " & slideIndex & ": " & errMsg
            end try
            
            -- Insert a text box (for example)
            try
                set newTextBox to make new text item at currentSlide with properties {position:{300, 300}, size:{200, 50}, object text:"Hello, Keynote!"}
                set font size of newTextBox to 24
                set font name of newTextBox to "Helvetica"
                log "Text box inserted successfully on slide " & slideIndex
            on error errMsg
                log "Error inserting text box on slide " & slideIndex & ": " & errMsg
            end try
            
            -- Insert a circle (oval shape)
            try
                -- Create an oval
                set newCircle to make new oval at currentSlide
                
                -- Set position and size of the oval (circle)
                set position of newCircle to {600, 50}
                set size of newCircle to {100, 100}
                
                log "Circle (oval) inserted successfully on slide " & slideIndex
            on error errMsg
                log "Error inserting circle on slide " & slideIndex & ": " & errMsg
            end try
            
        end repeat
        
        -- Notify when done
        display dialog "Objects insertion completed."
    else
        -- If no Keynote document is open
        display dialog "No Keynote document is open." buttons {"OK"} default button "OK"
    end if
end tell

Posted on Nov 19, 2024 11:17 PM

Reply
2 replies

Nov 25, 2024 08:33 AM in response to asuvathdhaman

There are two problems with your script.


First up, there is no 'rectangle' object class, so make new rectangle is doomed to fail in any instance.


Instead, Keynote has a generic shape class. Interestingly I couldn't see any obvious way to specify rectangle vs circle, vs. anything else - but make new shape will make a rectangle by default.


The second problem is that your command for adding the object is incorrect.


                set newRectangle to make new rectangle at currentSlide


When you make an object, the at parameter is used for it's own positioning/coordinates, so currentSlide is not valid and is what's throwing off your code.


Instead, to add an object to the slide, you need to target the slide. Rewrite the above line as:


tell currentSlide
    set newRectangle to make new shape
end tell


This will create a (rectangular) shape object on the slide and you can position it as you like, which brings me to the last point...


In Keynote, shapes do not have a size parameter. Instead, they have a height and width which you can set independently.


Putting that all together, the revised code should look something like this:


			try
				-- Create a rectangle
				tell currentSlide
					set newRectangle to make new shape
				end tell
				-- Set position and size separately
				tell newRectangle
					set position to {50, 50} -- Position of the rectangle
					set width to 200
					set height to 100 -- Size of the rectangle
				end tell
				
				log "Rectangle inserted successfully on slide " & slideIndex
			on error errMsg
				log "Error inserting rectangle on slide " & slideIndex & ": " & errMsg
			end try


(or you could pass position, height, and width as properties to the make new shape command. Your choice.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript code for inset a rectangle box in Keynote document

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.