Script to alert new mail as a Mail Rule ??

Hi All,

I’ve setup a rule to redirect some dept emails to a separate mailbox folder. This works fine. Next I wrote a script to attached to the rule to display an alert when an email is routed to the mailbox. The display dialog component doesn’t work, reading the forums, this is a ‘Mail’ issue, which doesn’t allow for user interaction.

Here’s the script :


tell application "Finder"
	set alertresponse to (display dialog "New emails to be viewed" buttons {"soon", "view now"} default button 2)
	
	set button_returned to button returned of alertresponse
	-- display dialog button_returned
	
	if button_returned is "view now" then
		tell application "Mail"
			activate
			set selected mailboxes of message viewer 1 to {mailbox "department"} -- of imap account "myAccount"}
		end tell
	end if
end tell

Is there any workaround for this? If not a ‘display dialog’ is there some other form of alert that can be used?

Thanks in Advance.

Matt.

G5 MacPro Dual Quad / 8 / 1tB / 5770
MacOS 10.7.5
Mail 5.3

Really, Mail can’t ‘display dialog’? Try putting the display dialog in tell app “System Events” block:

Tell app “System Events”
display dialog “hello”
end tell

Out curiosity: What version of mail can’t run the following code?

tell application "Mail" to display alert "Subject" message "body" 

I’m at home (running 10.6.8) and mail seems to work fine here.

I tried your script and its working ok except for an unrelated error.

Hi Kel,

Thanks for the response.

I can get the script to run external to mail rules.

But when I assign it to a rule it doesn’t work, did you setup a rule for this script? Please advise.

and also tried ‘System Events’ without success.

Thanks,

Matt.

Hi DJ BW,

Did you assign the script to run as part of a Mail ‘rule’? Please advise.

Thanks.

Matt.

Yes I did, saved it as an scriptd and then the rule was that when the “from” contained my mail adres. it seems that there is an huge latency between receiving the mail and executing the script, but it works without errors. I think it needs explicitly the tell application “Mail” target to show an alert or dialog in mail, or it is in an bug in newer versions of mail.

Hi DJ BW,

Setting the tell statement to ‘Mail’ was the fix.


tell application "Mail"
	set alertresponse to (display dialog "New email to be viewed" buttons {"soon", "view now"} default button 2)
	
	set button_returned to button returned of alertresponse
		
	if button_returned is "view now" then
		tell application "Mail"
			activate
			set selected mailboxes of message viewer 1 to {mailbox "sample dept"} -- of imap account "myAccount"}
		end tell
	else
		tell application "Mail"
			activate
			tell application "System Events"
				tell process "Mail" to keystroke "h" using {command down}
			end tell
		end tell
	end if
end tell

THANKS !!

Matt.