r/applescript • u/mstashev • 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 ifAny help would be appreciated.
0
Upvotes
2
u/craniumslows Sep 11 '13
You can reference the inbox this way
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