Announcements: Date/Time/Weather/Calendar Agenda

Posted on
Mon Feb 04, 2013 11:25 pm
whmoorejr offline
User avatar
Posts: 763
Joined: Jan 15, 2013
Location: Houston, TX

Announcements: Date/Time/Weather/Calendar Agenda

Using Indigo & Airfoil, I now have the following:

Triggers: (a) master bathroom motion detector (b) home office motion detector

conditions: (a) morning between 5am and 9am (b) variable "morning announcement" = 0

Action: (a) Activate speakers "Master Bedroom, Office"
(b) set volume to 75
(c) set source to "Server"
(d) Run applescript
(e) add 1 to variable "morning announcement"

Another timer sets the "morning announcement" variable back to 0 at midnight.
So I got this to work finally, but not how I anticipated it to work. The script works for me. If you want to use it, you'll have to make a couple changes (name, location, calendars)

When I cut and pasted the applescript into an action, error error error..... for days.

My work around was to save the script as an app and create an action with an applescript to open the app.

If you aren't looking at the computer, everything sounds fine. Alex tells me good morning or good evening, the weather and what is on my appointment calendar. If you look at the computer, Indigo Plugin Host has my activity monitor spiked (don't know if that is related or not) At the end of the announcement, I get a Event Log error Error script error: MorningAnouncement got an error: Connection is invalid. (-609)

But that is after it plays just fine.

Code: Select all
--get weather forcast
set TID to AppleScript's text item delimiters
set webadress to do shell script "curl http://i.wund.com/US/TX/Houston.html#conditions"
-- Find temperature
set AppleScript's text item delimiters to ¬
   "<span class=\"nowrap\"><b>"
set text1 to text item 2 of webadress
set AppleScript's text item delimiters to "</b>&deg"
set temp to text item 1 of text1
set temp to round (temp)

-- Find outlook
set AppleScript's text item delimiters to ¬
   "<div>"
set text2 to text item 3 of webadress
set AppleScript's text item delimiters to "</div>"
set outlook to text item 1 of text2
set AppleScript's text item delimiters to TID

-- get the time
on thetime()
   
   -- hours
   set timeStr to time string of (current date)
   set Pos to offset of ":" in timeStr
   set theHour to characters 1 thru (Pos - 1) of timeStr as string
   set timeStr to characters (Pos + 1) through end of timeStr as string
   
   -- minutes
   set Pos to offset of ":" in timeStr
   set theMin to characters 1 thru (Pos - 1) of timeStr as string
   set timeStr to characters (Pos + 1) through end of timeStr as string
   
   
   return (theHour & ":" & theMin & "") as string
end thetime
set d to (date string of (current date))

set myforcast to "It is " & d & return & return & "And the time is " & thetime() & return & return & "The current temperature is " & temp & "degrees farenhight, and the outlook for today is " & outlook


