If you have a Twitter account and is behind a Mac you can easily post new Tweets from the Terminal without having to login on their website. To achieve this we’re using the in-built function in Mac called cURL.
cURLcURL is an inbuilt tool in Mac OS X which lets you transfer data from or to a server, using one of the many supported protocols (HTTP,HTTPS, FTP, FTPS, SCP, SFTP, TFTP, DICT, TELNET, LDAP or FILE). How to use cURL in the Terminal for Mac OS X.
Post new Tweets on Twitter from the Terminal window
The syntax used to post new tweets looks like this:
curl -u username:password -d status=”My status update” http://twitter.com/statuses/update.xml
- Start by opening the Terminal window on your Mac.
- Let’s say your username on Twitter is “my@username.com” and your password is “test“.
- Write the following in your Terminal window:
curl -u my@username.com:test -d status=”I’m currently twittering from the Terminal in Mac OS X” http://twitter.com/statuses/update.xml
If you now login to your Twitter account the regular way (in a web browser) you should see your new tweet.
See also – Ping an IP address or hostname in the Terminal for Mac OS X
Make it even easier to tweet from the Terminal
Even if the example above if relatively simple, you might start to feel annoyed by having to enter your username and password each time you want to post a new tweet. This can be avoided by doing the following:
- Create a new file called “tweet.sh” in the root of your harddrive.
In the Terminal window you can do this by entering the following two commands:
cd /
touch tweet.sh - When you’re done, add the following content into your new file (tweet.sh) och save the changes:#!/bin/bash
curl -u my@username.com:test -d status=”$1″ http://twitter.com/statuses/update.xml - Before you can try it out you have to make sure the file is executable. This can be achieved by setting the necessary permissions on the file using the following command:
chmod u+x tweet.sh - When this is done you can post new tweets even easier by entering the following in the Terminal:
./tweet.sh “Now I’m twittering even easier from my Mac”
Attention: Be aware that you’re storing both your username and password in plain text in “tweet.sh”. This means that everyone who has access to your computer can get your login credentials to Twitter by just opening this file.
One way to avoid this is to slightly alter the content of tweet.sh which will require you to enter your username and password everytime you want to post a new tweet. To achieve this, change the content of tweet.sh to the following:
#!/bin/bash
curl -u $1:$2 -d status=”$3″ http://twitter.com/statuses/update.xml
Don’t forget to save the changes after editing your file.
When all this is done you can post new tweets in a more secure way by entering the following in the Terminal window:
./tweet.sh “my@username.com” “test” “Now I’m twittering both easy and secure from the Terminal window of my Mac”
Links