Speak the time on the hour via Terminal
Jan 02, '08 07:30:01AM
Contributed by: Anonymous
Some may be familiar the say command, where you simply open the terminal and type say (something) and your mac will speak. This is especially fun when you are logged in remotely via ssh, and someone is close to your mac at home.
Anyhow, by combining the say and date commands, you can then set up a cron job that will allow your Mac to speak the time at any interval you choose. I'm not sure if there are utilities out there that can do this, but this is certainly a good way learn how to use some of the Unix goodness in OS X.
[robg adds: You can set up a voice announcement on the Clock tab of the Date & Time System Preferences panel -- at least on the hour, half-hour, or quarter hour. Read on, however, if you're new to the Unix side of OS X and you're interested in learning a bit more about it. This solution also has the advantage of running for all users, not just for the logged-in user.]
Here's what to do:
- Open Terminal, in Applications/Utilities, and navigate to a directory where you want to store the script. For example, type cd /Users/username/scripts.
- Type sudo -s, press Return, and enter your admin password when prompted. You're now logged in (inside Terminal) as the root user.
- Type nano time.sh (or you could use another text editor if you've done this before). You'll get a screen where you'll type in the info for your shell script.
- Type say it is now `date +%I`, oh clock. Be sure to use the ` symbol and not the ' symbol -- the correct one is found on the same key as the tilde (~). Explanation: `date +%I` returns the value of the current hour in non-military format. You can use man date to see different outputs options. Using oh clock seems to be the best way to get the computer to say it correctly -- say cannot use contractions.
- Press Control-X, then Y, then press Enter to save your file.
- Now you have to make the script executable. Type chmod +x time.sh and press Return.
Test the script by typing ./time.sh and pressing Return. You should hear a male voice saying "It is now nn O'clock." Now we want to make a cron job to execute the script at specific intervals, like every hour. This is also very easy.
- Type crontab -e at the Terminal prompt. You'll get a blank screen, and you are using the vi editor by default.
- Press the letter i -- this will put you into 'insert' mode. You should see the word insert at the bottom of the screen.
- To make this script run every hour, on the hour, enter the following on the screen:
00 * * * * /Users/(username)/scripts/time.sh
cron uses a specific time format which you can read about, and 00 represents "on the hour."
- Press Escape; this will take you out of insert mode, then type :wq!, and this will save your crontab file and quit the editor.
- Type exit to end your root shell.
At the top of the next hour (assuming it's not less than three minutes away), you should hear a male voice telling you what time it is. I hope this was a useful and educational experience for you.
Comments (9)
Mac OS X Hints
http://hints.macworld.com/article.php?story=20071226130633932