.

Icy Cellar 3

Twitter client on CotEditor


Here is a Ruby script that posts selected text on CotEditor to Twitter. It requires RubyGems and Ruby Twitter Gem. Perhaps you should install JSON library.

Worked on Ruby Twitter Gem 1.0.0, 1.4.1

~/Library/Application Support/CotEditor/ScriptMenu/00)Tweet.@~^p.rb

#!/opt/local/bin/ruby -Ku

# %%%{CotEditorXInput=Selection}%%%

# %%%{CotEditorXOutput=ReplaceSelection}%%%

require 'rubygems'

require 'twitter'

class TwitterClient

def initialize(consumer_key, consumer_secret, token, token_secret, lat, long)

Twitter.configure do |config|

config.consumer_key = consumer_key

config.consumer_secret = consumer_secret

config.oauth_token = token

config.oauth_token_secret = token_secret

end

@lat = lat

@long = long

@client = Twitter::Client.new

end

def postTweet(str)

post_count = 0

begin

@client.update(str, {'lat' => @lat, 'long' => @long})

print('Posted:' + "\n" + str + "\n")

rescue

if post_count < 10

post_count += 1

retry

else

print('Failed to post:' + "\n" + str + "\n")

end

end

end

end

client = TwitterClient.new('____', '____', '____', '____', '____', '____')

while line = STDIN.gets

line.chomp!

if line.length > 0

client.postTweet(line)

end

end

Worked on Ruby Twitter Gem 0.7.9

~/Library/Application Support/CotEditor/ScriptMenu/01)Tweet (for older Ruby Twitter Gem).rb

#!/opt/local/bin/ruby -Ku

# %%%{CotEditorXInput=Selection}%%%

# %%%{CotEditorXOutput=ReplaceSelection}%%%

require 'rubygems'

require 'twitter'

class TwitterClient

def initialize(consumer_key, consumer_secret, token, token_secret, lat, long)

@consumer_key = consumer_key

@consumer_secret = consumer_secret

@token = token

@token_secret = token_secret

@lat = lat

@long = long

end

def postTweet(str)

post_count = 0

begin

oauth = Twitter::OAuth.new(@consumer_key, @consumer_secret)

oauth.authorize_from_access(@token, @token_secret)

tw = Twitter::Base.new(oauth)

tw.update(str, {'lat' => @lat, 'long' => @long})

print('Posted:' + "\n" + str + "\n")

rescue

if post_count < 10

post_count += 1

retry

else

print('Failed to post:' + "\n" + str + "\n")

end

end

end

end

client = TwitterClient.new('____', '____', '____', '____', '____', '____')

while line = STDIN.gets

line.chomp!

if line.length > 0

client.postTweet(line)

end

end