Need Help calling Handlers in long scripts.

Hi, I’ve noticed an annoying occurrence in my scripts. I can get handlers to work under simple circumstances, but when I put them inside of long scripts and inside of repeat loops, they don’t work. The handler does get called, but the function is not carried out. Here is an example of a handler that works just fine in the script below, but doesn’t work in my actual long script.

set OTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""

set VARtext to "      Glutaral   "

repeat with i from 1 to 10
	if i is equal to 5 then
		set VARtext to Truncate(VARtext)
	end if
end repeat
set AppleScript's text item delimiters to OTID
return VARtext

on Truncate(TargetText)
	set x to 1
	if character 1 of TargetText is equal to " " then
		beep
		repeat (number of characters in TargetText) times
			set test1 to character x of TargetText
			set test2 to character (x + 1) of TargetText
			if test1 is equal to " " and test1 is equal to test2 then
				set x to x + 1
			end if
		end repeat
		set TargetText to (characters (x + 1) thru -1) of TargetText as text
		return TargetText
	else
		return TargetText
	end if
end Truncate

This handler removes the spaces that are upstream of any text in the sent variable. This script works just fine under these conditions, but when I throw it into my long script it doesn’t perform it’s function at all.

I’m sure that there is a way to do this using a shell script so if anyone can let me know how it’s done that way I’d greatly appreciate it.

Are you sure it’s getting called?

This is just a guess, but is your handler call inside of a “tell application” block of code? If so then you have to preface the handler call with “my” so that the script knows the handler belongs to the script and not the application of the tell block. For example…

=== this won’t work ===

tell application "Finder"
	display_a_dialog_box("this doesn't work")
end tell

on display_a_dialog_box(theText)
	display dialog theText
end display_a_dialog_box

=== this will work ===

tell application "Finder"
	my display_a_dialog_box("this does work")
end tell

on display_a_dialog_box(theText)
	display dialog theText
end display_a_dialog_box

Yes, I’m calling the handler inside of a tell statement, inside of a repeat loop, and inside of an if statement…

I’ve called the handler with “my” but it doesn’t perform the function. I can tell that it is being called because the first “beep” in the handler is heard but the second beep (which is after the “if” statement) is not heard. This makes me think that the text class is not right inside when the handler is called. The text variable is coming from a list as shown below…
I can get the handler to work by setting the variable manually just prior to calling the handler…

This will call the script but it won’t perform the function


--Here is where I set the list
	set Shell_Capture2 to every paragraph of Shell_Capture as list

--Then inside of the repeat loop...
	set VARtext to item (INC_counter + 1) of Shell_Capture2 as text
			set VARtext to my Truncate(VARtext) as text  --> "                                   Glutaral     "

This will call the script AND perform the function


set VARtext to "                                   Glutaral     "
		set VARtext to my Truncate(VARtext) as text  --> "Glutaral     "

And by placing the beeps in these places I can tell that the handler is called in the first case because I hear only one beep. In the second case above I hear the second beep too…


on Truncate(TargetText)
	set x to 1
	beep
	delay 1
	if character 1 of TargetText is equal to " " then
		beep

So I’m pretty sure it has to do with the fact that I’m setting “VARtext” from a “paragraph” list. How can I set the text class to something that will work.

Thanks in advance,
-DW

Without seeing the whole script it’s difficult to determine where the problem lies. Here’s a couple tips which might help…

  1. set Shell_Capture2 to every paragraph of Shell_Capture as list
    There’s no need to use “as list” because by getting the paragraphs you already have a list

  2. set VARtext to my Truncate(VARtext) as text → " Glutaral "
    You probably don’t need “as text” here either

  3. Here’s a handler I use to remove spaces from the front of some text… try this instead of your handler

on removeLeadingSpaces(theString)
	repeat while theString begins with space
		set theString to text 2 thru -1 of theString
	end repeat
	return theString
end removeLeadingSpaces
  1. In case you need to remove trailing spaces from the text too then use this…
on removeTrailingSpaces(theString)
	repeat while theString ends with space
		set theString to text 1 thru -2 of theString
	end repeat
	return theString
end removeTrailingSpaces

Is INC_counter being incremented each time round the loop?

Hi regulus6633, thank you for the excellent short version of removing spaces, much better than my complex version.
Hi Nigel, yes, INC_counter is incremented around the repeat loop below…

Here is a short version of my script that still doesn’t work. I do get the “beeps” but I don’t get the result that I need…

Any Ideas to fix this are greatly appreciated!
-DW


set OTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""

