Error running a script

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 !!
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