Use iCal to tweet automatically
Recently, the question came up of how to automatically "tweet" a birthday message to a friend or loved one on their special day. After thinking on this question for some time I finally came to a solution. As it turns out, intrepid TUAW blogger Dave Caolo wrote a Mac 101 article all about attaching AppleScripts to iCal events. Additionally, Brad Linder over at Download Squad put together a nice little tutorial for posting to Twitter via the command line.
It wasn't obvious at first, but these two articles together formed a dynamic duo of birthday tweeting proportions. If, dear reader, you are interested in setting up a system for sending out some birthday tweets then follow along as we travel the mysterious world of iCal events and command line tweeting. Read on for a somewhat technical tutorial on how to set up some pre-scheduled tweeting goodness. (If you're looking for a service to take care of all this for you, here you go.)
At first, I considered the possibility of using a cron job and shell script as the solution. I eventually discovered this would work, and we'll cover that later, but the melding of iCal and AppleScript was just too awesome to pass up.
The first thing we need to do is create our AppleScript which will be run by iCal. If you read Brad's article at Download Squad you have a basic idea of how this script is going to work. In the past we have covered AppleScripts so I'll skip the basics now; for more details check out this beginner's guide. Go ahead and launch Script Editor and create a new script like this one:
do shell script "curl -u username:password -d status=\"I'm in ur Tweets!\"
If the stuff in quotes seems familiar, it should -- I stole it from Brad. No, just kidding, but seriously Brad's post was a huge influence on this tutorial. Essentially, we're creating an AppleScript (which is iCal friendly) that runs a command line program to update your Twitter status. Because our AppleScript has to encapsulate the shell command in quotation marks, we have to use the UNIX escape character ("\") to tell AppleScript to ignore the quotation marks that are part of the shell command.
Replace "username" and "password" with the appropriate values as necessary and then add in your Twitter message. Press "Compile" to make sure everything is kosher and then go ahead and press "Run" to test it out. If all went according to plan your Twitter status will be updated with the test tweet you just typed (if you don't want to test with your regular Twitter account, you can create an alternate account for this purpose). Save the AppleScript somewhere that will be easy to locate. For example, I have a folder called "Scripts" in my Documents folder.
Now that we have an AppleScript that can update our Twitter status (you did save it somewhere right?) we can create an iCal event with an alarm to call our AppleScript. Using iCal, create an event called, for example, "Dad's birthday" on a given date such as July 16. Dave's article provides an excellent overview of the different types of iCal alarms that are available. The one that we are interested in is "Run Script." Selecting that option makes available a second option for choosing our birthday tweet AppleScript.
Just as with a standard message alarm, this AppleScript birthday tweet can be configured in several different ways. It can occur on the actual day at any given time or it can be configured to occur some number of days before or after. Additionally, because we used iCal for scheduling, it's possible to use multiple alerts on multiple days. You will also benefit from the fact that iCal is able to sync with MobileMe and with your iPhone. In other words, while iCal is doing your birthday Twittering for you, your iPhone can be reminding you its time to give your dad a phone call.
As I mentioned earlier it is also possible to send out some pre-scheduled Tweet-age using cron. For the uninitiated, cron is a time-based scheduler found in *nix operating systems. If you are using Windows, you can get cron set up using Cygwin. A cron-based sample of the above script looks something like this:
38 16 14 07 * curl -u username:password -d status="This was posted thanks to
cron and the Twitter API! " https://twitter.com/statuses/update.xml
I won't get into the finer points of setting up a cron job; for a good overview read more here. Basically, it goes like this: create/view cron jobs by opening Terminal.app and running "crontab -e." If this is your first cron job then the resulting file will be blank. The cron jobs will be displayed using a command line text editor called "vi." Chances are, though, that if you're trying to set up a cron job you know how to use vi; for more on vi start here.
The beginning part of the line tells you at what interval to repeat the task. In the above example, the task (which starts with "curl" by the way) would be repeated on the 38th minute of the 16th hour of the 14th day of the 7th month. The simplest way of saying that is it will repeat on July 14 at 4:38PM. If you are setting up your cron job using OS X there is a great GUI program called Cronnix which can be used for easily setting up new cron jobs.
One thing to consider when using a shell script: the Twitter account credentials will be stored in plain text. For this reason it might be a good idea to store the credentials in ~/.netrc and chmod 600 ~/.netrc to prevent unwanted access from other users. In the case of using .netrc to store the user credentials the command would then look like this:
38 16 14 07 * curl -n -d status="This was posted thanks to cron and the Twitter
API! " https://twitter.com/statuses/update.xml
Whether you use cron and a shell script or iCal and an AppleScript there are about a million other applications for automation and Twitter. It's clear that Twitter has taken the world by storm and with a little code sorcery you can really amp up your ninja ways. If you have any other Twitter automation tips throw them out in the comments; I am always looking for ways in which I can solve a repetitive problem with a little bit of coding.
Update: It seems that a couple people are having trouble getting this to work. The first thing I have run into is that there seems to be a problem with having special characters at the end of your tweet. If you are going to conclude your message with an exclamation point and are having problems try adding a space afterwards. Twitter will trim off any trailing spaces and it does not seem to count against the 140 character limit.
Another issue that has cropped up is that cURL is not able to authenticate Twitter's SSL certificate when using HTTPS. The solution is to either add the "-k" option before "-u" (more secure) or to change the script to use HTTP instead of HTTPS (less secure). As always, if you have any other issues speak up in the comments.