Dialog box seems to crash mail.app

Hello there,

I’m trying to write a suite of scripts to create a ‘Tickler File’ in Mail, that will let me defer a selected message until a certain date. Using Mail Act On, I’ve set up scripts to put mails in Month/Day folders at some relative time in the future (1 day, 1 week, 2 weeks, 4 weeks) and these work fine, creating the necessary folders and moving the mail. The script to fetch the messages also works well and deletes empty folders, etc. So, most of the code has been tested in other forms.

I’m now trying to write a script to file mails for a specific date in the future, taking the date from two dialog boxes. As soon as the first box appears, if it appears at all, Mail crashes. On a very rare occasion, the box will appear OK, but then I can’t write into it, just click OK.

If anyone could help me figure this one out, they would have my eternal gratitude.

Thanks,
Ross

(sorry if the code is a little messy, I’m new to this and I’ve been hacking and slashing in frustration!)

-- The tickler folder
property tickler : "@TICKLER"
property FOLDERS_MONTHS : {"01.Jan", "02.Feb", "03.Mar", "04.Apr", "05.May", "06.Jun", "07.Jul", "08.Aug", "09.Sep", "10.Oct", "11.Nov", "12.Dec"}

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		
		-- Preparatory work
		set month_ok to false
		set day_ok to false
		if ((year of (current date)) mod 4) is equal to 0 then
			set days_in_feb to 29
		else
			set days_in_feb to 28
		end if
		
		set DAYS_EACH_MONTH to {31, days_in_feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
		set this_month to ((month of (current date)) as integer)
		set this_day to day of (current date) as integer
		
		display dialog "Which Month?" default answer this_month buttons ["OK"] default button "OK"
		
		if (text returned of result) is not "" then
			set chosen_month to text returned of result
			try
				set days_this_month to item (chosen_month as integer) of DAYS_EACH_MONTH
				set month_ok to true
			on error
				display dialog "That doesn't look like a month to me. I should be a number from 1 to 12."
			end try
		else
			display dialog "You didn't enter a month."
		end if
		
		if month_ok then
			
			display dialog "Which Date (1-" & days_this_month & ")?" default answer this_day buttons ["OK"] default button "OK"
			
			try
				set target_day to ((text returned of result) as integer)
				set day_ok to true
			on error
				display dialog "That doesn't look like a date to me. I should be a number from 1 to" & days_this_month & "."
			end try
		end if
		
		if day_ok then
			tell application "Mail"
				set target_month to item (chosen_month as integer) of FOLDERS_MONTHS
				set destination_mailbox to tickler & "/" & target_month & "/" & target_day
				-- Check if the month folder exists. If not, create it.
				try
					set mbox to mailbox named target_month
				on error
					make new mailbox with properties {name:(tickler & "/" & target_month)}
				end try
				-- Check if the day folder exists. If not, create it.
				try
					set mbox to mailbox named destination_mailbox
				on error
					make new mailbox with properties {name:(tickler & "/" & target_month & "/" & target_day)}
				end try
				-- Put the items in the destination folder.
				repeat with eachMessage in theMessages
					move eachMessage to (mailbox named destination_mailbox)
				end repeat
			end tell
		end if
		
	end perform mail action with messages
end using terms from

Model: MacBook
Browser: Safari 522.12.1
Operating System: Mac OS X (10.4)

Ross just a quick guess here … try telling mail to display the dialog when I have some time I’ll test your script. let me know if that helps though

mm

Hi mm,

Thanks for getting back to me. I’m afraid telling mail to display the dialog doesn’t help, it still splats with a half-drawn dialog box. I’ve tried just setting the default value as “” too, to no avail.

If you get a chance to look at it, that would be just grand.

Cheers,
Ross

Hi,

I guess(!), rule scripts don’t like or even don’t allow user interaction.
I changed your script to choose the month and the day from lists, but it also crashes

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		-- Preparatory work
		tell (current date) to set {this_year, this_month, this_day, this_date} to {year, its month as integer, day as integer, it}
		set chosen_month to choose from list FOLDERS_MONTHS with prompt "Choose a month" default items (item this_month of FOLDERS_MONTHS)
		tell result
			if it is false then return
			set {chosen_month, target_month} to {text 1 thru 2 of item 1 of it as integer, item 1 of it}
		end tell
		if this_month > chosen_month then set this_year to this_year + 1
		tell this_date to set {its year, its month} to {this_year, chosen_month}
		tell this_date to tell it + (32 - day) * days to tell it - day * days - time + 86399 to set days_this_month to its day -- calculate days of given month
		set dayList to {}
		repeat with i from 1 to days_this_month
			set end of dayList to i
		end repeat
		set chosen_day to choose from list dayList with prompt "Choose a day" default items (item this_day of dayList)
		tell result
			if it is false then return
			set chosen_day to item 1 of it as integer
		end tell
		tell application "Mail"
			set destination_mailbox to tickler & "/" & target_month & "/" & target_day
			-- Check if the month folder exists. If not, create it.
			try
				set mbox to mailbox named target_month
			on error
				make new mailbox with properties {name:(tickler & "/" & target_month)}
			end try
			-- Check if the day folder exists. If not, create it.
			try
				set mbox to mailbox named destination_mailbox
			on error
				make new mailbox with properties {name:(tickler & "/" & target_month & "/" & target_day)}
			end try
			-- Put the items in the destination folder.
			repeat with eachMessage in theMessages
				move eachMessage to (mailbox named destination_mailbox)
			end repeat
		end tell
	end perform mail action with messages
end using terms from

thinking back I remember trying to do other user interactive type things and got poor results

so I thought well the problem is with mail so let me move the user interaction out of mail so thats what I did ! try this version out see how it works for out


-- The tickler folder
property tickler : "@TICKLER"
property FOLDERS_MONTHS : {"01.Jan", "02.Feb", "03.Mar", "04.Apr", "05.May", "06.Jun", "07.Jul", "08.Aug", "09.Sep", "10.Oct", "11.Nov", "12.Dec"}

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		
		-- Preparatory work
		set month_ok to false
		set day_ok to false
		if ((year of (current date)) mod 4) is equal to 0 then
			set days_in_feb to 29
		else
			set days_in_feb to 28
		end if
		
		set DAYS_EACH_MONTH to {31, days_in_feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
		set this_month to ((month of (current date)) as integer)
		set this_day to day of (current date) as integer
		tell application "Finder"
			activate
			display dialog "Which Month?" default answer this_month buttons ["OK"] default button "OK"
		end tell
		if (text returned of result) is not "" then
			set chosen_month to text returned of result
			try
				set days_this_month to item (chosen_month as integer) of DAYS_EACH_MONTH
				set month_ok to true
			on error
				tell application "Finder"
					activate
					display dialog "That doesn't look like a month to me. I should be a number from 1 to 12."
				end tell
			end try
		else
			tell application "Finder"
				activate
				display dialog "You didn't enter a month."
			end tell
		end if
		
		if month_ok then
			tell application "Finder"
				activate
				display dialog "Which Date (1-" & days_this_month & ")?" default answer this_day buttons ["OK"] default button "OK"
			end tell
			try
				set target_day to ((text returned of result) as integer)
				set day_ok to true
			on error
				tell application "Finder"
					activate
					display dialog "That doesn't look like a date to me. I should be a number from 1 to" & days_this_month & "."
				end tell
			end try
		end if
		
		if day_ok then
			tell application "Mail"
				activate
				set target_month to item (chosen_month as integer) of FOLDERS_MONTHS
				set destination_mailbox to tickler & "/" & target_month & "/" & target_day
				-- Check if the month folder exists. If not, create it.
				try
					set mbox to mailbox named target_month
				on error
					make new mailbox with properties {name:(tickler & "/" & target_month)}
				end try
				-- Check if the day folder exists. If not, create it.
				try
					set mbox to mailbox named destination_mailbox
				on error
					make new mailbox with properties {name:(tickler & "/" & target_month & "/" & target_day)}
				end try
				-- Put the items in the destination folder.
				repeat with eachMessage in theMessages
					move eachMessage to (mailbox named destination_mailbox)
				end repeat
			end tell
		end if
		
	end perform mail action with messages
end using terms from

Hi Stefan,

Thanks for giving it a go.

It didn’t seem to do anything, though. I tried telling Finder to do the whole first block (below), which changes focus to the Finder, but nothing else.

Peculiarly, I get a crash if I run the Sample Mail Rule Script, too.

Hmmmm

Thanks again,
Ross


-- The tickler folder
property tickler : "@TICKLER"
property FOLDERS_MONTHS : {"01.Jan", "02.Feb", "03.Mar", "04.Apr", "05.May", "06.Jun", "07.Jul", "08.Aug", "09.Sep", "10.Oct", "11.Nov", "12.Dec"}

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		
		
		-- Preparatory work
		set month_ok to false
		set day_ok to false
		if ((year of (current date)) mod 4) is equal to 0 then
			set days_in_feb to 29
		else
			set days_in_feb to 28
		end if
		
		set DAYS_EACH_MONTH to {31, days_in_feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
		set this_month to ((month of (current date)) as integer)
		set this_day to day of (current date) as integer
		tell application "Finder"
			activate
			display dialog "Which Month?" default answer this_month buttons ["OK"] default button "OK"
			--end tell
			if (text returned of result) is not "" then
				set chosen_month to text returned of result
				try
					set days_this_month to item (chosen_month as integer) of DAYS_EACH_MONTH
					set month_ok to true
				on error
					--tell application "Finder"
					activate
					display dialog "That doesn't look like a month to me. I should be a number from 1 to 12."
					--end tell
				end try
			else
				--tell application "Finder"
				activate
				display dialog "You didn't enter a month."
				--end tell
			end if
			
			if month_ok then
				--tell application "Finder"
				activate
				display dialog "Which Date (1-" & days_this_month & ")?" default answer this_day buttons ["OK"] default button "OK"
				--end tell
				try
					set target_day to ((text returned of result) as integer)
					set day_ok to true
				on error
					--tell application "Finder"
					activate
					display dialog "That doesn't look like a date to me. I should be a number from 1 to" & days_this_month & "."
					--end tell
				end try
			end if
		end tell
		
		if day_ok then
			tell application "Mail"
				activate
				set target_month to item (chosen_month as integer) of FOLDERS_MONTHS
				set destination_mailbox to tickler & "/" & target_month & "/" & target_day
				-- Check if the month folder exists. If not, create it.
				try
					set mbox to mailbox named target_month
				on error
					make new mailbox with properties {name:(tickler & "/" & target_month)}
				end try
				-- Check if the day folder exists. If not, create it.
				try
					set mbox to mailbox named destination_mailbox
				on error
					make new mailbox with properties {name:(tickler & "/" & target_month & "/" & target_day)}
				end try
				-- Put the items in the destination folder.
				repeat with eachMessage in theMessages
					move eachMessage to (mailbox named destination_mailbox)
				end repeat
			end tell
		end if
		
	end perform mail action with messages
end using terms from

what version of mail do you have

Version 2.1.1 (752.3)

would it make more sense to have the script run on selected messages rather than from a rule seeing there is already user interaction. doing it this way may fix the problem ?

mm

Finally I have found someone else having the same OS X Mail problem I’ve always had. After I moved from OS 9 to Tiger I tried writing Applescript filters for Mail but each and every time the script ran soon Mail hangs and eventually other running apps hang too. As a result I have never been able to use the Mail filter scripts feature. Alas, Leopard Mail has the exact same problem. In the past I sent several dump reports to Apple but they seem to be ignored.

Rebooting recovers without any obvious system damage that I can detect, but I don’t like experimenting with something that could possibly cause a corruption that might get saved to disk.

I have tried all kinds of things to avoid the problem. Get this: even having empty run handlers in the filter script causes the system problems.

I have used Applescript heavily since it was introduced so I checked for any dangling OS 9 scripting components that may be hanging around but found nothing. I also recompiled my scripts using the latest Apple Script editor. Nothing works.

Out of frustration I am now creating a totally new clean Leopard install on an external disk and will try running Apple’s own Sample Mail Rule Script on it. If that fixes the problem I’ll add another post here.

Model: Mac Mini Intel Core Dual 2
Operating System: Mac OS X (10.5)

Mail has a significant bug running AppleScript rules.

I installed a pristine version of Leopard, booted to it then started mail and configured my email settings. Then I replaced the single default rule in Mail preferences with one that runs Apple’s own demonstration mail rule script in /Macintosh HD/Library/Scripts/Mail Scripts/Rule Actions/Sample rule action. I created a dummy test email and applied the rules to it and the problems started right away. Mail displayed a corrupt dialog box (the sample script displays a dialog for each message processed) and the console log showed Mail had a thread fail with an unaligned malloc free attempt.

On Tiger my Mail sometimes shows a good dialog box and sometimes shows nothing. But in either case Mail starts hanging up and other applications soon start hanging as well, forcing me to boot.

This bug must not be causing such obvious anomalies for other scripters writing Mail rule scripts or it would have been noticed more widely by now.

So the original problem was how to defer the email I wonder if it would be easier to just do the by selecting the messages and then running the script against the selection rather than using rules.

I also got a complaint from a client last night saying they had upgraded to leopard/ mail 3.0 and there script no longer worked ! There script were using rules as well.

mm

Yes, avoiding the use of a rule script should solve your problem. I have had no problems running lots of different kinds of Applescripts and Applescript Studio scripts to control Mail. Just make sure to use ‘with transaction’ around your top level tell statements to Mail. Doing this prohibits script errors due to stale script variables if Mail happens to do any concurrent activity (timed actions or user actions) while your run handler is running.