Why won't this script run in "run applescript" action

This script runs perfectly as a stand-alone script. But when I add a “run applescript” to my automator and insert the script between the “On run” and the “End run” I get the following message"

syntax error: Expected “end” but found “on”. (The line “on get_name(the_path)” is highlighted)

Is the problemt that I have two “On” commands going on in the same script in the automator “run applescript” and if so, how do I get around it?

My automator workflow runs fine up to the point where I add the “run applescript” action. It creates the TextEdit documents, combines them into one textedit document and opens that document. I then run the applescript independently and and the script takes the paragraphs of that open document and flows them perfectly into the corresponding text boxes of my QuarkXPress document. But I need this script to work in the workflow.


		tell application "TextEdit"
			set boxCount to count of paragraphs of document 1
			set annoList to paragraphs of document 1
		end tell

		tell application "QuarkXPressâ„¢"
			repeat with i from 1 to count of items of annoList
				tell document 1
					set story 1 of text box 1 to (my get_name(item 1 of annoList)) as string
					set story 1 of text box 2 to (my get_name(item 2 of annoList)) as string
					set story 1 of text box 3 to (my get_name(item 3 of annoList)) as string
					set story 1 of text box 4 to (my get_name(item 4 of annoList)) as string
			
				end tell
			end repeat
		end tell

		on get_name(the_path)
			set old_delim to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {":"}
			set the_name to (text item -1 of (the_path as string))
			set AppleScript's text item delimiters to old_delim
			return the_name
		end get_name


Never mind. I found that if I deleted the “On run” and “End run”, in fact all the copy in the Run Applescript action and then inserted my applescript, it runs perfectly.