I am a JavaScript writer for InDesign, and I am desparately looking for a ‘simple’ appleScript to monitor my selected mailbox in Mail.app, and when a new message arrives the text from the body of the message gets saved into a folder as a txt file. This will then trigger another script to do other bits, but it is the mail side I need some assistance with.
If it cannot be done, then please say do.
Many Thanks
Royston
Model: Powerbook G4
AppleScript: 1.10.7
Browser: Firefox 1.5.0.4
Operating System: Mac OS X (10.4)
I had some time so I cobbled this together.
It saves the message text to a text file on the desktop - you can easily specify a different location. The text file name is the mail subject and id.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with thisMessage in theMessages
set theText to content of thisMessage
tell thisMessage
set fileName to subject & "_" & (id of thisMessage as string) & ".txt"
end tell
--this part removes colons from the file name so the path is correct
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set newName to (text items of fileName)
set AppleScript's text item delimiters to "_"
set fileName to newName as string
set AppleScript's text item delimiters to TID
set theFile to ((path to desktop folder) as string) & fileName
set theFileID to open for access file theFile with write permission
write theText to theFileID
close access theFileID
end repeat
end perform mail action with messages
end using terms from
Hope that helps.
j
Thanks alot for the quick response.
As I know very little (at the moment) about applescript, can you answer a few questions about it for me:-
If I save this as an application, will it work for new messages automatically, or does it have to run for each message?
It looks like I have to have a rule set up. What do I need the rule to do?
What other info does a beginner with this script need to get it going?
I appreciate you may be busy, but I could do with some more help.
Thanks again,
Royston
I have a few minutes to spare - be right back.
j
Hi Roy.
The above script will be triggered by a mail rule - it will run on any message that meets your preset conditions. There is a choice in Mail >Preferences>Rules to attach a script to a rule.
The rule needs to distinguish messages that you want to save from those you don’t. Are all messages you want to be processed by the script kept in the same mailbox? Is there something else they all have in common - sender, subject, etc?
If you want to process messages manually, this will work as an application on selected messages:
tell application "Mail"
set thisMessage to item 1 of (get selection)
set theText to content of thisMessage
tell thisMessage
set fileName to subject & "_" & (id of thisMessage as string) & ".txt"
end tell
--this part removes colons from the file name so the path is correct
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set newName to (text items of fileName)
set AppleScript's text item delimiters to "_"
set fileName to newName as string
set AppleScript's text item delimiters to TID
set theFile to ((path to desktop folder) as string) & fileName
set theFileID to open for access file theFile with write permission
write theText to theFileID
close access theFileID
end tell
You mentioned that you wanted the files saved in a specific folder. Which folder is that, and is it in a permanent location?
j
Hi J
I am greatful for your time. Let me tell you what is behind this…
My aim is to have a link on my companies web site that will send data, as mail content to my Mac in my design studio. What I want is for when my Mail app recieves a mail with the subject “automation” (or something common) the mail contents gets saved as a text file into a folder, on my hard disk, which will have a folder options attached that will run an applescript, to start an InDesign JavaScript (which is where my skll comes in) to format a page with data from the txt file!!!
This will then be converted to a low res PDF file, and (via hot folder) be uploaded to an FTP location.
I know this sounds ambitious, but I have the InDesign bit sussed, and need to tackle the front end, and later the FTP end.
I hope this answers your questions.
Cheers again,
Roy
I would suggest creating a mail rule like this (using your Automation idea.)
Description: Automation
if all of the following conditions are met:
Subject Contains Automation
Message Content Contains (I’m assuming all messages will have something in common to enter here.)
Perform the following actions:
Run AppleScript (press the choose button and navigate to where you saved the script)
If you want to have all the files in a folder called “Automation” then this line
set theFile to ((path to desktop folder) as string) & fileName
needs to be changed - “HD:Users:YourAccount:Documents:Automation:” & fileName" for example.
Hopefully this is helping.
Hi J
Looks like this could do it for me. I am having problems with Mail sinse upgrading to 10.4.7, so cant test it at the moment, but will check it out as soon as I can on Monday when I get to work.
Thanks for the help,
Roy
No problem.
That line should look something like this:
set theFile to "Macintosh HD:Users:Roy:Documents:Automation:" & fileName
and the Automation folder needs to be created before running the script.
You can adjust the path to suit your needs, but I thought a more realistic example might be helpful. Post back if you have problems.
OK J.
I got this working on another mail account. The applescript does not kick in automatically :(. I have to hit the run button with the email selected. Should this be happening?
Cheers,
Roy
J.
I have got a bit more info for you.
The script is kicking in automatically, but what is not happening is the new message that triggers the script is NOT the selected message that is actioned on. If there is no message selected, then nothing happens, If I have an old message selected, then the contents of that is copied into the file, not the contents of the triggered message.
Maybe there is a line that can go into AS to select the unread message?
I will get there!!!
Roy
I was away from the computer for a while…
Which script did you attach to the mail rule? It should be the one that starts
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
It sounds like you used the one that begins
tell application "Mail"
set thisMessage to item 1 of (get selection)
which won’t work properly as a mail rule - as you have seen.
For that one you will have to select a message and click run (in Script Editor) or save it as an application and select a message then double click the app icon.
Use this with a rule (still just a file to the desktop, but first things first.)
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with thisMessage in theMessages
set theText to content of thisMessage
tell thisMessage
set fileName to subject & "_" & (id of thisMessage as string) & ".txt"
end tell
--this part removes colons from the file name so the path is correct
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set newName to (text items of fileName)
set AppleScript's text item delimiters to "_"
set fileName to newName as string
set AppleScript's text item delimiters to TID
set theFile to ((path to desktop folder) as string) & fileName
set theFileID to open for access file theFile with write permission
write theText to theFileID
close access theFileID
end repeat
end perform mail action with messages
end using terms from
Okay Roy,
Now I’m confused. I set up a rule (every message, just to be sure it triggered) to test the script. I saved the script in the Rule Actions folder. It didn’t work automatically, but did work if I selected a message and clicked “Apply Rules” (Message>Apply Rule) from the dropdown menu.
I did notice that I accidentally cut out “tell application “Mail”” when I aded “using terms from…”
The script should be:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with thisMessage in theMessages
set theText to content of thisMessage
tell thisMessage
set fileName to subject & "_" & (id of thisMessage as string) & ".txt"
end tell
--this part removes colons from the file name so the path is correct
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set newName to (text items of fileName)
set AppleScript's text item delimiters to "_"
set fileName to newName as string
set AppleScript's text item delimiters to TID
set theFile to ((path to desktop folder) as string) & fileName
set theFileID to open for access file theFile with write permission
write theText to theFileID
close access theFileID
end repeat
end tell
end perform mail action with messages
end using terms from
I don’t know what else is wrong - I’ve been trying to figure it out for a couple of hours so it’s time for a break. Tomorrow I’ll probably find a post pointing out my obvious mistake.
J
I am baffled now. I thought i’d add a new action into the rule to see if that whould kick the script in. I told the rule to make the message red. The next email turned red, but as you said, only when selecting apply rules does the script kick in. It is now late on Sunday night, and i am going to rest my mind!
I eagerly wait your (or anyone elses reply)
Cheers
Roy
I think I figured it out - I’m testing now. Back in a few…
Nope, sorry.
But I did find out that the script is triggered, just not working properly - I added a line:
repeat with thisMessage in theMessages
beep 3-- this part works
set theText to content of thisMessage
I get the beeps, but no text file. Now I have to figure out why.
j
I added more beeps as tests - the results are in the comments.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with thisMessage in theMessages
beep 3 --trigger test 1 PASSED
set theText to content of thisMessage
delay 1
beep 3 --trigger test 2 PASSED
tell thisMessage
delay 1
beep 3 --trigger test 3 PASSED
set fileName to subject & "_" & (id of thisMessage as string) & ".txt"
delay 1
beep 3 --trigger test 4 FAILED
end tell
but the script - including trigger test 4 - works if I click “Apply Rules”
So I’m still stumped.
Hi Jacques.
I can fix that, but why does it find the messge ID when I click “Apply Rules” - does Mail get it from the emlx file?
Thanks,
j
Thanks Jacques.
I worked on a couple of scripts that used the message ID on selected messages with 10.4.6.
I wonder if using a rule to move the email to a mailbox on the computer could trigger the script so that the message ID could still be used… but at least there is a working version, now.
Roy - I’ll get back to work.
j