Twerminal – twittering from Terminal.app

Twitter-Terminal

Do you want to twitter from the OSX command line, a.k.a. Terminal.app? Let’s do that, don’t be scared to use the terminal; DIYers can do this — I promise.

First , a list of things we need. 

  1. Terminal.app  - (check) you’ve got this inside Applications/Utilities folder; you could just press cmd+space bar, then type “Terminal” in Spotlight, then enter
  2. Internet connection – (I feel silly writing this down, but just so the list is complete) 
  3. A text editor – (check) TextEdit will do, you’ve also got this inside Applications/Utilities

And finally curl – What? don’t .. don’t .. don’t move away from the page just yet — I’ll try to explain curl in a non-geeky way (as much as possible). 

Curl is command line program that you would use to move files around — pretty much like cut and paste ; command-V then command-C. But it does so, using the syntax of the web — so you can toss the files using http, ftp, scp and many more. I think that’s about enough background that we need about curl.

Why are we using curl to talk to Twitter? 

Because the Twitter functionalities , like updating status, sending a message, getting timelines etc., are directly reachable via a URL (RESTful), we will use curl’s ability to move a file from your Twerminal – ahem, Terminal – and take it to the Twitter function. Once Twitter receives our message, the ball is in it’s court, and our work is done. 

Enough background, let’s work now 

1. Fire up the Terminal.app, press command+space bar, type Terminal .. hit enter. 
2. Just to make sure, type  ”$ cd ~” — that’s the tilde character, it’s on the far left of your Mac keyboard. It means cd to your home directory. 
3. Now, type

$ touch t
$ open t

The touch command created a plain text file called “t”, and the open command will launch TextEdit.

4. You will need the “t” file to be executable (I’d like to explain this, but I will digress; maybe another post). Just type this on the Terminal.

$ chmod u=rwx t
$ ls -l t

The chmod command changed the mode (permissions) of the “t” file, the second command “ls -l” is just so you can look and verify if it really was changed, you should see something like this

_$ ls -l t
-rwxr–r–@ 1 tedheich staff 191 Apr 17 22:46 t
_$

You need to do the chmod command. Otherwise, later on when you press “t” from the Terminal, OSX won’t let you run the script — so this is important.

Now that the file where we will write our script is created — file “t” — , and it’s also executable, you can type the following (or copy and paste) into TextEdit.


#! /bin/sh

user="type-your-twitter-username-here"
password="type-your-password-here"

echo "What are you doing right now? "
read message
curl -u  $user:$password -d "status=$message" "http://twitter.com/statuses/update.json"

# This is just a remark to add whitespace above

Lots of things going on there, here’s the breakdown.

Line #3 – the “user” variable is defined, i’m just assigning the text “tedheich” to a variable called “user”. Why? So that it’s easier for you to put your own name, this way, you don’t have to mess around and stick your name in line #8.

Line#4 – same reason as line #3, this is where you stick your twitter password.

Line #6 – I’m just writing a message to the screen, so that you’ll know the script is asking you to do something. Type what ever you would type on the twitter status, then hit enter.

Line #7 – After you hit enter, the script needs a way to remember what you typed, so I’m putting it inside the “message” variable.

Line #8 – This actually came from the Twitter tutorial, this is where curl will do it’s magic. Can you see now why I wrote lines #3 and #4? For old hats on scripting, it’s a bit ninny-ish verbosing what could be accomplished in a 1-liner. But if you’re just starting out with scripts, I thought poka-yoking the script a bit won’t hurt.

NOTE : if you didn’t copy and paste the script, instead chose to type it up yourself, just make sure that there are no white spaces before and after the “=” signs. Type it exactly as you see it.

By the way, this script is tested on a Mac OSX (Intel) 10.5.6

To test it out, just type

$ t
$ What are you doing right now?
I’m just about finished writing the twerminal post, I’ll be hitting publish in a little while

That’s it. I hope the Terminal intro wasn’t so shocking. If you encounter some problems, drop me a note .. or comment, I’ll get back to you.

I might continue working on this after all, you can follow this little script in code.google.com, here’s the svn address

svn checkout http://twerminal.googlecode.com/svn/trunk/ twerminal-read-only

By the way, don’t forget to drop by TwitterTip for beginners, it’s cool that you have a new toy; but it’s best that you brush up on some Twitter rules — both written and unwritten (especially the unwritten ones).

Post navigation
(previous post)
(next post)
| | | | .

{0 Comments .. you can add one }

Leave a comment