Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aravintht

macrumors 6502a
Original poster
Jun 7, 2007
626
1
London, UK
Hi, i finally got round to sorting out my alarm clock that ceased to work when switching to lepord (yes i know it was ages ago...i'm a busy man)

any way its a pretty basic applescript but thought i'd share...

any comments would be greatly appreciated - and if anyone knows how to speed up the ical event searching (it takes 9 seconds) let me know

my alarm clock says good morning, the current date and time, the current temperature and weather conditons (from internet), tells me how many unread emails i have, tells me if i have lectures today and then checks the tube status (London underground has a shockingly bad underground service)/tells me if i'm working/on holiday, and finally loads Capital Radio (Londoners will know this station)

it does a couple of other things like closing applications, messing around with the volume, snooze etc

here it is...

--mute Volume
set volume output volume 0

--Quit music/sound applications
tell application "QuickTime Player"
quit
end tell

tell application "iTunes"
quit
end tell

tell application "EyeTV"
quit
end tell

--get weather forcast
set TID to AppleScript's text item delimiters
set webadress to do shell script "curl http://i.wund.com/global/stations/03772.html#conditions"
-- Find temperature
set AppleScript's text item delimiters to ¬
"<span class=\"nowrap\"><b>"
set text1 to text item 3 of webadress
set AppleScript's text item delimiters to "</b>°"
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

--Check new mail
tell application "Mail"
check for new mail
end tell

--find calendar items for today
tell (current date) to set today to its date string
set counter to 0
set counter1 to 0
tell application "iCal"

set e_vent to every event in calendar "University"
repeat with i from 1 to number of items in e_vent
set this_item to item i of e_vent
set startdate to start date of this_item
--set starttime to start of this_item
set startdate to date string of startdate
if startdate is today then
set counter to counter + 1
end if
end repeat

if counter is equal to 0 then
set e_vent1 to every event in calendar "WHSmith"
repeat with i from 1 to number of items in e_vent1
set this_item1 to item i of e_vent1
set startdate1 to start date of this_item1
set startdate1 to date string of startdate1
if startdate1 is today then
set counter1 to counter1 + 1
end if
end repeat
else
-- Find tube status
set TID to AppleScript's text item delimiters
set webadress to do shell script "curl http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/tube-piccadilly-now.html"

set AppleScript's text item delimiters to ¬
"<p>"
set stat to text item 3 of webadress
set AppleScript's text item delimiters to "<br ><br ></p>"
set tubestat to text item 1 of stat
set AppleScript's text item delimiters to TID
end if
quit
end tell

--count unread emails
set unreadCount to 0

set exlcudeMailboxes to {"Drafts", "Trash", "Sent", "Sent Messages", "Junk", "Spam", "Outbox"}

tell application "Mail"

-- get mailboxes with unread count
set mailboxesWithUnreadMessages to mailboxes of accounts whose unread count is not 0
set localMailboxesWithUnreadMessages to every mailbox whose unread count is not 0

-- loop through all accounts and mailboxes
set returnArray to {}
repeat with currentAccount in mailboxesWithUnreadMessages & localMailboxesWithUnreadMessages
repeat with currentMailbox in currentAccount

-- get mailbox attributes
set mailboxName to name of currentMailbox
set mailboxCount to unread count of currentMailbox

if mailboxName is not in exlcudeMailboxes then

-- increment total count
set unreadCount to unreadCount + mailboxCount
end if

end repeat
end repeat

end tell

--open quicktime player
tell application "QuickTime Player"
run
end tell

-- 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

--Say good morning
set d to (date string of (current date))
set volume output volume 20
say "Good morning Aravinth, it is time to wake up."
delay 1
say "It is " & d
say "And the time is " & thetime()
set volume output volume 16

--say weather
delay 1
say "The current temperature in London is " & temp
say "degrees celcius"
say "and the outlook for today is " & outlook

--say number of unread emails and capital loading

delay 2
say "you have " & unreadCount
say "unread emails"
delay 2
--say calendar events
if counter is equal to 1 then
say "and one lecture today"
delay 2
say "the current tube status "
delay 0.5
say tubestat
else if counter is greater than 1 then
say "and " & counter
say "lectures today"
delay 2
say "the current tube status "
delay 0.5
say tubestat
else if counter1 is greater than 0 then
say "your calendar shows you have work today"
else
say "it appears you are on holiday as you have no lectures or work events in your calendar"
end if

--Open Captial FM stream in Quicktime Player
tell application "QuickTime Player"
open location "http://mediaweb.musicradio.com/V1/playlist.asx?StreamID=1"

end tell
say "Capital Radio is now loading"

--hide windows
tell application "System Events"
set visible of (every process whose visible is true and frontmost is false) to false
set visible of process "Finder" to false
end tell

--Fade in volume
delay 3
set volume output volume 5
delay 1
set volume output volume 6
delay 1
set volume output volume 7
delay 1
set volume output volume 8
delay 1
set volume output volume 9
delay 1
set volume output volume 10
delay 1
set volume output volume 11
delay 1
set volume output volume 12
delay 1
set volume output volume 13
delay 1
set volume output volume 14
delay 1
set volume output volume 15
delay 1
set volume output volume 16
delay 1
set volume output volume 17
delay 1
set volume output volume 18
delay 1
set volume output volume 19
delay 1
set volume output volume 20
delay 4
set volume output volume 19
delay 1
set volume output volume 18
delay 1
set volume output volume 17
delay 1
set volume output volume 16

--dialog boxes
set question to display dialog "Your Wake up routine has been completed sucessfully, what would you like to do?" buttons {"Continue listening to Capital Radio", "Snooze for 5 minutes", "Switch off Radio"} default button 1
set answer to button returned of question

if answer is equal to "Continue listening to Capital Radio" then
display dialog "Have a good day, the time is " & thetime() buttons {"OK!"} default button 1
end if

if answer is equal to "Snooze for 5 minutes" then
set volume output volume 0
display dialog "The volume has been muted for 5 minutes" buttons {"OK!"} default button 1
delay 298

set volume output volume 18
end if

if answer is equal to "Switch off Radio" then
tell application "QuickTime Player"
quit
end tell
display dialog "Capital Radio is now switched off" buttons {"close"} default button 1
end if
 

Aravintht

macrumors 6502a
Original poster
Jun 7, 2007
626
1
London, UK
i'll be improving it as time goes on/i test it out

eg this morning, it didnt wake me up till the radio started playing so i missed all the 'info'


also i need to sort out the error handling, eg if the internet is down, i will want it to start playing music from itunes etc..
 

Dclevela

macrumors newbie
Sep 24, 2008
2
0
Aravinth - this is really cool. I've actually been working on a very similar alarm clock for a while now and I like a couple of your ideas a lot -- especially the volume fade and launching the radio.

My alarm clock (which is still an ongoing project) currently gets news headlines, the weather, current market data (i.e. where the Dow is trading) and calculates my stock portfolio value.

I use an applescript to run the speaking part, but most of the data is collected by a perl script (which is called in the applescript) and saved to text files for later use...

Cool to see other people doing something similar and please post any updates as I'd love to get more ideas for this project.
 

itzmurray

macrumors newbie
Jan 27, 2009
1
0
i need help!!!

i really like the weather forecast and the temperature..
but all i got it is this.....
<a href=""><img src="http://mail.google.com/mail/?attid=0.1&disp=emb&view=att&th=11f18c0100e8a113" border="0"/></a>

plssss help me out!!!
 

gcouger

macrumors newbie
Mar 22, 2009
2
0
My applescript alarm clock (Londoners)

Thanks for posting the code.

I learned more about Applescript form your code than I have in the two weeks I have been trying to get up to speed on it.

Between Applescript, shell scripts and a little C code will do almost anything.

Thanks a lot
Gordon
 

KettyKrueger

macrumors 6502a
Feb 17, 2007
509
4
UK
I must have this!!!

However, I have no experience with scripts. How would one get this to work? Maybe invoke an iTunes plist instead of C. Radio.

Thanks
 

Dclevela

macrumors newbie
Sep 24, 2008
2
0
Play from iTunes...

@Ketty -- The code below will start playing the first track from a playlist titled "Alarm" in itunes.

Code:
tell application "iTunes"
	activate
	set view of front browser window to user playlist "Alarm" of source "Library"
	set visible of window 1 to false
	set thetrack to the first track of playlist "Alarm" of source "Library"
        play thetrack
end tell
 

Aravintht

macrumors 6502a
Original poster
Jun 7, 2007
626
1
London, UK
Hi all, haven't looked at this thread since i created it

here is my latest code.

--switch on bedroom lights/open curtains using x10
tell application "Safari"
set thisDocument to make new document
set URL of thisDocument to "http://192.168.0.111/x10ui.spi?house=B%20&unit=1%20&cmd=2%20&x10event=2"
delay 2
close every window
quit
end tell

try
--mute Volume & intialise
set volume output volume 30
beep
set volume output volume 20
beep
set volume output volume 10
beep
set volume output volume 0

--Quit music/sound applications
tell application "QuickTime Player"
quit
end tell

tell application "iTunes"
quit
end tell

--tell application "Front Row"
-- quit
--end tell

tell application "EyeTV"
quit
end tell

--get weather forcast
try
set TID to AppleScript's text item delimiters
set webadress to do shell script "curl http://i.wund.com/global/stations/03772.html#conditions"
-- Find temperature
set AppleScript's text item delimiters to ¬
"<span class=\"nowrap\"><b>"
set text1 to text item 3 of webadress
set AppleScript's text item delimiters to "</b>°"
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
on error
set temp to "error unable to retrieve temperature"
set outlook to "error unable to retrieve outlook"
end try

--Check new mail
tell application "Mail"
check for new mail
end tell

--find calendar items for today
try
tell (current date) to set today to its date string
set counter to 0
set counter1 to 0
tell application "iCal"

set e_vent to every event in calendar "University"
repeat with i from 1 to number of items in e_vent
set this_item to item i of e_vent
set startdate to start date of this_item
--set starttime to start of this_item
set startdate to date string of startdate
if startdate is today then
set counter to counter + 1
end if
end repeat

if counter is equal to 0 then
set e_vent1 to every event in calendar "WHSmith"
repeat with i from 1 to number of items in e_vent1
set this_item1 to item i of e_vent1
set startdate1 to start date of this_item1
set startdate1 to date string of startdate1
if startdate1 is today then
set counter1 to counter1 + 1
end if
end repeat
else
-- Find tube status
try
set TID to AppleScript's text item delimiters
set webadress to do shell script "curl http://www.tfl.gov.uk/tfl/livetravelnews/realtime/tube/tube-piccadilly-now.html"

set AppleScript's text item delimiters to ¬
"<p>"
set stat to text item 3 of webadress
set AppleScript's text item delimiters to "<br ><br ></p>"
set tubestat to text item 1 of stat
set AppleScript's text item delimiters to TID
on error
set tubestat to "error unable to retrieve tube status"
end try
end if
quit
end tell
on error
set counter to "error"
set counter1 to "error"
end try

--count unread emails
set unreadCount to 0

set exlcudeMailboxes to {"Drafts", "Trash", "Sent", "Sent Messages", "Junk", "Spam", "Outbox"}

tell application "Mail"

-- get mailboxes with unread count
set mailboxesWithUnreadMessages to mailboxes of accounts whose unread count is not 0
set localMailboxesWithUnreadMessages to every mailbox whose unread count is not 0

-- loop through all accounts and mailboxes
set returnArray to {}
repeat with currentAccount in mailboxesWithUnreadMessages & localMailboxesWithUnreadMessages
repeat with currentMailbox in currentAccount

-- get mailbox attributes
set mailboxName to name of currentMailbox
set mailboxCount to unread count of currentMailbox

if mailboxName is not in exlcudeMailboxes then

-- increment total count
set unreadCount to unreadCount + mailboxCount
end if

end repeat
end repeat

end tell

--open quicktime player
tell application "QuickTime Player"
run
end tell

-- get the time

-- 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

set thetime to (theHour & ":" & theMin & "") as string

--play alarm file on loop
set volume output volume 30
tell application "QuickTime Player"
open "/volumes/user/documents/alarm.caf"
tell document 1
set looping to true
end tell
end tell

--stop alarm
set sleeping to 1
set awake to 4
delay 5
repeat with i from 1 to 180
delay 1
set display_sleep_state to do shell script "ioreg -n IODisplayWrangler |grep -i IOPowerManagement"
if display_sleep_state contains awake then
tell application "QuickTime Player"
quit
end tell
exit repeat
end if
end repeat

if display_sleep_state contains sleeping then
tell application "System Events" to key code 59
tell application "QuickTime Player"
quit
end tell
end if


--Say good morning
set d to (date string of (current date))
set volume output volume 20
say "Good morning Aravinth, it is time to wake up."
delay 1
say "It is " & d
say "And the time is " & thetime
set volume output volume 20

--say weather
delay 1
say "The current temperature in London is " & temp
if temp is not equal to "error unable to retrieve temperature" then
say "degrees celcius"
end if
say "and the outlook for today is " & outlook

--say number of unread emails and capital loading
delay 2
say "you have " & unreadCount
if unreadCount is equal to 1 then
say "unread email"
else
say "unread emails"
end if
delay 2

--say calendar events
if counter is equal to "error" or counter1 is equal to "error" then
say "There was an error retrieving your calendar for today"
else
if counter is equal to 1 then
say "and one lecture today"
delay 2
say "the current tube status "
delay 0.5
say tubestat
else if counter is greater than 1 then
say "and " & counter
say "lectures today"
delay 2
say "the current tube status "
delay 0.5
say tubestat
else if counter1 is greater than 0 then
say "your calendar shows you have work today"
else
say "it appears you are on holiday as you have no lectures or work events in your calendar"
end if
end if

--Open Captial FM stream in Quicktime Player and open itunes on error
if temp is equal to "error unable to retrieve temperature" or outlook is equal to "error unable to retrieve outlook" then
say "Unable to open Capital Radio stream, iTunes will now play from your music library"
tell application "QuickTime Player"
quit
end tell
tell application "iTunes"
set sound volume to 100
play user playlist "music"
end tell
else
tell application "QuickTime Player"
--open location "http://mediaweb.musicradio.com/V1/playlist.asx?StreamID=1"
quit
end tell
tell application "Safari"
set thisDocument to make new document
set URL of thisDocument to "http://mediaweb.musicradio.com/player/default.asp?s=1&e=0"

end tell
say "Capital Radio is now loading"
end if

--open Capital FM with Eyetv
--tell application "EyeTV"
-- launch
-- activate
--volume_change level 0.01
--delay 2
--volume_change level 0.85
--hide controller_window
--set current channel to 221
--enter full screen

--end tell

--hide windows
tell application "System Events"
set visible of (every process whose visible is true and frontmost is false) to false
set visible of process "Finder" to false
set visible of process "Mail" to false
end tell
tell application "Mail"
quit
end tell
--Fade in volume
set volume output volume 5
delay 3
repeat with i from 5 to 20
set volume output volume i
delay 1
end repeat
--dialog boxes
if temp is equal to "error unable to retrieve temperature" or outlook is equal to "error unable to retrieve outlook" or counter is equal to "error" or counter1 is equal to "error" then
display alert "Your wake up routine did not complete sucessfully, Capital FM is now playing. Please check internet conection and event log" buttons {"Okay"} default button 1
else
set question to display dialog "Your wake up routine has been completed sucessfully, what would you like to do?" buttons {"Continue listening to Capital Radio", "Snooze for 5 minutes", "Switch off Radio"} default button 1 giving up after 10
set answer to button returned of question

if answer is equal to "Continue listening to Capital Radio" then
display dialog "Have a good day, Capital Radio has been playing since " & thetime buttons {"OK!"} default button 1
end if

if answer is equal to "Snooze for 5 minutes" then
set volume output volume 0
display dialog "The volume has been muted for 5 minutes" buttons {"OK!"} default button 1
delay 298

set volume output volume 18
end if

if answer is equal to "Switch off Radio" then
tell application "EyeTV"
quit
end tell
display dialog "Capital Radio is now switched off" buttons {"close"} default button 1
end if

if answer is equal to "" then
beep 3
end if
end if

on error
set volume output volume 40
say "Wake up routine failed"
delay 10
beep 50

end try
 

samcox89

macrumors newbie
Nov 7, 2007
5
0
Hi sorry to dig this up again but,

Ive been using the weather section from this script for sometime now and today it failed.

Code:
try
	set TID to AppleScript's text item delimiters
	set webadress to do shell script "curl http://i.wund.com/global/stations/03772.html#conditions"
	-- Find temperature
	set AppleScript's text item delimiters to ¬
		"<span class=\"nowrap\"><b>"
	set text1 to text item 3 of webadress
	set AppleScript's text item delimiters to "</b>°"
	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
on error
	set temp to "error unable to retrieve temperature"
	set outlook to "error unable to retrieve outlook"
end try

The result is that it errors out.

Any ideas?
 

andy 111r

macrumors newbie
Nov 13, 2010
11
0
leeds
Hi sorry to dig this up again but,

Ive been using the weather section from this script for sometime now and today it failed.

Code:
try
	set TID to AppleScript's text item delimiters
	set webadress to do shell script "curl http://i.wund.com/global/stations/03772.html#conditions"
	-- Find temperature
	set AppleScript's text item delimiters to ¬
		"<span class=\"nowrap\"><b>"
	set text1 to text item 3 of webadress
	set AppleScript's text item delimiters to "</b>°"
	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
on error
	set temp to "error unable to retrieve temperature"
	set outlook to "error unable to retrieve outlook"
end try

The result is that it errors out.

Any ideas?


Yes i guess the html of the site has changed as it could not find the "</b>°" delimiter. Anyway this is a quick solution....

Code:
try
	set TID to AppleScript's text item delimiters
	set webadress to do shell script "curl http://i.wund.com/global/stations/03772.html#conditions"
	-- Find temperature
	set AppleScript's text item delimiters to ¬
		"<span class=\"nowrap\"><b>"
	set text1 to text item 3 of webadress
	set temp to first word of text1
	-- 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
	
on error
	set temp to "error unable to retrieve temperature"
	set outlook to "error unable to retrieve outlook"
end try
 

samcox89

macrumors newbie
Nov 7, 2007
5
0
Thats great thanks.

Just out of interest.. does anyone know if there any how to's that go through pulling sections from websites with Applescript? This is something i'm quite interested in learning.
 

kolax

macrumors G3
Mar 20, 2007
9,181
115
Been any updates to your alarm clock script?

Where did you get your equipment for opening the curtains and turning on your light?
 

christianboyce

macrumors newbie
Jun 19, 2007
18
7
Faster way to get the iCal events

Looping through the list of events takes a long time. You can do it faster this way:

set the_events to (every event whose start date > today and start date < tomorrow)

Obviously you have to define "today" and "tomorrow" but that's easy.

I made my own script to read the calendar and blogged about it here:

http://christianboyce.blogspot.com/2009/09/your-appointments-sir_4544.html

Additionally, I use "Weatherman" (which is scriptable) to get the weather in two locations. I extract the information I need from Weatherman and have my Mac speak that too. If you're interested in the Weatherman script let me know.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.