I'm not trying to be coy either, but I don't see that posting the whole script is going to help particularly because the perl script uses WWW::Mechanize to get data from a website where I have to provide user credentials - hence, it will not be possible to run it without those credentials.
The expectation is that I will get approximately ten lines of data back (which I put into a local variable). I then use an offset check to determine whether a particular string is present. If it is present, I then go on to compose a message (using Messages) to send to myself.
Regardless of the content, I open a local text file and insert the web response at the beginning of the file (as an activity log). The file is then closed for access and that's it.
I have only once seen an error when I ran the script from the AS Editor and this seemed to indicate a problem with the site's availability - so when Mechanize couldn't find any identifiable data, it threw an error.
The app is not saved as stay open, but it does. All I can say for certain is that the log file does not get written to, so I suspect that the problem lies with the perl script. It is for that reason that I wrapped it in a try...with timeout structure. (My understanding of the timeout, is that if script execution fails to complete within the timeout an error will be thrown, so this is picked-up by the try...on error. That understanding may be wrong.)
So, after all that, here is the script [with a single hard-coded address obscured for privacy]:
try
set desktopPath to (path to desktop) as alias
set FlightCheckLog to (desktopPath as text) & "FlightCheckLog.txt"
set targetList to {"Sep-7", "Sep-8", "Sep-9", "Sep-10"}
tell application "Messages"
set theBuddy to buddy id "<OBSCURED>"
end tell
set messageSent to false
try
with timeout of 300 seconds
set theFlights to do shell script "~/Documents/perl/FilghtCheck.pl -x -c J -d 2013-09-07 ATH-LHR"
end timeout
on error
set theFlights to ""
end try
repeat with theDate in targetList
set theOffset to (offsetoftheDateintheFlights)
if theOffset is not 0 then
set theFlight to text (theOffset - 5) thru end of theFlights
set theReturn to (offsetofreturnintheFlight)
set theFlight to text 1 thru (theReturn - 1) of theFlight
tell application "Messages"
sendtheFlighttotheBuddy
set messageSent to true
end tell
end if
end repeat
try
set theFile to open for accessFlightCheckLog with write permission
get eoftheFile
if result > 0 then
set fileText to readtheFile
set eoftheFileto 0
else
set fileText to ""
end if
if messageSent then
write ((current date) & return & theFlights & return & "MESSAGE SENT" & return & return & fileText) as text to theFile
else
write ((current date) & return & theFlights & return & return & fileText) as text to theFile
end if
close accesstheFile
on error
return
end try
on error
return
end try