Jump to content
Server Maintenance This Week. ×

Sending a timed email from inside filemaker


This topic is 4690 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Hello, I have a database that runs every weekday which takes about 14 hours to run. I have a layout that monitors the progress of each of the multiple scripts (38 in total) in my database. This progress monitor layout has a field for each script in the database so that when each of the 38 scripts is completed the corresponding field is marked with a timestamp. In addition to the completion timestamp field each script also has a field that that upon completion of the previous script an estimated completion time of the current script is calculated and placed in the estimated completion time field. The purpose of this is to be able to monitor the progress of the multiple scripts and to determine if the program has encountered an error that is causing the script to hang. What I would like to be able to do is set up the database so that if a script does not complete by the time indicated in its estimated completion time field then I am sent an email. I believe that the way to handle this is to write an applescript that is performed at the completion of a script which uses the information from the estimated completion time field of the upcoming script and then tells my Mac to send me an email at that time, unless the script completes on time in which case the send email script is cancelled. The problem with this is that I don't know how to insert the time from the estimated completion time field into an applescript that will be run using the PerformApplescript script step. I also do not know how to get an applescript to send an email at a certain time. So just to iterate. 1) a script completes 2) the upcoming script estimated completion time is calculated 3) an applescript is performed which tells my mac to send me an email at the estimated completion time saying the script did not complete. 4) Either the script completes within the allotted time and the send email applescript is canceled or the the script does not complete on time and the email is sent. I have a fair understanding of Applescript, so writing a script to send an email should not be too difficult:

tell application "Mail"

set theNewMessage to make new outgoing message with properties {subject:"Subject text", content:"Content text", visible:true}

tell theNewMessage

make new to recipient at end of to recipients with properties {address:"email address"}

send

end tell

end tell

However, how to make this send the email at a designated time is beyond my skill level. Perhaps Automator could be used in some fashion. If anybody has any thoughts on how to accomplish my goal I would appreciate any help you could provide. Sincerely, George

Link to comment
Share on other sites

There is a very big difference between "script did not complete" and "script did not complete on time". In the first case, the script is still running and everything else will have to wait. If your purpose is "to determine if the program has encountered an error that is causing the script to hang", you'll need to rethink your approach. For example, you could insert checkpoints within the scripts themselves, so that if they get stuck they can do something about it. Of course, that doesn't cover a crash or similar failure.

---

BTW, 38 scripts and 14 hours?? Perhaps some optimization can reduce that?

Link to comment
Share on other sites

Hello, Yes, I have just spent the last three months optimizing the code to get it down to 14 hours. What I am looking for is a way to get the database to send me an email when a given script does not complete within its estimated completion time. My current script progress layout does provide me with checkpoints that allow me to monitor the progress of the database as it works it's way through the scripts.That is all fine when I am in the same location as my database but I am trying to create a system that alerts me to a problem if I am away from my system, for instance if I'm traveling. Sincerely, George

Link to comment
Share on other sites

Hello, Thank you for your response. The problem I see with the approach you suggest is that the script would have to complete before the send email step was used. This would mean that in the event of a script crash the send email step would never be reached and I would never get the email alerting me to the problem. Or in the event that the script does not complete within it's estimated completion time I would not receive the email notifying me of the problem until after completion time had passed. I envision writing an AppleScript that is initiated in filemaker that accesses the estimated completion time before the script in question is even begun. This way if the script crashes or just takes a long time, the email notifying me of the problem still gets sent. This initial AppleScript could then initiate another AppleScript that is resident on my system outside of filemaker, the second script would accept as input the time from the first filemaker AppleScript. That way control of the filemaker script would then return to filemaker without having to wait for the AppleScript which completes the send email to complete. What this means, I believe, is that the first real issue I have to address is how do I pass a value from a field within filemaker to a variable within an AppleScript. Thanks again, George

Link to comment
Share on other sites

The problem I see with the approach you suggest is that the script would have to complete before the send email step was used.

Well, yes - that's what I said in the beginning. I thought your reply meant you wanted mail sent only if the script was late to complete.

Now, if Filemaker crashed then nothing else will happen. This means you have to send the mail unless something happens, and you have to send it from AppleScript, not from Filemaker - for example something like:

tell application "FileMaker Pro"

	do script "Script 1"

	delay 500

	try

		if cell "gCompleted" = "1" then do script "Script 2"

	on error

		my mailme()

	end try

