Program Automatically Quits... Why though?

I have this program that I’ve been working on. It takes an E-Mail out of a certain mailbox in “Mail”. It then parses it to find the first link in the e-mail. Then it open’s Safari and loads the URL for a designated period of time. This goes on and on for every e-mail in the box. But for some reason this code always quits on me after it goes through once or twice. This is weird because it’s random and will sometimes go through once, sometimes twice, but never more than that. If anyone has any ideas why it might be doing that they would be very much appreciated as I have spent a decent amount of time trying to debug this. Here is the code:


startProgram()

on startProgram()
	set x to 1
	set linkList to {}
	repeat while retrieveMail() is true
		runLink(linkFind(getContent()))
	end repeat
	beep
end startProgram

on programActivate()
	tell application "Safari"
		run
	end tell
	tell application "Mail"
		run
	end tell
end programActivate

on retrieveMail()
	tell application "Mail"
		if (count of messages in mailbox "Mailbox 1") > 0 then return true
		return false
	end tell
end retrieveMail

on linkFind(stringPassed)
	set linkList to {}
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
	set wordList to text items of stringPassed
	set AppleScript's text item delimiters to TID
	set stringLength to count of wordList
	repeat with i from 1 to stringLength by 1
		if (item i of wordList) contains "http:" then
				if item i of wordList contains "pages" then
					set text item i of wordList to ""
					delete text item i of wordList
				end if
				if item i of wordList contains "google.com" then
					if item i of wordList contains "run" and (count of items in linkList) is equal to 0 then
						set o to offset of "http:" in (text item i of wordList)
						set p to offset of "</a>" in (text item i of wordList)
						set t to text (o + 74) thru (p - 1) of text item i of wordList
						copy (t) to the end of linkList
					end if -- This if statement looks confusing but it works correctly.
				end if
		end if
	end repeat
	return linkList
end linkFind

on getContent()
	tell application "Mail"
		set messageSource to source of first message of mailbox "Mailbox 1" as string
		return messageSource
	end tell
end getContent

on runLink(listPassed)
	tell application "Safari"
		repeat with i from 1 to count of items in listPassed by 1
			open location item i of listPassed
			delay 5
			close front window
		end repeat
	end tell
end runLink

on deleteMessage()
	tell application "Mail"
		delete (first message in mailbox "Mailbox 1")
	end tell
end deleteMessage

Now I have taken the linkFind() function out of the code and placed it in a separate file along with the getContent() function and those have returned the list that I am looking for. So I know that isn’t a problem. It’s strange though that it works a couple of times and then doesn’t work the rest of the time. Thank you in advance for any help on this.

Henry

Hi,

You can take this with a grain of salt, but when things like this happen to me, cut the program down.

I didn’t read your program, but I might try to use subroutines that work all the time. Run the program step by step and look at the results. I usually do this as I’m creating the program unless things are obvious. Finally, if you think that your program should work, but it doesn’t, add logs to see the values of your variables. Note that log is an AppleScript Command. You can also use the ‘display dialog’ or ‘beep’ s to debug your program.

gl,

one other thing is that a program will quit unexpectedly if you do a quit from a tell block. Bigginers will often quit the Finder not knowing why, because it was within a Finder tell block. You might look for this also.

gl,

I just did it again and after two runs it quit itself while it was trying to get the link out of the email. This is curious because this part works perfectly when I try it by itself.

On second look, it always goes through two emails perfectly but then gets held up on the third email. This is constant and it always gets held up during the linkFind function. Could it be that the applescript is overloaded?

try looking at the third email and its properties. Maybe you can find why it is errorring. You’re building debugging knowledge.

gl,