I am new to applescript which may mean this is a simple question.
I am trying to copy and paste a text into a keynote slide at a certain location or position.
The following script will not work for some reason. The error messages are:
object text is not recognized. A : is expected after object. The properties of a shape do include object text though.
when I exclude the text part and run the script it won’t recognize shape and asks for a definition of shape as a variable. This is despite the fact that shape is a defined word in the keynote library with its properties.
I can’t find anything appropriate here nor more broadly.
Thanks in advance.
Here is the script:
tell application “Keynote”
set PanelN to “test”
set horizontalPosition to 587
set verticalPosition to 488
tell the slide
set thisShape to ¬
make new shape with properties ¬
{position:{horizontalPosition, verticalPosition}, width:5, height:7, object text:PanelN, opacity:100}
end tell
end tell
It seems that something is broken in Keynote support.
If we remove the part of the instruction defining the object text, the script compiles the wrong way.
tell application "Keynote"
set PanelN to "test"
set horizontalPosition to 587
set verticalPosition to 488
tell the slide
set thisShape to make new shape with properties ¬
{position:{horizontalPosition, verticalPosition}, width:5, height:7, opacity:100}
end tell
end tell
shape is not treated as a Keynote object displayed in italic blue but as a variable displayed in green.
It seems that a bug report would be required.
I checked with the two other applications. In Pages and Numbers, shape is correctly compiled.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 12 juillet 2019 10:50:13
You need a proper specifier for the slide, which is in turn an element of a document. eg.:
tell application "Keynote"
set PanelN to "test"
set horizontalPosition to 587
set verticalPosition to 488
tell document 1 -- Added.
tell slide 1 -- Changed. Adjust as required.
set thisShape to ¬
make new shape with properties ¬
{position:{horizontalPosition, verticalPosition}, width:5, height:7, object text:PanelN, opacity:100}
end tell
end tell
end tell
The shape created with the dimensions given above is very small, but the script works!
The hierarchy is great input. I now wonder if I am missing a hierarchical step when trying to further define properties such as:
background fill type:no fill
–(or background color:{250,250,250} ?)
font name:“Helvetica Neue”
alignment:left
text color:{0,0,0}
vertical alignment:center
font size:12
None of these works within shape. But they are properties of a cell, column or row. Obviously not part of Keynote.
Alternatively, I have pasted a pre-formatted text from Numbers into Keynote and find all properties above to be correct except for the position of the text box. So if I could only move the text box to a specific location inside the slide, that would help as well. Here is the script I tried, but it didn’t work either:
tell application “Numbers” to activate --NUMBERS
delay 0.2
– bring “System Events” to the front
tell application “System Events”
delay 0.2
key code 36 using option down
keystroke “a” using command down
keystroke “c” using command down
end tell
– bring “Keynote” to the front
tell application “Keynote” to activate --KEYNOTE
delay 0.2
– bring “System Events” to the front
set horizontalPosition to 587
set verticalPosition to 723
tell application “System Events”
delay 0.2
keystroke “v” using command down
end tell
tell application “Keynote”
delay 0.2
tell document 1
tell slide 1
move shape to slide 1 with properties {position:{horizontalPosition, verticalPosition}}
end tell
end tell
end tell
If you’ve successfully copied the text box into the slide, you should be able to change its position by setting the position to what you want:
tell application "Keynote"
delay 0.2
tell document 1
tell slide 1
set position of shape 1 to {horizontalPosition, verticalPosition}
end tell
end tell
end tell
The same with any other of its properties you want to change.
By the way, if you enclose your AppleScript code in MacScripter’s [applescript] and [/applescript] tags when you post it, it’ll appear formatted as in Yvan’s and my posts, with a clickable link to open it in people’s script editors. There’s a button for these tags just above the text window on posting pages.
It seems to be a bug in Keynote, or in its AppleScript implementation. On my machine, only the vertical coordinate is heeded when setting an image position. The horizontal coordinate’s totally ignored!
The only way I can see round it at the moment is a mixture of Keynote and GUI scripting:
set thePosition to {94, 122} -- Adjust as required.
tell application "Keynote"
activate
tell document 1
tell slide 4
-- While this only moves the image vertically, it does also select it.
set position of image 1 to thePosition
end tell
end tell
end tell
tell application "System Events"
tell application process "Keynote"
set frontmost to true
tell window 1
-- If the "Format" button in the toolbar's not selected, click it and wait for the "Arrange" button in the "Format" pane to appear.
-- The details in the pane will be for the image just selected above.
-- You may need to translate "Arrange" into your own language.
set formatButton to radio button 1 of radio group 1 of toolbar 1
if (formatButton's value is 0) then
click formatButton
repeat until radio button "Arrange" of radio group 1 exists
delay 0.2
end repeat
end if
-- Click the "Arrange" button.
click radio button 3 of radio group 1
-- Fill in the X coordinate text field.
tell text field "X" of scroll area 1
set focused to true
set value to (beginning of thePosition) as text
end tell
end tell
end tell
-- Apply the setting.
keystroke return
end tell
I wish I had known this sooner. I can’t upgrade to OS 10.14 because of hardware limitation. I worked around this bug with a combination of Filemaker Pro scripting and Applescript (I am pasting images from Filemaker Pro container fields) by pasting the image and then using System Events key codes 123 and 124 to push the image to the desired location. This solution looks a lot cleaner.