Tweet from Growl/Notification Center/Applescript?

207 views Asked by At

I'm looking to automatically tweet notification center messages from my mac. Is there a way to use Growl or Applescript to achieve this? Thanks!

1

There are 1 answers

0
CRGreen On

I've been able to use the following method to script tweets.

This Applescript code is used to compose the tweet manually with a enter text dialogue and run the shell script which checks and send the tweet. It does not use text from Growl or Notification Centre.

Applescript code:

set q to display dialog "Tweet:" default answer ""

    set myTweet to text returned of q

    if (length of myTweet) > 140 then
        display dialog "I'm pretty sure that tweet is too long (" & (length of t as text) & " chars)" buttons {"oh"} default button 1
    else
        set e to do shell script "cd /pathTo/folderContaining/;./tweet.sh <yourtwitterhandle> <yourtwitterpassword> \"" & myTweet & "\""
        if e ≠ "" then display dialog e buttons {"OK"} default button 1
    end if

I've had some problems with the character limit, so I usually make my tweets extra short. Maybe you can find a 'length of' limit shorter than 140 (see AS code: if (length of myTweet) > 140 then) that will work consistently.

Here is the shell script; modified from http://360percents.com/posts/command-line-twitter-status-update-for-linux-and-mac/ ,

Make sure you name it 'tweet.sh' and make it executable using chmod or Kilometre.app (notice I've commented out all the reporting so it runs completely quietly):

#!/bin/bash

#REQUIRED PARAMS (crg - now passed through command line)
username=$1
password=$2
tweet=$3 #must be less than 140 chars

#EXTRA OPTIONS
uagent="Mozilla/5.0" #user agent (fake a browser)
sleeptime=0 #add pause between requests

if [ $(echo "$tweet" | wc -c) -gt 140 ]; then
    echo "[FAIL] Tweet must not be longer than 140 chars!" && exit 1
elif [ "$tweet" == "" ]; then
    echo "[FAIL] Nothing to tweet. Enter your text as argument." && exit 1
fi

touch "cookie.txt" #create a temp. cookie file
#crg - commented out all 'success' echos ... will only return string if error
#GRAB LOGIN TOKENS
#echo "[+] Fetching twitter.com..." && sleep $sleeptime
initpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/session/new")
token=$(echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//')

#LOGIN
#echo "[+] Submitting the login form..." && sleep $sleeptime
loginpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$token&username=$username&password=$password" "https://mobile.twitter.com/session")

#GRAB COMPOSE TWEET TOKENS
#echo "[+] Getting compose tweet page..." && sleep $sleeptime
composepage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent" "https://mobile.twitter.com/compose/tweet")

#TWEET
#echo "[+] Posting a new tweet: $tweet..." && sleep $sleeptime
tweettoken=$(echo "$composepage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
update=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$tweettoken&tweet[text]=$tweet&tweet[display_coordinates]=false" "https://mobile.twitter.com/")

#GRAB LOGOUT TOKENS
logoutpage=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" "https://mobile.twitter.com/account")

#LOGOUT
#echo "[+] Logging out..." && sleep $sleeptime
logouttoken=$(echo "$logoutpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e 's/" \/>.*//' | tail -n 1)
logout=$(curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d "authenticity_token=$logouttoken" "https://mobile.twitter.com/session/destroy")

rm "cookie.txt"