Skip to content

Instantly share code, notes, and snippets.

@benwaldie
Created April 1, 2013 02:36
Show Gist options
  • Save benwaldie/5282932 to your computer and use it in GitHub Desktop.
Save benwaldie/5282932 to your computer and use it in GitHub Desktop.
TUAW > AppleScripting Microsoft Outlook > Announce New Emails By Voice
-- Options to process a maximum number of messages
property limitMessageProcessing : true
property theMaxMessagesToProcess : 5
-- The voice to use when speaking. This should be one of the voices in System Preferences > Dictation & Speech > Text to Speech
property theVoice : "Alex"
-- Tihs setting controls whether the script raises the volume to speak, if needed.
property raiseVolumeIfNeeded : true
-- This setting controls the minimum volume level for the message. This should be a value from 1 to 100.
property theMinimumVolumeLevel : 50
-- This setting controls whether the first paragraph of the message is read.
property readFirstParagraphsOfEachMessage : true
-- This setting controls the maximum number of paragraphs to read.
property theMaximumParagraphsToRead : 3
-- This code is triggered if you manually run the script in AppleScript Editor. It retreives any selected messages and then processes them. This is good for testing.
tell application "Microsoft Outlook"
set theMessages to current messages
--(plain text content of (item 1 of theMessages))
--return paragraphs of result
end tell
-- Count the messages received from Outlook.
set theMessageCount to count theMessages
-- Begin preparing the announcement text.
set theAnnouncement to "You have mail!"
if theMessageCount = 1 then
set theAnnouncement to theAnnouncement & " There is 1 new message."
else
set theAnnouncement to theAnnouncement & " There are " & theMessageCount & " new messages."
end if
-- Raise the volume, if needed
if raiseVolumeIfNeeded = true then
set thePreviousVolume to output volume of (get volume settings)
set previousVolumeMuted to output muted of (get volume settings)
if thePreviousVolume is less than theMinimumVolumeLevel then
set volume output volume theMinimumVolumeLevel
else
set volume output volume thePreviousVolume
end if
end if
-- Speak the announcement text.
say theAnnouncement using theVoice
-- Loop through the messages received from Outlook.
tell application "Microsoft Outlook"
repeat with a from 1 to theMessageCount
-- Stop processing after the maximum number of allowable messages
if limitMessageProcessing = true then
if a is greater than theMaxMessagesToProcess then exit repeat
end if
-- Target the current message in the loop.
tell item a of theMessages
-- Retrieve the name of the current message's sender.
set theSender to sender
try
set theSenderName to name of theSender
on error
set theSenderName to address of theSender
end try
-- Retrieve the current message's subject.
set theSubject to subject
-- Customize the announcement text, based on whether the current message is a forward or reply. Also, trim off any "Re: " and "Fwd: " prefixes.
set theMessageType to "message"
if theSubject begins with "Re: " then
set theMessageType to "reply"
if theSubject = "Re: " then
set theSubject to ""
else
set theSubject to (characters 5 thru -1 of theSubject) as string
end if
else if theSubject begins with "Fwd: " then
set theMessageType to "forwarded message"
if theSubject = "Fwd: " then
set theSubject to ""
else
set theSubject to (characters 6 thru -1 of theSubject) as string
end if
end if
-- Prepare the message sender and subject text.
set theMessageAnnouncement to " New " & theMessageType & " from " & theSenderName
if theSubject = "" then
set theMessageAnnouncement to theMessageAnnouncement & ". No subject was specified."
else
set theMessageAnnouncement to theMessageAnnouncement & " with subject \"" & theSubject & "\"."
end if
-- Add the message body text, if necessary.
if readFirstParagraphsOfEachMessage = true then
set theBody to plain text content
if theBody is not equal to "" then
set theMessageAnnouncement to theMessageAnnouncement & " The message begins: "
set theParagraphCount to 1
repeat with b from 1 to count paragraphs of theBody
set theCurrentParagraph to paragraph b of theBody
-- Remove any leading whitespace from the current paragraph
set theCurrentParagraph to removeLeadingWhitespace(theCurrentParagraph) of me
-- Only add the paragraph if its not empty and does not contain http (to filter out lengthy URL lines)
if theCurrentParagraph is not equal to "" and theCurrentParagraph does not contain "http" then
set theMessageAnnouncement to theMessageAnnouncement & paragraph b of theBody & return
if theParagraphCount = theMaximumParagraphsToRead then exit repeat
set theParagraphCount to theParagraphCount + 1
end if
end repeat
end if
end if
-- Speak the message text.
say theMessageAnnouncement using theVoice
end tell
end repeat
end tell
-- Once all messages have been processed, speak the announcement text.
say "No more messages." using theVoice
-- Reset the volume to its previous level, if needed
if raiseVolumeIfNeeded = true then
set volume output volume thePreviousVolume output muted previousVolumeMuted
end if
-- This handler removes leading whitespace from any text it receives as input.
on removeLeadingWhitespace(theText)
repeat
if {"", tab, space} contains theText then return ""
if {space, tab} does not contain character 1 of theText then return theText
set theText to text 2 thru -1 of theText
end repeat
end removeLeadingWhitespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment