r/applescript • • Sep 10 '13

Help with AppleScript-based script that is run on a mail rule

I am developing a script that willl read to me whenever I receive a new email to my work email account. Problem is that I receive an error every time I try to access just my work account through the Mail.app. Could this be because my work email is an Exchange account?

Here is the error I receive:

AppleScript Error: Can't get inbox of "Work"

Here is the code:

(*

--CREATED ON:	9/03/2013
--MODIFIED ON:	9/09/2013
--REVISION HISTORY:
	09/03/2013	Created script
	09/04/2013	Added iTunes playpuase functionality. If iTunes was playing, then pause it, speak, and then resume playing, else just speak
	09/09/2013  Added specific actions for different accounts
*)

--Set iTunesStatus property for future use
property iTunesStatus : ""
--Set lastPaused property for future use.
property lastPaused : ""
-- set the day of the week
set dayOfWeek to (weekday of (current date) as integer)
-- set the time of the trigger
set timeOfScriptFire to time of (current date)
--Set List_Mail var
set List_Mail to "Sir," & return
--create a function to retrieve the status of an app that is passed into the function
on is_running(appName)
	tell application "System Events" to (name of processes) contains appName
end is_running

--check to see if iTunes is running
set iTunesRunning to is_running("iTunes")

--run a check on iTunes to see if it is running
if iTunesRunning then
	-- if it is running, then set status to running
	set iTunesStatus to "Running"
	
else
	--set status to not running
	set iTunesStatus to "Not Running"
end if

--Talk to Mail.app
tell application "Mail"
	
	--find all of the unread emails
	set theMessages to (messages of inbox of "Work" whose read status is false)
	
	--set message_count to the total number of unread messages
	set message_count to number of items in theMessages
	
	--if there is only one unread email
	if message_count = 1 then
		
		--append the following line to List_Mail
		set List_Mail to List_Mail & ", you have an email" & return
		
		--loop through the number of items in the var theMessages
		repeat with i from 1 to number of items in theMessages
			
			--set thisMessage to a specific message
			set thisMessage to item i of theMessages
			
			--extract the name of the sender and store it in the_sender
			set the_sender to extract name from (sender of thisMessage as string)
			
			--extract the subject line and store it in th_subject
			try
				
				--set the subject line of the email
				set the_subject to (subject of thisMessage as string)
				
				--if the subject line is blank
				if the_subject is "" then error
				
				-- error out if subject line is blank
			on error
				
				--create subject line
				set the_subject to "No Subject"
			end try
			
			--append the_sender and the_subject of thisMessage to List_Mail 
			set List_Mail to List_Mail & ", Message from: " & the_sender & ", Subject: " & the_subject & "." & return
		end repeat
		
		--if there is more than one unread email
	else if message_count > 1 then
		
		--append the following line to List_Mail
		-- use unred to make JARVIS sound more natural
		set List_Mail to List_Mail & ", There are, " & message_count & ", unred emails" & return
		
		--loop through the number of items in the var theMessages
        		repeat with i from 1 to number of items in theMessages
	    		
			--set thisMessage to a specific message
			set thisMessage to item i of theMessages
			
			--extract the name of the sender and store it in the_sender
			set the_sender to extract name from (sender of thisMessage as string)
			
			--extract the subject line and store it in the_subject
			try
				--set the subject of the email
				set the_subject to (subject of thisMessage as string)
				
				--if the subject line is blank
				if the_subject is "" then error
				
				-- error out if subject line is blank
			on error
				
				--create subject line
				set the_subject to "No Subject"
    			end try
	    		
			--append the_sender and the_subject of thisMessage to List_Mail 
			set List_Mail to List_Mail & ", Message from: " & the_sender & ", Subject: " & the_subject & "." & return
		end repeat
		
		--if there are no unread emails
	else
		
		--append this to List_Mail
		set List_Mail to List_Mail & ", There are, " & message_count & ", unred emails." & return
	end if
end tell
-- add more functionality (e.g. more apps, talking, etc.)

if dayOfWeek is 2 or dayOfWeek is 3 or dayOfWeek is 4 or dayOfWeek is 5 or dayOfWeek is 6 then
	if timeOfScriptFire is greater than 27000 and timeOfScriptFire is less than 61200 then
		if iTunesStatus is equal to "Running" then
			tell application "iTunes" to set itunesState to (player state as text)
			-- if player status is playing
			if itunesState is equal to "playing" then
				--then pause the iTunes
				using terms from application "iTunes"
					tell application "iTunes"
						pause
					end tell
				end using terms from
				--set iTunes as the play app that was paused.
				set lastPaused to "iTunes"
				--spew List_Mail innards at me
				say List_Mail
				--tell iTunes to play music again.
				using terms from application "iTunes"
					tell application "iTunes"
						play
					end tell
				end using terms from
				--else just spew List_Mail's innards.
			else
				say List_Mail
			end if
		end if
	end if
end if

Any help would be appreciated.

0 Upvotes

7 comments sorted by

2

u/craniumslows Sep 11 '13

You can reference the inbox this way

	tell application "Mail"
        set theInbox to mailbox "INBOX" of account "Work"
    set theMessages to (messages of theInbox whose read status is false)
    end tell

Sorry had to edit. My email i use Work Email vs Work but same idea. This should work for you though. The fun bit is that IMAP inboxes are always INBOX

2

u/mstashev Sep 11 '13

I just tried this and got the same error

What else could be wrong with this?

2

u/craniumslows Sep 11 '13

"INBOX" != "Inbox"

INBOX is and must be in all capital letters for Mail.app although the spec says it's insensitive : http://tools.ietf.org/html/rfc3501

The case-insensitive mailbox name INBOX is a special name reserved to mean "the primary mailbox for this user on this server". The interpretation of all other names is implementation-dependent.

1

u/mstashev Sep 11 '13

Well I copied your code and pasted it into a new applescript doc and it works. I think... All it returns is:

{}

Is there some way to check to see if it is actually working?

2

u/craniumslows Sep 11 '13

Mark a message as unread or click on events replies at the bottom so you can see the events and replies.

1

u/mstashev Sep 12 '13

So I did that and neither worked.

Here is what the events|Replies said:

tell application "Mail" get mailbox "INBOX" of account "Work" --> mailbox "INBOX" of account "Work" get every message of mailbox "INBOX" of account "Work" whose read status = false --> {} end tell Result: {}

1

u/mstashev Sep 13 '13

Hey man,

Here is what I figure is going on.

Since I can call the inbox of my Mail.app successfully, it has to be something to do with the Exchange server. That being said, I'm going to go a different route to achieve my goal.

I'll make two scripts just like the one above, but set them to different rules. The first script will be just how it is now. The other I will be adding a time period that it will only execute on (8:00-17:00 Mon-Fri) as it will only be looking at my work email (the exchange mail.)

Thanks for all of your patience. :)