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