Applescript behavior different when called via script menu

The following applescript takes a chunk of text in the clipboard and, using each line as a separate term to search on, initiates a new search for every line. Works fine when I run it from the script editor, but when accessed through the Devonagent script menu, it doesn’t hit the return key to start the search (I’m using System Events, but if there’s a way to activate the search that’s easier, please let me know).

set ClipContents to get the clipboard
set theLines to paragraphs of ClipContents
repeat with eachLine in theLines
	tell application "DEVONagent"
		activate
		search eachLine using set "Internet (Fast Scan)"
	end tell
	delay 2
	tell application "System Events"
		keystroke return
	end tell
end repeat

The keystroke seems to replace the search string, clicking the button like this…


set ClipContents to get the clipboard
set theLines to paragraphs of ClipContents
repeat with eachLine in theLines
	tell application "DEVONagent"
		activate
		search eachLine using set "Internet (Fast Scan)"
	end tell
	delay 1
	tell application "System Events"
		tell process "DEVONagent"
			tell window 1 to click button "Start"
		end tell
	end tell
end repeat

…doesn’t seem to work either. One workaround is to add the script to ~/Library/Scripts/Applications/DEVONagent and to use the menu extra.

I made a small change in the way “keystroke enter” was called. Seems to work now.

set ClipContents to get the clipboard
set theLines to paragraphs of ClipContents
repeat with eachLine in theLines
tell application “DEVONagent”
activate
search eachLine using set “Internet (Fast Scan)”
end tell

delay 2
tell application "System Events"
	keystroke (key code 76)
end tell

end repeat