Repeat until word

I am very new to applescript but have a very basic script i want to run. I need it to run until it reaches the word FINISH in text edit. I have searched around for ages and not been able to find a solution.

This is my current script:

tell application “TextEdit” to activate
tell application “System Events”

key code 125
key code 51
key code 0
key code 125
key code 51
key code 124

end tell

Welcome to MacScripter.

I don’t think we can tell what you’re asking, I don’t know what you mean by “reach the word finish in Text Edit.” Is it parsing words in a text edit document one at a time?

Locating the occurrences of the word “finish” inside a Text Edit document is not difficult.

Your current script is just typing:
down arrow
delete
“a”
down arrow
delete
right arrow

If you tell us what your script is actually intended to accomplish, we’d be better equipped to help.

This script tells you the count of the word “finish” in the front TextEdit document, but I can’t tell from your question if this helps with what you’re trying to accomplish.


set searchWord to "finish"
tell application "TextEdit"
	set wordList to the words of the front document
	set wordFound to false
	repeat with i from 1 to count of wordList
		if item i of wordList is searchWord then
			set wordFound to true
			exit repeat
		end if
	end repeat
	if wordFound is false then display dialog "Searched to end of current TextEdit document without finding the word \"" & searchWord & "\""
end tell

return i