I'm puzzled.
According to the entry of your handler, I assumed that Mail was at work with one of several messages selected in a window already open.
Your extraneous code match the case where Mail is not already running.
May you give more details about the environmment available when your script calls the handler ?
Here's a rough outline of what the app does, and the problems encountered.
Incoming mail is moved to a mail folder “* items to shift” and a mail rule calls the script application MM
MM moves all mail to a new folder called “* items to store”, and calls them one at a time
Each mail is selected, and it is normally stored and treated as an rtfd document. rtfd’s with attachments are broken in Mail under Lion and Mountain Lion, so I’m trying to rebuild them, using the GUI, with some success, apart from the intermittent saving error.
An initial cover sheet is printed showing the details and attachments, with a universal date bar code.
Each attachment (usually a graphic file(s) and/or folder) is printed using CS5 suite, Graphic Converter, or QuarkXPress, depending on it’s type. A bar code is added to each graphic.
All data is saved (the email, the Attachments, and data related to clients etc) in three places for security.
As each email is dealt with, it’s saved in a dated mail folder.
Emails are sent when/if errors occur, and also giving daily, monthly, yearly data.
As well, the App has to ‘watch’ an ftp download folder that takes priority.
Any mail that requires reprinting must be able to be dropped on the folders, and printed when activated by toggling the on/off app.
The main problems are...
- Emails moved from folder to folder often exist in the two folders at once, for a considerable time, and are picked up by the ‘get every message of folder “* items to store”’. I have to deal with them. I call them ‘ghosts’. I can’t use ‘get first or last message’, as they are sometimes ghosts. I get the whole list, and use a repeat loop to go through them.
set tempstore to messages of mailbox MailBoxProcessName as list
my CheckForSwitchOff()
try
repeat with Cyclethroughmessages in reverse of tempstore
- Moving a message is often not instantaneous, and can be ‘seen’ multiple times by the script, more ghosts. I’ve had 50+ ghosts in a folder that had no visible messages in it.
- There doesn’t seem to be a way of saving attachments directly by scripting.
- There doesn’t seem to be a way of saving messages in raw message source directly by scripting
- because of 3 & 4 I’m trying to resort to the GUI, but it seems that if a ghost email is opened, the save to desktop fails with the error message I posted. This error is erratic, and if an email is opened that is a ghost, problems occur. I hate using GUI for this, it’s slow, and unreliable.
- Choosing the individual emails is difficult, the list of emails often contain ghosts.
- To add to the difficulty, the Application must be idiot proof. If someone clicks on the screen during execution, or hits Quit, the app must not stop. To make it bow out gracefully, i have a small app that sets a variable in a folder. The App checks this variable, and stops appropriately. It’s imperative that no email is lost, and no attachment goes unprinted.
8. the app has been running under Snow Leopard for 18 months non-stop, running 24/7. BUT it relies on the mail being saved as rtfd’s with attachments. The Macs are only used for this, and all files must be able to be opened by PC’s. So, I change the Mac’s eml mail to emlx by appending the ‘x’. They open fine.