Error running a script

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Error running a script Sun Apr 08, 2012 12:56 pm • by flynn
So basically I tried to set up a self-sending email with a file that comes from a folder.
In my first folder, hazel watches for .pdf files created now, and sends them to a second folder. Once in that second folder, I have an apple script that should create and send an email with that file, and then hazel should put that file back into the original folder appending _sent so that it doesn't send it again.

My problem is that I get an error when hazel runs the script, whereas the script runs perfectly well without it.
Please help !!

Code: Select all
tell application "Finder"
   set folderPath to folder "Macintosh HD:Users:user_directory:Desktop:Send_Mail:"
   set theFile to first file in folderPath as alias
   set fileName to name of theFile
end tell


tell application "System Events" to set isMailRunning to (name of processes) contains "Mail"

tell application "Mail"
   set theSubject to fileName
   set theBody to "Hello Sir. Here is my " & fileName
   set theAddress to "some address"
   set theAttachment to theFile
   set theSender to "some sender"
   
   
   
   set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true}
   tell theNewMessage
      set visibile to true
      set sender to theSender
      make new to recipient at end of to recipients with properties {address:theAddress}
      
      try
         make new attachment with properties {file name:theFile} at after the last word of the last paragraph
         set message_attachment to 0
         log "message_attachment = " & message_attachment
      on error errmess -- oops
         log errmess -- log the error
         set message_attachment to 1
      end try
      
      #end tell
      if isMailRunning = false then delay 10
      
      send
   end tell
end tell
if isMailRunning = false then
   tell application "Mail"
      quit
   end tell
end if
flynn
 
Posts: 66
Joined: Sun Apr 08, 2012 12:41 pm
Location: Philippines

Re: Error running a script Mon Apr 09, 2012 11:19 am • by Mr_Noodle
Check the logs and post any Applescript errors you see there.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Error running a script Mon Apr 09, 2012 11:34 am • by a_freyer
I havent testd yet, I would bet that your error is coming from the isMailRunning logic. It may take a few seconds for mail to launch if it is not open, and portions of your script may be lost.

I'd add an Activate or a Run right after the mail tell, and perhaps a few seconds of delay before the new message object is created.

Try running the rule with mail open to see if this is the case.


And just to be clear, this script is in the actions part of the rule, not the matching section yes? If this is the matching section, the rule will return an error because you are not returning the proper values.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Re: Error running a script Mon Apr 09, 2012 12:36 pm • by flynn
I realized you could embed the code directly into hazel, I tried that and it worked. (the code was running fine without hazel)
I think the problem might be coming from hazel, as I had the same problem with a shell script that I embed instead of linking to a file.

To answer to question/suggestions in previous post:

Mail was running when I did the test, since it didn't close at the end because the code didn't complete successfully. I never closed mail after that. And yes, the script was in the action part, not the conditions. (also the isMailRunning part was added later on to my code, after I got errors when running the script through hazel. I just updated the script here so that it could match my actual script, but I doubt this made any differences).

For Mr_noodle, I will try to gather AppleScript error logs later so that you could check whether the problem was in fact coming from hazel or not, and try to fix the problem if it is.
flynn
 
Posts: 66
Joined: Sun Apr 08, 2012 12:41 pm
Location: Philippines


Return to Support