Applescript for Mail

Joined
Sep 14, 2009
Messages
1
Reaction score
0
Points
1
I'm trying to write a script that does a few things on wakeup every morning. So far, I've figured out how to open Safari with a particular URL, play a playlist in iTunes and get new mail. However, I'm having trouble with a little feature I thought would be nice. Here's what I've got so far:

say "Hey, wake up. I've got news for you." using "Ralph"
tell application "Safari"
open location "http://online.wsj.com/home-page"
end tell
tell application "Mail"
activate
check for new mail
mailbox "inbox"
exists unread count
If true then
say "You've got mail" using "Ralph
else
quit application
end tell

However, it's not working. I'd like to set it where after it checks for new mail, it says "You've got mail" using the voice "Ralph". I can't figure out for the life of me how to do that. I thought that I'd have to use a conditional, but it's not working. If you've got any ideas, I'd really appreciate them.

-Alex
 
Joined
Apr 7, 2009
Messages
3,308
Reaction score
58
Points
48
Location
Whangarei NZ
Your Mac's Specs
27 iMac+Thunderbolt, iMac 21,
Perhaps start of with an existing Script from the collection and modify it to do what you want. From the bit of playing around that i have done the above seems out of order. Putting above in Script Editor and compiling gives a syntax error.
 
Joined
Dec 13, 2007
Messages
256
Reaction score
10
Points
18
Location
United States of America
Your Mac's Specs
2.1GHz MacBook with 4GB RAM, Mac OS X 10.6, iLife and iWork ‘09
This might be a little more complex than you wanted, but I did manage to come up with some code that finds whether you have any unread messages:
Code:
tell application "Mail"
	-- this returns a list with the unread counts of every mailbox
	set unreadCounts to unread count of every mailbox
	-- assume there are no new messages
	set areNewMessages to false
	-- check each unread count individually
	repeat with i from 1 to the count of unreadCounts
		set currentUnreadCount to item i of unreadCounts
		if currentUnreadCount is greater than 0 then
			-- this mailbox has one or more unread messages
			set areNewMessages to true
		end if
	end repeat
	if areNewMessages is true then
		-- there are unread messages, so say an alert
		say "You've got mail!" using "Ralph"
	else
		-- there are no new messages, so quit the application
		quit
	end if
end tell
 
Joined
Mar 30, 2004
Messages
4,744
Reaction score
381
Points
83
Location
USA
Your Mac's Specs
12" Apple PowerBook G4 (1.5GHz)
As an alternative, you could set up a filter ("Rule") in Mail that runs a a simple "Say..." script whenever any message is received.

Though this will run every time you get mail throughout the day, not just in the morning.
 
Joined
Aug 26, 2010
Messages
1
Reaction score
1
Points
3
Late, but its what I came up with

Code:
tell application "Mail"
	if (count of (every mailbox in every account where unread count is greater than 0)) is greater than 0 then
		activate
		tell current application
			say "You've got mail!" using "Ralph"
		end tell
	else
		quit
	end if
end tell
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top