Advertisement
  1. Computer Skills
  2. Terminal
Computers

How to Tweet From Terminal on a Mac

by
Difficulty:AdvancedLength:ShortLanguages:

Screencast

Before Apple pioneered the desktop environment and graphical user interfaces, or GUIs, all computers were controlled through the command line. To do anything with a computer you entered the relevant command as a string of text pressed Return and let the machine do its thing. For the most part, GUIs have supplanted command lines but, through the Terminal app, it’s still possible to use one on modern Macs.

Often, the reason for using the Terminal is that you get more powerful control over the operating system—for example you can do things with Time Machine using the command line that you can’t do with the System Preferences pane. 

For more on using the Terminal check out the Taming the Terminal series.

Sometimes, though, the real reason to use the Terminal is that it’s fun and educational. This tutorial is one of these situations. In it, I’ll show you how to use Twitter from the  Terminal.

Introducing t

For this tutorial I’m using a Ruby gem called t developed by Erik Michaels-Ober. With t you can access the Twitter API from the Terminal and do things like post a tweet, view your timeline, search Twitter and follow other users.

You can also combine multiple commands and use command line tools like pipes, grep, awk and xargs to perform operations that are impossible with regular Twitter apps..

Installing Ruby With Ruby Version Manager

To work, t requires that Ruby is installed on the Mac. The simplest way to install Ruby on OS X is to use Ruby Version Manager (RVM).

Open the Terminal and paste in \curl -sSL https://get.rvm.io | bash -s stable –ruby and press Return to install the latest version of Ruby.

If you already have Ruby installed using RVM run ruby -v to check what version you have. If it isn’t the latest, run rvm get stable to update it.

For more information on using RVM check out their install guide.

Creating a Twitter App

To access the Twitter API you need to register an app; to do that you need to add a phone number to your Twitter account.

  • First, log in to your Twitter account and navigate to the Mobile settings page
  • Enter your phone number and click Continue. Within a few moments you’ll receive a verification code. 
  • Enter it and then click Activate phone to complete the registration.

Tip: Twitter automatically enables text notifications. I recommend you revisit the Mobile settings page and turn them off.

Visit apps.twitter.com and select Create New App.

app creating
Creating a Twitter app.

Enter a unique Name for your app and a short Description of what it does. You’re required to enter a Website as well but if you don’t have one a placeholder will do.

Accept the Developer Agreement and then click Create your Twitter application.

For t to work, it needs access to the read, write and direct message APIs. By default, the new Twitter app only has access to the read and write ones. On the app management page, under Application Settings click modify app permissions. Change the the setting to Read, Write and Access direct messages and then click Update Settings.

With that done, it’s time to set up t.

Setting Up t

Open the Terminal and enter gem install t to download and install t.

Next run the command t authorize.

Your web browser will open to the Twitter Application Management page. Sign in with your Twitter account and select the app you created earlier. 

In the Keys and Access Tokens tab, copy the app’s Consumer Key (API Key) into the Terminal and press Return. Afterwards, do the same for the Consumer Secret (API Secret).

app keys
The Consumer Key and Consumer Secret for my Twitter app.

To finish setting up t, press Return to open the Twitter app authorization page. Sign in with your Twitter account and copy and paste the authorization pin into the Terminal. Press Return one final time to finish setting up t.

Posting a Status to Twitter

With t set up the first thing to do is send a tweet announcing that you’re tweeting from the command line. To do so, use t’s update command. 

The update command takes a single argument: the message you want to tweet.

tweeting command line
Sending a tweet from the command line.

For example, you should use something like t update “Hey @tutsplus fans, this tweet was sent from the command line. Check out @TutsCompSkills later this month to find out how.”

tweet
A tweet sent from the command line.

Other t Commands

While tweeting straight from the command line is fun, t has a lot more powerful features. To see all the available commands run t help. To get more information on a specific command such as the arguments it takes, run t help [command].

Combining these commands with other Terminal features such as awk, pipes and xarg means you can do things like unfollow everyone, mute the most prolific tweeters you follow and lots more.

Stream Your Timeline

Although the Terminal is no where near as pretty as a proper Twitter app like Tweetbot, there is still a certain throwback appeal to a barebones text interface. With t, the Terminal can be turned into a streaming Twitter client.

t streamiing
Streaming my timeline with t.

To do so, run the command t stream timeline. This will start your timeline streaming until you press Control-C to stop it.

Search Twitter

One of t’s major features is the powerful search command. With t you can search the whole of Twitter or any user’s timeline, favourites, mentions or retweets.

For example, to find all tweets that mention Tuts+ run t search all “@tutsplus”. On the other hand, to see every time the Tuts+ Twitter account has mentioned me, run t search timeline @tutsplus “@harryguinness”.

t search
Searching Twitter with t.

If you’re looking to dig deep into what is being said on Twitter, t is a great way to do it.

Unfollow Everyone

If you’re anything like me, over time you’ll find the number of other users you follow slowly grows until using Twitter is next to impossible. When this happens I feel like just unfollowing everyone and starting from scratch. With t this is easy.

To unfollow everyone run t followings | awk ‘{print $1}’ | xargs t unfollow -i.

Mute the Five Most Prolific Tweeters You Follow

If it’s just a few people you follow who are filling your timeline and it’s politically difficult for you to unfollow them, you can use t to mute the worst cases.

To mute the five most prolific tweeters you follow run t followings -l –sort=tweets | tail –10 | awk ‘{print $1}’ | xargs t mute -i

Conclusion

There are countless other things you can do with t. The best way to get a feel for what is possible is to check out t’s documentation on GitHub as well as the t help command. You can even write Bash scripts that use t to create your own specialised Twitter apps.

If you’re having any difficulties using t, or discover any awesome commands, please let me know in the comments.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.