# Apple mail initiated AppleScript.



## Gambuchi (Jun 11, 2007)

I am trying to impliment a applescript I got from one of the forums to put my computer to sleep using the arrival of a 'certain' email to trigger an applescript... here is the information you might need to help me figure why it refuses to work.:

The email arrives in the inbox but will not execute the applescript but if I run the applescript outside of mail, it works perfectly.  I am lost.  If you can help, please do so


----------



## supanatral (Jun 19, 2007)

I'm not exactly sure as to how to get it to work, but I know why it's not working. Your Apple script is only programed to show a dialog box to ask the user whether they want the computer to be put to sleep or not. Nothing in that script tells it to look for whether there is any mail present.

It's not the script that is causing a problem. The problem that the script isn't being launched when you get that mail.


----------



## bobw (Jun 19, 2007)

Try this;

tell application "System Events" to sleep


----------



## supanatral (Jun 19, 2007)

bobw said:


> Try this;
> 
> tell application "System Events" to sleep



Bobw: What is a good website that you would suggest for someone that wants to learn how to write an applescripts?


----------



## bobw (Jun 19, 2007)

Take a look  Here  and Here


----------



## Gambuchi (Jun 20, 2007)

> I'm not exactly sure as to how to get it to work, but I know why it's not working. Your Apple script is only programed to show a dialog box to ask the user whether they want the computer to be put to sleep or not. Nothing in that script tells it to look for whether there is any mail present.
> 
> It's not the script that is causing a problem. The problem that the script isn't being launched when you get that mail.


Actually, supanatral, the mail program does call the script and the script does launch... the problem is that it hangs just before the "display dialect" line and goes no further or possibly drops out at that point.  I've placed some 'beep' commands in it to determine how far it gets so I know it does at least start to work.  Odd thing is that if I run the script from the desktop, it works perfectly.  I cannot, for the life of me, figure out why it stops right at the 'display dialog' line of code.  Perhaps you have an answer for me... also there is a line there that tells the system to 'sleep' and that works from desktop running also.  I am curious is perhaps an earlier version of 'Mail' would run it properly and if that is the case then the problem could be related to a programming change in subsequent versions of Mail.


----------