end tell



on mailme()

	...

end mailme

Link to comment
Share on other sites

Hello, thanks for your response. I must admit I don't understand your AppleScript. Since I need to have the email sent via an AppleScript not from filemaker why am I telling filemaker to run either script 1 or script 2? Is the delay of 500 seconds supposed to correspond to the time the script is estimated to take to complete? Is the script 2 within the If condition the script that is sent from AppleScript rather than from inside filemaker? Inside the if cell "gCompleted" = "1" statement is the AppleScript reading the value of "gCompleted"? Is that how I pass the estimated completion time into the AppleScript? Or is "gCompleted" = "1" a test to show that if the script completes then run the external AppleScript which sends the email, if that is the case then the send email script doesn't run if the script never completes(crashes). I'm assuming I need to have an AppleScript that exists outside of filemaker that is activated when it receives the estimated completion time of the filemaker script which is just about to begin. With the estimated completion time being sent to the "outside" AppleScript via an AppleScript coming from within filemaker. Once the "outside" AppleScript has the correct send time then it schedules the email to be sent at the correct send time. Thanks for your help, I wish I understood it better. Sincerely, George

Link to comment
Share on other sites

I'm assuming I need to have an AppleScript that exists outside of filemaker

Yes - otherwise there will be no one around to send the mail if Filemaker crashes.

In my example, I chose to call the Filemaker scripts from AppleScript. The assumption is that Filemaker script "Script 1" will set field gCompleted to 1 when completed. If so, AppleScript will call the next script, and so on. This is not the only possible way - it just seemed the simplest one to me.

Link to comment
Share on other sites

Hello, Thanks again for your response. I'm still very confused. If I understand you correctly you are saying that I should have an external AppleScript that controls the flow of my filemaker scripts. Since I already have a good amount of effort into having the control of my scripts occur within filemaker I am reluctant to switch the control to an external AppleScript. Even if I were to make the change I believe the AppleScript code controlling the sending of the email would have to wait for an error condition to occur before an email would be sent to me. I envision situations where the script could hang without generating an error and so no email would be sent. What I would prefer is to find a way to generate an AppleScript within filemaker that includes either the estimated completion time of the script which is about to be run or the estimated length of time the script will run. Essentially I would like to be able to insert a specified field value into an AppleScript which would then be run from inside filemaker using the PerformApplescript script step. Then once this filemaker run AppleScript is executed it serves to pass the field value to a second AppleScript that resides outside of filemaker and it is then this second script which then sends the email at the appropriate time. The script you suggested gave me the idea that instead of trying to write a second external AppleScript that schedules an email using the estimated completion time value, the second script could just begin with a delay that is equal to the length of time the script is estimated to run and unless the second script is cancelled by the condition of the filemaker script completion then it sends the email. So I guess I should ask you do you know of any way to have an AppleScript that is generated within filemaker that can read the value of a field when the PerformApplescript step is invoked? Again thanks for all the help, I really appreciate it. Sincerely, George

Link to comment
Share on other sites

If I understand you correctly you are saying that I should have an external AppleScript that controls the flow of my filemaker scripts.

No, I am saying you must have an external AppleScript that monitors the progress of your Filemaker scripts. You could have that same AppleScript run the entire show, but that's up to you.

I envision situations where the script could hang without generating an error

The error monitored here is an AppleScript error - when it's unable to get data from a Filemaker field. Perhaps it would be better to make that part:

...

try

	if 	cell "gCompleted" = "1" then 

		do script "Script 2"

	else

		my mailme()

on error

	my mailme()

end try

...

do you know of any way to have an AppleScript that is generated within filemaker that can read the value of a field when the PerformApplescript step is invoked?

I don't see why you need this. The external AppleScript can read the value of a Filemaker field directly.

Link to comment
Share on other sites

