Keynote with Applescript find/replace function

Hello,

Currently I am making a Applescript and I have 1 problem now.
The problem is that I have made a keynote presentation with text items like: <<-titel-script->> and what I want is to replace that text with a text that is defined by an Applescript. Keynote is already active and the document is loaded by the Applescript. My only problem now is, how can I make an item in Applescript that searches for some text over the whole presentation an replace that text with a variable that is already defined earlier in the script.

Can somebody help me to find this little piece of code? I can’t find anything about it on the internet.

Thanks already!

Karsten,

Model: MacBook Pro
Browser: Firefox 3.0.1
Operating System: Mac OS X (10.5)

Hi karsten,

I have never scripted Keynote before, but this code works on my iMac with Keynote 08 (v4.0.3):


tell application "Keynote"
	tell slideshow 1		
		set slidebody to body of slide 1
		set slidebody to my searchnreplace("<<-titel-script->>", "DER TITEL", slidebody)
		set body of slide 1 to slidebody
	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

Thanks for your quick reply!

The result of that script is very strange, regular text items are replace normally as it should be, but if I want to change the title, new textfield or any copied textfield he won’t do anything strange enough.
Also the subtitle is working correcty.

I checked everything but can’t find nothing. Also the dia number is set correctly twice.

I work with Keynote 2008, I was forgotten to told that detail.

Karsten,

Well, we can also change the titles of the slides :wink:


tell application "Keynote"
	tell slideshow 1
		set slidebody to body of slide 1
		set slidebody to my searchnreplace("<<-titel-script->>", "DER TITEL", slidebody)
		set body of slide 1 to slidebody
		--
		set slidetitle to title of slide 1
		set slidetitle to my searchnreplace("<<-titel-script->>", "DER TITEL", slidetitle)
		set title 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

But I do not know how to address regular text boxes, maybe it’s not possible.

Thanks, I see what I have do wrong.

The problem was that the script is set to change te body of the presentation!

So… the problem is solved!