with timeout of 1000 seconds
   set AppleScript's text item delimiters to ""
   set the_text to ""
   set the_birthday_text to ""
   set today to current date
   set time of today to 0
   set tomorrow to (today) + 86400 -- seconds
   set month_of_today to month of today as string
   set day_of_today to day of today as string
   set the_birthday_list to {}
   
   set the_events to {}
   --
   -- Need to be sure the iCal events are updated
   tell application "System Events"
      tell application "Calendar" to activate
      keystroke "r" using {command down, shift down}
   end tell
   
   tell application "Calendar"
      set the_calendars to every calendar whose name is "Calendar" or name is "Entourage" or name is "Home" or name is "Sync'ed events" or name is "Work" or name is "LDS.org" or name is "Birthdays" or name is "U.S. Holidays" or name is "TW2AG"
      set j to 0
      repeat with a_calendar in the_calendars
         tell a_calendar
            -- Note: repeating events will not work in this way. The start date is the start date of the first event.
            set this_calendars_events to (every event whose start date > today and start date < tomorrow)
            if this_calendars_events is not {} then
               repeat with an_item in this_calendars_events
                  set end of the_events to an_item
               end repeat
            end if
         end tell
      end repeat
      --return the_events
      set the_events to my sortEvents(the_events)
      set i to 1
      --set the_text to the_text & return & "From the " & (name of a_calendar) & " Calendar:"
      repeat with an_event in the_events
         set x to properties of an_event
         --set the_start_date to start_date of an_event
         --
         set the_summary to summary of an_event as string
         set the_location to ""
         set the_location to location of an_event as string
         if the_location is "missing value" then
            set the_location to " "
         end if
         
         
         set the_start_date to start date of an_event
         set the_end_date to end date of an_event
         set the_start_time to time string of the_start_date
         set the_end_time to time string of the_end_date
         --
         --trim the phone numbers from CB & A events
         set the_short_summary to my TrimThePhoneOff(the_summary)
         --
         set the_start_time to my TrimTheTime(the_start_time)
         set the_end_time to my TrimTheTime(the_end_time)
         --
         set j to j + 1
         set the_text to the_text & return & "Appointment number " & j & "." & return & the_start_time & " to " & the_end_time & "." & return & the_short_summary & ",," & the_location & return & return
         set i to i + 1
      end repeat
      --
      
      --Add in the birthdays.
      set the_calendar to first calendar whose name is "Birthdays"
      tell the_calendar
         set the_events to every event
         repeat with an_event in the_events
            set x to start date of an_event
            set month_of_event to month of x as string
            set day_of_event to day of x as string
            if (month_of_event = month_of_today and day_of_event = day_of_today) then
               set event_title to summary of an_event
               set end of the_birthday_list to event_title
            end if
         end repeat
      end tell
      if the_birthday_list is not {} then
         set the_birthday_text to "Also..." & return
         repeat with i from 1 to count of the_birthday_list
            set the_birthday_text to the_birthday_text & "Today is " & item i of the_birthday_list & ". "
         end repeat
      end if
      --
      --SET THE GREETING-- using the idea that midnight is 0 seconds, noon is 43200 seconds, 5 PM is 61200 seconds.
      --
      set the_seconds_since_midnight to time of (current date)
      if the_seconds_since_midnight < 43200 then
         set the_greeting to "morning"
      else
         if the_seconds_since_midnight > 61200 then
            set the_greeting to "evening"
         else
            set the_greeting to "afternoon"
         end if
      end if
      --
      if the_text is "" then
         set the_text to "Good " & the_greeting & ", Mr. More!" & return & return & myforcast & return & return & "Unfortunately, you have no appointments today." & return & the_birthday_text
      else
         set the_text to "Good " & the_greeting & ", Mr. More!" & return & return & myforcast & return & return & "Here are today's appointments." & return & return & the_text & return & "That is the last appointment for today." & return & the_birthday_text
      end if
   end tell
   --
   --
   say the_text using "Alex"
   
   say "It has been my pleasure to give you this report. Have a nice " & the_greeting & "." using "Alex"
   --
end timeout

--
on findLeastItem(lst)
   tell application "Calendar"
      set theLeast to start date of item 1 of lst
      set theIndex to 1
      set iterater to 1
      repeat with i in lst
         if start date of i ≤ theLeast then
            set theLeast to start date of i
            set theIndex to iterater
         end if
         set iterater to iterater + 1
      end repeat
      
      return theIndex
   end tell
end findLeastItem

on removeItemAtIndex(lst, theIndex)
   set newList to {}
   set theLength to length of lst
   if theLength = 1 then
      set newList to {}
   else if theLength = theIndex then
      set newList to items 1 thru (theLength - 1) of lst
   else if theIndex = 1 then
      set newList to items 2 thru theLength of lst
   else
      set newList to items 1 thru (theIndex - 1) of lst & items (theIndex + 1) thru (theLength) of lst
   end if
   return newList
end removeItemAtIndex

on sortEvents(myList)
   set myNewList to {}
   repeat until length of myList = 0
      set leastIndex to findLeastItem(myList)
      set end of myNewList to item leastIndex of myList
      set myList to removeItemAtIndex(myList, leastIndex)
   end repeat
   return myNewList
end sortEvents

on TrimTheTime(the_time)
   return (characters 1 thru -7 of the_time & characters -3 thru -1 of the_time) as string
end TrimTheTime

on convert_mph(input)
   set input to input
   --set old_delims to AppleScript's text item delimiters
   set AppleScript's text item delimiters to "mph"
   set the_text_items to text items of input
   set AppleScript's text item delimiters to "miles per hour"
   set input to the_text_items as string
   --set AppleScript's text item delimiters to old_delims
   set AppleScript's text item delimiters to ""
   return input
end convert_mph

on TrimThePhoneOff(the_summary)
   set the_short_summary to the_summary
   if (count of the_summary) > 12 then
      set the_chars to characters -12 thru -1 of the_summary as list
      repeat with a_char in the_chars
         if a_char is in {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"} then
            set the_short_summary to characters 1 thru -15 of the_summary as string
         else
            set the_short_summary to the_summary
         end if
      end repeat
   end if
   return the_short_summary
end TrimThePhoneOff

Bill
My Plugin: My People

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 21 guests