Hi, this is Gary with MacMost.com. Let's use the Terminal to create a custom chime for
a Mac. MacMost is brought to you thanks to a community
of supporters. Join us and get exclusive content at macmost.com/patreon.
So in System Preferences, under Date & Time, Clock, you have the ability to set Announce
the time and have it speak the time on the hour and half hour or quarter hour. That's
fine if you want it to actually speak the time and you want to do it at one of those
intervals. But if you want it to play a sound or wanted to do it every ten minutes or twenty
minutes or something then you're going to have to create your own. So to do this run
the Terminal app.
What we're going to do is we're going to use something called Crontab which is the way
commands run on a regular schedule. First, let's learn how to play a sound. To do that
we're going to use the command afplay. To get a sound to play we can find a sound we
have on our hard drive or we can use one of the system sounds. I'm going to go into Finder
here and then go to the Computer level and in the Hard drive, System, Library, I can
look for the Sounds folder. In there I see the system sounds. So I'm going to use the
Morse one here and to put the path to that in there very easily I'm just going to drag
and drop it into terminal and you can see it inserts the path. I could have typed that
as well. Now if I hit Return terminal will do what I want which is to play that sound.
So I'm going to select that and copy that.
Now I'm going to setup the regular schedule for this command by typing Crontab dash e.
This enters an editor, the vi editor, which allows us to add lines to this which will
do things on a regular schedule. To add a line I'm going to do i, so press the i key
for insert, and I'm going to type the command. The first five things are the time. The minute,
the hour, the day, the month, and the day of the week. You can use an asterisk to represent
any. So five asterisks say basically play every minute of every hour of every day of
every month of every day of the week.
If I wanted it to be only on, say, the 25th minute after an hour I could do 25 there and
then it play it at 1:25, 2:25, etc. If I wanted it to play every ten minutes I could do asterisk
slash 10 and it would do it every ten minutes of every hour of every day, etc. For a test
here I'm going to have it do every minute and the command I'm just going to paste in
there is that afplay command. Now to Save this I hit the Escape key to exit insert mode.
I hit colon to start the command. You can see at the bottom there is command. W for
write and Q for quit.
I hit Return and I'm going to get this dialogue now saying that the Terminal wants to make
a change here and I'm going to have to allow it to make a change to that Crontab. To test
it I can go and do Crontab-L and see what's there. Now let's wait for the minute to change
and we'll hear the sound. There we go! So now we could modify this. I could go and do
Crontab -e again and I can hit the i key for insert again and I could add /10 after that
for every ten minutes say or /15 for every fifteen minutes. I'm going to Escape, colon,
w, q, to exit.
Let's try something more interesting. Let's have it actually speak the time. So you could
use the Say command to say anything. We could have that speak the time by using the Date
command. So we could do date and then in quotes plus and then percent, lower case l, colon,
percent, capital M. (date "+%l:%M"). This will print out the date in a format like that.
So now we can actually have it say that by piping that same thing. Pipe is the character
that's between the delete and Return key on your keyboard. Shift and it's a vertical bar.
Then you can say send that to Say. (10:53)
We can furthermore go and do something with the voice. We can say dash v (-v) to give
it a different voice. There's a variety of different voices. If you do Say -v and space
? you get a list of all the voices. You can try these out by using this command again
but with another one. So we can do, for instance, (voice says 10:53). So let's copy that and
you think we should be able to paste that directly into that Crontab. But we can't.
We have to create a special shell script to do that.
So we're going to go to our Documents folder and then I'm going to create this file by
typing vi to edit directly and calling it chime.sh for shell script. Now I'm going to
do i to paste in this with command v and then I'm going to Esc colon w q, and it writes
it out. Now if I do LS to list the directory there I see everything that's in my Documents
folder including that chime.sh. If I do period slash chime.sh and I'll test it out. You can
see there I forgot to set permissions for it. Set you want to set permissions. chmod
755 to make it executable chime.sh. Now when I test it out, it works.
So now I can edit the Cron and change the command here from playing this sound to using
my shell script. So I'll go to the end here, i for insert, delete all of this, and then
I'll give it a path to that file. For testing purposes let's change it to do it every minute
again. So I'll do Esc : w q to write it out. It'll ask me to Save it. Now let's wait for
the minute to change. (voice says 10:57). There we go. We have our chime anyway we want
it. With a sound or with a spoken time.
Note that if you wanted to run only between certain hours you can easily do that by setting
it up like this. So let's do i for insert, say every 15 minutes and instead of asterisk
for the hour I can say, okay between 7 and say 5 in the afternoon which would be 17.
So hours 7 and 17 in a 24 hour clock. We'll get it starting at 7 o'clock we'll go all
the way to 5:45 which would be the last occurrence that fits within this. If we wanted to only
go to say 4:45 you would do this. If you wanted it to do 4:45 but also 5:00 then what you
would do is add a new line here with the same command on it and say you wanted it to do
at exactly 5:00. You could do zero for on the hour and then 17, for 5 o'clock. This
separate line would just give you that one 5 o'clock chime. These would give you everything
from 7 o'clock
to 4:45.