Hello, thank you for your response. I believe I am beginning to understand you. I still don't think this approach will address my concerns. Any solution that checks to see if the completed script field has been completed must be timed in order to determine if the script has not taken longer than expected to complete. So it is not clear to me how the AppleScript knows when to check the gCompleted field to determine if an error exists. I am also concerned that the external AppleScript may not be able to access to appropriate script monitoring layout. Every day when I run the database I start with a fresh script progress monitor layout, how would the external AppleScript know which layout record to check the gCompleted field on? Also if the filemaker script did complete on time and was then running the next filemaker script then the layouts associated with that particular script would be visible rather than the script progress monitor layout, what would then happen if the AppleScript tried to read the state of gCompleted, how would it gain access to the value of gCompleted? I really would rather just have an AppleScript that can read the value of the progress monitor layout field that contains the estimated length the script will run, then using that estimated time send an email accordingly, that would alleviate any of the concerns I previously cited. This I why I am trying to figure out how to get the AppleScript to be able to read the value of the field when the AppleScript is generated in the PerformApplescript step. Here is the information flow that I believe would address all the field access problems and script error types I can envision:

1) script 1 completes

2) the script progress monitor layout comes to the front

3) the script 1 completed field is time stamped

4) the estimated completion time of script 2 and the estimated length of time for script 2 to complete are calculated

5) an AppleScript within a PerformApplescript script step acquires the estimated length of time for script 2 to complete

6) this filemaker internal AppleScript is performed using the PerformApplescript script step.

7) the internal AppleScript passes the script 2 estimated time to an external AppleScript

8) the external AppleScript begins a delay corresponding to the estimated script 2 run time

9 the internal AppleScript has completed and passes control back to filemaker so that script 2 can run while the external AppleScript is processing it's delay

10) script 2 does not completes within the allotted time so the external AppleScript sends the email

11) or script 2 completes within the allotted time and the progress monitor layout comes to the front

12) the script 2 complete field is time stamped

13) an AppleScript is sent from within filemaker using the PerformApplescript step which cancels the currently running external AppleScript for filemaker script 2

14) the process begins anew with step 4) for filemaker script 3

This process would always insure that the appropriate scripts would have access to the estimated script run time information at the appropriate juncture in the script sequence. This requires that there be an internal AppleScript that can pass the value of the estimated script run time to the external AppleScript either directly or by telling the external AppleScript that it is now time to read the estimated script run time from within filemaker. It is because I believe that this sequence of events is needed to insure that all possible script error types are accounted for that I am trying to figure out how to pass the estimated script run time value from within the filemaker script progress monitor layout field to the external AppleScript. Again thank you for your help it has without doubt helped me to clarify all the issues I am facing. Sincerely, George

Link to comment
Share on other sites

Can't help but wonder what the whole purpose of these scripts is. This forum has many people that are new to FM and I would like to point out that what you're doing is not a typical setup. Is this some sort of massive import?

Link to comment
Share on other sites

how would the external AppleScript know which layout record to check the gCompleted field on?

You need to check out Filemaker's dictionary in AppleScript Editor.

In any case, I see no reason why you couldn't do it the way you describe. Assuming you have created an AppleScript application with the appropriate handlers, you can then use the Perfom AppleScript[] step to control it, just like any other scriptable application - see:

http://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html#//apple_ref/doc/uid/TP40000983-CH206-SW17

Link to comment
Share on other sites

Can't help but wonder what the whole purpose of these scripts is. This forum has many people that are new to FM and I would like to point out that what you're doing is not a typical setup. Is this some sort of massive import?

Hi Moderator, I basically have a database that collects data from the internet, the first script, and then performs lots and lots of calculations looking for patterns and then correlating those patterns to various outputs. I am a chemical engineer with a background in statistical analysis and biochemistry. The primary goal of my database is to monitor clinical trial test results and other reported scientific results so that I can maintain a current knowledge base of the state of the biotech industry. I am not a spammer who is collecting hugh databases of say, I don't know, facebook data scrapes or anything like that. I'm retired but am trying to stay abreast of a very active and complicated discipline. Sincerely, George

You need to check out Filemaker's dictionary in AppleScript Editor.

In any case, I see no reason why you couldn't do it the way you describe. Assuming you have created an AppleScript application with the appropriate handlers, you can then use the Perfom AppleScript[] step to control it, just like any other scriptable application - see:

http://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_about_handlers.html#//apple_ref/doc/uid/TP40000983-CH206-SW17

Hi Consultant, Thanks again for all your help. You have really helped me clarify my thoughts on what needs to be done and given me some excellent insight on how to proceed. I hope I haven't been too thick headed as I tried to understand your code. Thanks again, George

Link to comment
Share on other sites

This topic is 4690 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.