One of the features of the standard additions is the current date and the ability to speak out loud.
set volume 1
copy the (current date) to theDate
say theDate as string
This makes your computer quiet (so you don’t bother other people near where you are using this tutorial), and then it copies the current date into a “variable” called “theDate”. Finally, it says whatever is in the variable “theDate”. The “say” command can only say text. If you try to say something other than text, AppleScript tries to send that thing a message called “say” instead of sending that item to “say”. So you need to tell AppleScript to convert that thing to text first. This is why we put the “as string” after “say theDate”.
You can look at what it is speaking by adding “get theDate” to the end of your script and then looking at your window titled “the result” (pull down the Windows menu to see this listed).
What it is speaking is a overly complex. It is reading the numbers without recognizing what they are. For example, it is reading 2002 as “twenty zero two” on the computer on which I’m writing this, and it is trying to speak the letters “AM” as a word “am”. We’ll need to help it along.
--be nice to our neighbors
set volume 1
on idle
--get the current date and time
copy the (current date) to theDate
--get the weekday
copy the weekday of theDate as string to theDay
--the hours (from 1 to 12)
copy the hours of theDate to theHour
if theHour is greater than 12 then
--13:00 to 23:00
set theHour to theHour - 12
set theAMPM to "PM"
else if theHour is "12" then
--12:00
set theAMPM to "PM"
else if theHour is "0" then
--00:00
set theHour to "12"
set theAMPM to "AM"
else
--01:00 to 11:00
set theAMPM to "AM"
end if
--the minutes (including preceding zero)
copy the minutes of theDate to theMinutes
if theMinutes is less than 10 then
set theMinutes to "0" & theMinutes
end if
say theDay & ", " & theHour & ":" & theMinutes & " " & theAMPM
return 60
end idle
The “ampersand” or “&” character glues together two pieces of text. In this case, we’re putting our variable “theHour” after our variable “theDay” and putting a comma and space between them. Then, adding theMinutes with a colon in front of it, and then theAMPM with a space in front of it. So it will end up looking like “Wednesday, 6:21 PM”.
Which then gets spoken.
Make sure you save your talking clock as an “Application” that will “Stay Open”.