I’m new to Macs, and new to AppleScript and I am totally stumped on what I thought would be an easy thing to do. The task I want to accomplish is: when an email with a ‘keyword’ in the subject arrives in Mail, it should copy the body of the message to a text file.
So I set up a mail rule to select messages with the ‘keyword’ in the subject, telling it to run a script (and also to change the color of the message, just so I can make sure the rule is working).
Here’s the little script I’m using (I found it somewhere on this forum, I think - it’s not written from scratch because I’m just learning this stuff):
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Finder" to set ptd to ("iMac:Users:Laura:Documents:" & "Add to Things:") as string
tell application "Mail"
repeat with theMessage in theMessages -- loop through the messages sent by Mail
set theText to content of theMessage -- retrieve message body
set theFile to ptd & "Things to Do " & (theMessage's id as string) -- create file name
set theFileID to open for access file theFile with write permission -- open the new file for writing
write theText to theFileID -- write the body text of the current email into the new file
close access theFileID -- close the file
end repeat
end tell
end perform mail action with messages
end using terms from
So far so good. When I receive a message that meets the rule, however, the script is (apparently) not run. The color gets changed but no text document gets created. However, if I highlight the message in Mail and choose ‘Apply Rules,’ then the text document does get created! So it seems like the script runs and does what I want it to, but Mail won’t run it automatically when a message matching the rule arrives. I’m totally stumped! Is there some tweak of the script that can be done to coax Mail into running it automatically? Have I missed something fundamental in what I’m trying to do?
PS. I’m running 10.5.1 on an iMac
Browser: Firefox 2.0.0.11
Operating System: Mac OS X (10.5)
I have had similar issues in the past, and what works for me is to use a handler to get everything out of the Mail tell block that does not need to be there:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
tell application "Finder" to set ptd to ("iMac:Users:Laura:Documents:" & "Add to Things:") as string
repeat with theMessage in theMessages -- loop through the messages sent by Mail
set theText to content of theMessage -- retrieve message body
set theFile to ptd & "Things to Do " & (theMessage's id as string) -- create file name
end repeat
my WriteIt(theFile, theText)
end tell
end perform mail action with messages
end using terms from
to WriteIt(a, z)
set theFileID to open for access file a with write permission -- open the new file for writing
write z to theFileID -- write the body text of the current email into the new file
close access theFileID -- close the file
end WriteIt
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
set ptd to my SetFolder()
tell application "Mail"
repeat with theMessage in theMessages -- loop through the messages sent by Mail
set theText to content of theMessage -- retrieve message body
set theFile to ptd & "Things to Do " & (theMessage's id as string) -- create file name
end repeat
my WriteIt(theFile, theText)
end tell
end perform mail action with messages
end using terms from
to WriteIt(a, z)
set theFileID to open for access file a with write permission -- open the new file for writing
write z to theFileID -- write the body text of the current email into the new file
close access theFileID -- close the file
end WriteIt
to SetFolder()
set ptd_handler to (path to documents folder as Unicode text & "Add to Things:")
return ptd_handler
end SetFolder
Thanks for that, but alas, I get the same behavior as with the version I was first using. That is to say, a matching message comes in and its color gets changed, but the script doesn’t run. If I select the message and do ‘Apply Rules,’ then the script does get run and the file gets created. (I am using the second version of your script.)
It just seems like it ought to be such an easy thing! I’m bummed - but thanks much for your help. If you think of anything else I can try, please do let me know!
PS - As I said, I’m new to the OS X operating system after years of being a Windows user. So I don’t really know enough yet to know what to look for, but do you think this might have something to do with file permissions or somesuch? Maybe Mail doesn’t have the necessary permissions to create a file on its own, but when it’s a user-initiated action in Mail, it does? Grasping at straws here…
One more thing - again, this is a total newbie thing - but when I used your script ‘as is’ it didn’t work, either as an automated rule or when I did the ‘Apply Rules’ thing in Mail. I changed the ‘path to documents’ part to the actual path (like “computer:Users:me:Documents”) and then it worked (but only when doing the ‘Apply Rules’ action in Mail). Not sure if that’s relevant or not - not sure if I was supposed to change it or if that ‘path to documents’ type thing is a system set variable.
Well, this is downright infuriating. I pulled up a script that I use (that functions just fine) as a Mail rule, and copied all the pertinent commands to come up with this:
using terms from application "Mail"
on perform mail action with messages theMessages
set ptd to my SetFolder()
tell application "Mail" to repeat with theMessage in theMessages -- loop through the messages sent by Mail
set theText to (get theMessage's content) -- retrieve message body
set theFile to ptd & "Things to Do " & (theMessage's id as string) -- create file name
set theFileID to open for access file theFile with write permission -- open the new file for writing
write theT to theFileID -- write the body text of the current email into the new file
close access theFileID -- close the file
end repeat
end perform mail action with messages
end using terms from
to SetFolder()
set ptd_handler to ((path to documents folder) as Unicode text) & "Test:"
return ptd_handler
end SetFolder
And, it does not work for me, either, even when I do the Apply thing you got to work. Well, it did actually process one message with the Apply thing, but it should have processed about a dozen. Anyway, if I get some time this weekend, I will play some more with it.
The major problem with running scripts in Mail is that Mail won’t report Applescript errors. If something goes wrong, the script just dies a quiet death.
For this reason, I always suggest you use a try…on error block around any code that doesn’t seem to work. Use the “Error Dialogs” contextual menu in Script Editor and choose “Message and OK” or “Message and Cancel” as these will give you error number and message for the offending problem area.
it works as intended when operating on a selection of messages in Mail, but not completely when operating on incoming mail, as the subject field doesn’t change when it should. yet the virus program (clamAV) logfile shows that the script runs and so does putting in dialog box popups – but the subject field doesn’t get changed on incoming messages.
paul
Model: MBP C2D
Browser: Opera/9.25 (Macintosh; Intel Mac OS X; U; en)
Operating System: Mac OS X (10.4)
Mail Rule’s are not normally the problem. Normally there is an error in the script.
It’s a good idea to have a debug written into the script. this allows you to track where the script is stopping
You have an error in the script.
write theT to theFileID – write the body text of the current email into the new file
should be
write theText to theFileID – write the body text of the current email into the new file
I also the script gets stuck on getting the mails id. This maybe because it does not have one yet.
Here I have commented out the id part.
using terms from application "Mail"
on perform mail action with messages theMessages
set debug to true
if debug is true then say "starting"
set ptd to my SetFolder()
if debug is true then say "have folder"
--tell application "Mail" to set theMessages to (get selection) --for testing
tell application "Mail" to repeat with theMessage in theMessages -- loop through the messages sent by Mail
set theText to (get theMessage's content) -- retrieve message body
if debug is true then say "got contents"
delay 2
set theFile to ptd & "Things to Do " --& (theMessage's id as string) -- create file name
if debug is true then say "set name"
set theFileID to open for access file theFile with write permission -- open the new file for writing
if debug is true then say "open file"
write theText to theFileID -- write the body text of the current email into the new file
close access theFileID -- close the file
if debug is true then say "closed file"
end repeat
end perform mail action with messages
end using terms from
to SetFolder()
set ptd_handler to ((path to documents folder) as Unicode text) & "Test:"
return ptd_handler
end SetFolder