Keynote text and image replacement using applescript

Hi All,

Is it possible to replace the text in keynote document from the different soruces like keynote, excel and pages(for multi-languages) using applescript? And also I want to replace the images in the keynote document from the selected path.

Thanks

If Keynote is scriptable it has an AppleScript dictionary. That will tell you what you can script in Keynote.
Open the dictionaries palette in Script Editor with cmd-shift-L.
The window that opens has a button to add dictionaries.

Hi all

I have replaced the text for title and body content see the below code


tell application "Keynote"
	tell document 1
		set slidebody to object text of default body item of current slide
		set slidebody to my searchnreplace("<<2>>", "Body Text", slidebody)
		set object text of default body item of slide 1 to slidebody
		set slidetitle to object text of default title item of current slide
		set slidetitle to my searchnreplace("<<1>>", "Title Text", slidetitle)
		set object text of default title item of slide 1 to slidetitle
	end tell
end tell

-- I am a very old search & replace function...
on searchnreplace(searchstr, replacestr, txt)
	considering case, diacriticals and punctuation
		if txt contains searchstr then
			set olddelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {searchstr}
			set txtitems to text items of txt
			set AppleScript's text item delimiters to {replacestr}
			set txt to txtitems as Unicode text
			set AppleScript's text item delimiters to olddelims
		end if
	end considering
	return txt
end searchnreplace

For text containing without title and body text frames I can’t able to replace. Is there any options to replace the text without title and body tag?

Thanks

Text boxes and graphics are referred to in Keynote’s scripting dictionary as shapes. (As distinct from photos, which are referred to as images.)

Like the the default body and title items, shapes have an object text property, which can be modified in the same way.

So you would need to iterate through the shapes on the slide and replace the text as required.

To answer your earlier question about images, I don’t believe it is possible to use AppleScript to replace them in Keynote presentations.

Keynote’s scripting dictionary claims that images (as iWork items) have a file property and a file name property, but that these are read only.

However, even if I try to read either of these properties for images in a Keynote presentation, I get an AppleEvent handler failed error -10000. (Same with Pages, and probably with Numbers although I haven’t tested it.)

So unfortunately it looks like a non-starter.

About text replacement.
The KeyNote can contains rich text in the 4 KeyNote objects:

  1. object text of default body item of some slide,
  2. object text of default title item of some slide
  3. presenter notes of some slide
  4. object text of some text item of some slide

So, when you say: how about the text without the body or the title, you have to replace contents of presenter notes or object text of some text item. It is quit possible.

Here is replacing text contents example :


-- script: KeyNote text replacement example

tell application "Keynote" to tell document 1 to tell slide 1
	set slidebody to object text of default body item
	set slideTitle to object text of default title item
	set slidePresenterNotes to presenter notes -- this is of shape type
	set slideText to object text of text item 4
end tell

set slideTitle to my searchnreplace("<<1>>", "Title Text", slidebody)
set slidebody to my searchnreplace("<<2>>", "Body Text", slidebody)
set slidePresenterNotes to my searchnreplace("<<3>>", "Presenter Notes", slidePresenterNotes)
set slideText to my searchnreplace("<<4>>", "Textbox Text", slideText)

tell application "Keynote" to tell document 1 to tell slide 1
	set object text of default title item to slideTitle
	set object text of default body item to slidebody
	set presenter notes to slidePresenterNotes -- this is of shape type
	set object text of text item 4 to slideText
end tell


-- I am a very old search & replace function...
on searchnreplace(searchstr, replacestr, txt)
	considering case, diacriticals and punctuation
		if txt contains searchstr then
			set olddelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {searchstr}
			set txtitems to text items of txt
			set AppleScript's text item delimiters to {replacestr}
			set txt to txtitems as Unicode text
			set AppleScript's text item delimiters to olddelims
		end if
	end considering
	return txt
end searchnreplace

Now, about the image replacement.
It is possible as well. KeyNote has commands get/make/delete for working with images. So, you can get properties of old image, (remember its position and size in the some temp variables), delete this old image, crop the new selected in the Finder image to the size of old image, make new image in the slide (on the old position) using the cropped image.

Thanks KniazidisR! I was taking the wrong approach, good to know that it can be done.