Trouble using handlers

Greetings,

I’m relatively new to AppleScript. I’ve puttered about with some easy scripts, but now I’d like to try something a little more advanced. The problem is, I’m getting my butt kicked and I don’t know why.

I have a rather large HTML source page (from an online, browser- based game) in a Tex- Edit Plus 4.9.7 document (running on OS X.3.9) that I want to work with. Somewhere in this page there are six blocks of text I want to run two operations on apiece: first is finding the verifying the block, then sorting and counting the elements in the block.

This sounds like a job for a pair of handlers to me, but I can’t make them work. I can write the script without the handlers, and it will work, but it is not elegant, it’s a huge script, and it’s a mess to look at and change. I would very much like to get those handlers working.

The error I’m getting is:

AppleScript Syntax error
Expected ", " but found class name.
-2741

in the handler code block, keyed on the word “window” right after "if (search ". If I take the code out of the handler and stick it in the body of the script, everything is fine. Put it in a handler, and things go whacky in short order.

I would very much appreciate any help anyone could offer. Neither Script Editor nor Script Debugger (which I’m just trying out) seems to be of much help with this.

tell application "Tex-Edit Plus"
	activate
	
	--Begin timing of the operation.  This is finished at the very end of the script
	set t to (time of (current date))
	
	--These elements mark where, in the HTML source file, the related info starts
	set AmmoStartMarker to "<form name=\"takeammo\" method=\"post\" action=\"/map/safe.do\">"
	
	--These variables initialize the text fields
	set AmmoText to ""
	
	--Window 1 contains the HTML source
	set name of window 1 to "SourceWindow"
	
	FindInventoryBlock("SourceWindow", AmmoStartMarker, "Ammo block") of me
	
	--Finish timing evaluation
	set total to (time of (current date)) - t
	display dialog "This script took " & (total as text) & " seconds to complete." with icon note
	
end tell

on FindInventoryBlock(theName, Marker, errorBlock)
	if (search window theName looking for Marker with cases matching) is true then
	
	--We have found the correct section.  Test to see if the paragraph of data exists. If it is correctly formatted, the first word of the paragraph will be "<select"
	if word 1 of paragraph (4 + ((paragraph offset of selection) of window theName)) of window theName is "<select" then
		
		--This sets AmmoText to the paragraph containing the listing of ammo, which is the fourth paragraph after the paragraph indicated by AmmoStartMarker 
		return paragraph (4 + ((paragraph offset of selection) of window theName)) of window theName
		
	else
		--There is some kind of problem with the data paragraph			
		display dialog "There is a problem with the data in the " & (errorBlock as text) & "!" with icon note
		
	end if
	end if
end FindInventoryBlock

Model: PowerBook PowerPC G4
AppleScript: 1.9.3
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.3.9)

What’s missing from the handler that is in the main script? … Look at how you call the handler:

FindInventoryBlock("SourceWindow", AmmoStartMarker, "Ammo block") of me

You are (correctly) using [url=http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.d3.html]of me[/url] because you need to “indicate that the subroutine is part of the script (not a command that should be sent to the object of the Tell statement).”

Note the emphasis (and the comment).

on FindInventoryBlock(theName, Marker, errorBlock)
	tell application "Tex-Edit Plus" -- you still need a tell block
		if (search window theName looking for Marker with cases matching) is true then
			if word 1 of paragraph (4 + ((paragraph offset of selection) of window theName)) of window theName is "<select" then
			return paragraph (4 + ((paragraph offset of selection) of window theName)) of window theName
			else
				display dialog "There is a problem with the data in the " & (errorBlock as text) & "!" with icon note
			end if
		end if
	end tell
end FindInventoryBlock

Outstanding! Thank you so much, Bruce. The few resources I checked didn’t clearly explain that topic, and I had no idea I should use tell within the Handle block.

If you don’t mind, I have a follow- up question. While trying to figure things out I was toying with Handlers and putting code in them. The example below works without any issues, but I can’t see how my first script and this new script are different. But they must be different, because this second example doesn’t require any tell statements in the Handlers to work properly.

Why aren’t tell statements needed? I can’t see it:

tell application "Tex-Edit Plus"
	activate
	newWindow("Lincoln") of me
	
	make new window with properties ¬
		{bounds:{115, 250, 246, 141}, name:"Ceasar"}
	
	copy "Four score, and a couple of years ago." to the beginning of window "Lincoln"
	
	changeWindow("Our forefathers did something interesting.") of me
	
	set theCount to countWords("Lincoln") of me
	
	theCounted("Ceasar", theCount) of me
	
	set theResults to FindTheWord("Lincoln", "Our") of me
	display dialog theResults with icon note
	
	
end tell


on newWindow(newName)
	make new window with properties ¬
		{bounds:{15, 50, 246, 141}, name:newName}
end newWindow



on changeWindow(newText)
	set contents of window "Lincoln" to newText
end changeWindow



on countWords(theName)
	set theNumber to count of words of window theName
end countWords



on theCounted(theName, total)
	set the contents of window theName to total as text
end theCounted



on FindTheWord(theName, Marker)
	if (search window theName looking for Marker with cases matching) is true then
		
		--We have found the correct section.  Test to see if the paragraph of data exists. If it is correctly formatted, the first word of the paragraph will be "<select"
		if word 1 of paragraph 1 of window theName is "Our" then
			
			--This sets AmmoText to the paragraph containing the listing of ammo, which is the fourth paragraph after the paragraph indicated by AmmoStartMarker 
			--set AmmoText to "Happy!"
			return "Happy!"

		else
			--There is some kind of problem
			return "There is a problem!"

		end if
	end if
end FindTheWord

Model: PowerBook PowerPC G4
AppleScript: 1.9.3
Browser: Firefox 2.0.0.6
Operating System: Mac OS X (10.3.9)