tell application "Safari"
	open location "http://chem.sis.nlm.nih.gov/chemidplus/direct.jsp?regno=000111308"
	delay 5
	open location "http://chem.sis.nlm.nih.gov/chemidplus/ProxyServlet?objectHandle=Search&actionHandle=getAll3DMViewFiles&nextPage=jsp%2Fcommon%2FChemFull.jsp%3FcalledFrom%3Dlite&chemid=000111308&formatType=_3D"
	delay 5
	set theSource to source of the document 1
end tell

set Shell_Capture to do shell script "/bin/echo " & quoted form of theSource & " | /usr/bin/textutil -stdin -stdout -format html -inputencoding iso-8859-1 -convert txt -encoding UTF-8"
set Shell_Capture2 to every paragraph of Shell_Capture

repeat with INC_counter from 1 to number of items in Shell_Capture2
	if item INC_counter of Shell_Capture contains "Smiles" then
		set SmilesValue to item (INC_counter + 1) of Shell_Capture2
		set SmilesValue to my Truncate(SmilesValue) --> SmilesValue should be set to "C(CC=O)CC=O" but it doesn't get set at all
	end if
	if item INC_counter of Shell_Capture2 contains "Molecular Formula" then
		set MoleFormValue to item (INC_counter + 1) of Shell_Capture2
		set MoleFormValue to my Truncate(MoleFormValue) --> Should be set to "C5-H8-O2 " but " C5-H8-O2 " is returned
	end if
	if item INC_counter of Shell_Capture2 contains "Names and Synonyms" then
		if item (INC_counter + 1) of Shell_Capture2 contains "MeSH Heading" then
			set MeSH_name to item (INC_counter + 2) of Shell_Capture2
			set MeSH_name to my Truncate(MeSH_name) -->  MeSH should be set to "Glutaral " but "not defined" is returned
		end if
	else
		set MeSH_name to "not defined"
	end if
end repeat
return MoleFormValue
set AppleScript's text item delimiters to OTID

on Truncate(TargetText)
	beep
	repeat while TargetText begins with space
		set TargetText to text 2 thru -1 of TargetText
	end repeat
	return TargetText
end Truncate

Two coding errors.


if item INC_counter of Shell_Capture contains "Smiles" then

must be


if item INC_counter of Shell_Capture2 contains "Smiles" then

and after finding MeSH_name the next non matching item resets MeSH_name to “non defined”

The problem with the leading space character occurs because this character is no space character

try this, the text item delimiters lines are useless, because the script doesn’t use text items.
To filter the lines, the if – else if – endif form is better


tell application "Safari"
	open location "http://chem.sis.nlm.nih.gov/chemidplus/direct.jsp?regno=000111308"
	delay 5
	open location "http://chem.sis.nlm.nih.gov/chemidplus/ProxyServlet?objectHandle=Search&actionHandle=getAll3DMViewFiles&nextPage=jsp%2Fcommon%2FChemFull.jsp%3FcalledFrom=lite&chemid=000111308&formatType=_3D"
	delay 5
	set theSource to source of the document 1
end tell

set Shell_Capture to do shell script "/bin/echo " & quoted form of theSource & " | /usr/bin/textutil -stdin -stdout -format html -inputencoding iso-8859-1 -convert txt -encoding UTF-8"

set Shell_Capture2 to every paragraph of Shell_Capture
set MeSH_name to "not defined"
repeat with INC_counter from 1 to number of items in Shell_Capture2
	set theItem to item INC_counter of Shell_Capture2
	if theItem contains "Smiles" then
		set SmilesValue to item (INC_counter + 1) of Shell_Capture2
		set SmilesValue to my Truncate(SmilesValue) --> SmilesValue should be set to "C(CC=O)CC=O" but it doesn't get set at all
		
	else if theItem contains "Molecular Formula" then
		set MoleFormValue to item (INC_counter + 1) of Shell_Capture2
		set MoleFormValue to my Truncate(MoleFormValue) --> Should be set to "C5-H8-O2 " but " C5-H8-O2 " is returned
		
	else if theItem contains "Names and Synonyms" and item (INC_counter + 1) of Shell_Capture2 contains "MeSH Heading" then
		set MeSH_name to item (INC_counter + 2) of Shell_Capture2
		set MeSH_name to my Truncate(MeSH_name) -->  MeSH should be set to "Glutaral " but "not defined" is returned
	end if
end repeat
return MoleFormValue


on Truncate(TargetText)
	beep
	repeat while TargetText begins with space or id of character 1 of TargetText > 256
		set TargetText to text 2 thru -1 of TargetText
	end repeat
	return TargetText
end Truncate

Note: id of character works only in AppleScript 2 (Leopard)

StefanK,

You’ve saved my day again. I’ve now learned the lessons of setting variable names that make sense to me and thinking about text differently. Thank you for this elegant solution.

regulus6633,

Thank you again for the efficient Handlers that truncate the spaces off the beginning and end of text.

Best,
-DW