Hello All
I do not know, what kind of bug it is. Maybe you know? I wrote a mail rule, that is attached to an IMAP mail folder. If mail arrives in this folder and Apple Mail tries to execute the rule Apple Mail crashes.
The script that is called by the rule, extracts information from it (error reports) and pastes it to an FileMaker Pro Database. In the development environment this works, but not in the production environment. Strangely somehow.
The script will try to move the mail to a local mailbox after processing. But this may be not really working as I think. With a POP mailbox these things schould work. But with IMAP?
Is it a bug in Apple Maiil or in AppleScript?
I ask because this happened: I had the mail rule activated, then sent me two mp3 files from a friend’s PC and the rule executed, what it should not on these incoming mails because they did’nt meet the criterias of the rule. The script presented an empty error dialog from a general error handler I implemeted and that was all. The dialog was unclickable. I do not understand what’s going on with Apple Mail and cannot see any error in the scripting.
Any ideas?
That’s the code. (Posted with permission of the customer). I could imagine that the “using terms from” clause causes Apple Mail to crash. May this be possible?
-- -------------------------------------------------------------------------------------------------------------------------------------
-- User config part
-- -------------------------------------------------------------------------------------------------------------------------------------
property _db_name : "CATS"
property _lay_name : "sharing"
property _db_path : "Mac:Users:stephenjones:Development:CATS:Filemaker:cats.fp7"
property _mailbox : "ProcessedEmails"
property _rejects : "RejectedEmails"
property _imap : "cats"
-- -------------------------------------------------------------------------------------------------------------------------------------
-- Main - DO NOT CHANGE THE FOLLOWING CODE EXCEPT YOU KNOW EXACTLY WHAT YOU ARE DOING!
-- -------------------------------------------------------------------------------------------------------------------------------------
property _app_version : "v1.0"
-- -------------------------------------------------------------------------------------------------------------------------------------
using terms from application "Mail"
on perform mail action with messages _messages
tell application "Mail"
-- Process a single or multiple mails
repeat with _mail in _messages
try
-- Read message
tell _mail to set {_sender, _subject, _datereceived, _body} to {sender, subject, date received, content}
-- Extract time and date
set {_hs, _mins, _secs, _day, _month, _year} to {hours, minutes, seconds, day, month, year} of _datereceived
if _hs < 10 then set _hs to "0" & _hs
if _mins < 10 then set _mins to "0" & _mins
if _secs < 10 then set _secs to "0" & _secs
-- Format date and time stamp
set _time to _hs & ":" & _mins & ":" & _secs as string
set _date to _day & " " & _month & " " & _year as string
-- Converting text to a list
-- Strip "at 30%" if existing
if paragraph 1 of _body contains "at 30%" then
set _alertlevel30 to "Y"
set _body to paragraphs 3 thru -1 of _body
else
set _alertlevel30 to "N"
-- or leave it as it is
set _body to paragraphs 1 thru -1 of _body
end if
-- Extract values
set _new_bod to {}
repeat with _par in _body
set _temp to _par as string
if _temp is "" then exit repeat
set _off to offset of ":" in _par
set _new_bod to _new_bod & ((characters (_off + 2) thru -1 of _par) as string) as list
end repeat
-- Construct data structure
tell _new_bod to set {_acc_code, _serial, _ip_address, _pagecount, _dev_model, _evt_name, _evt_state, _evt_type, _evt_seve, _alt_info_evt} to items
-- Create a new FileMaker Record with data
tell application "FileMaker Pro"
-- Check for open database or open it
try
get database _db_name
on error
open file _db_path
end try
-- Create a new FileMaker Record with data
tell database _db_name
-- tell layout _lay_name
set _id to ID of (create new record)
go to last record
tell last record
copy _date to cell "AlertDate"
copy _time to cell "AlertTime"
copy _acc_code to cell "AccountCode"
copy _serial to cell "SerialNo"
copy _ip_address to cell "IPAddress"
copy _pagecount to cell "PageCount"
copy _dev_model to cell "DeviceModel"
copy _evt_state to cell "EventState"
copy _evt_type to cell "EventType"
copy _evt_seve to cell "EventSeverity"
copy _alt_info_evt to cell "AlertInformationEvent"
copy (current date) as string to cell "TimeLogged"
copy _evt_name to cell "EventName"
copy _alertlevel30 to cell "Alertlevel30%"
end tell
end tell
end tell
-- Duplicate incoming mail to a new outgoing mail (does not work)
-- Change the subject line (does not work)
-- Store the Mail (does not work)
-- Move the Mail to an incoming folder (it already is)
-- Mark the Mail (possible)
set read status of _mail to true
-- Move the Mail to another mailbox
move _mail to mailbox _mailbox of imap account _imap
-- Delete the incoming mail (unneeded)
on error errtext number errnum
activate
display dialog "An error occurred!" & return & return & "(" & errnum & ")" & " | " & errtext with icon caution with title _app_version giving up after 60
end try
end repeat
end tell
end perform mail action with messages
end using terms from
-------------------------------------------------------------------------------------------------------------------------------------
-- End of file
-- -------------------------------------------------------------------------------------------------------------------------------------
Hello Thomas,
I had exactly the same problem when creating AppleScripts to be attached to an Apple Mail rule and learned, that you are not supposed to use any user interaction in your code. At least, using any user interaction in my code always resulted in a crash…
So please try to remove the display dialog command from the try-error block, maybe this will already solve your problem.
Best regards to Cologne from Bielefeld
Thank you. That’s it. In a first release I did not implement an error handler but let AppleScript do it by itself. It worked. After I implemented the error dialog the bug appeared suddenly.
I will keep in mind not to use dialogs in scripts for AppleMail that will be executed by a rule. Thanks a lot!
Greetings to Bielefeld from Köln!
Thomas