Adrian Nier Code

Back to overview

Post to Twitter

Last modification: Thursday, April 30, 2009 06:32 pm

This script takes the string that is passed by LaunchBar and posts it to Twitter.

Implementation

on handle_string(_string)
   
   -- Init
   set _maxCharCount to 140
   set _charCount to (count characters of _string)
   set _twitterUpdateURL to "http://twitter.com/statuses/update.json"
   try
      -- Display error if the string is too long
      if _charCount > _maxCharCount then
         error "Your tweet has \"" & (_charCount as string) & "\" characters and is too long."
      end if
      
      -- Get credentials
      tell application "Keychain Scripting"
         set twitter_key to first Internet key of current keychain whose server is "twitter.com"
         set twitter_login to quoted form of (account of twitter_key & ":" & password of twitter_key)
      end tell
      
      -- Prepare string for curl
      set twitter_status to quoted form of ("status=" & _string)
      
      -- Send string    
      do shell script "curl --user " & twitter_login & " --data-binary " & twitter_status & " " & quoted form of _twitterUpdateURL
      
      -- Hide LaunchBar
      tell application "System Events" to set visible of process "LaunchBar" to false
      
   on error _eMsg number _eNum
      activate
      display alert "Error while tweeting from LaunchBar" message ((_eMsg as string) & " (" & (_eNum as string) & ")") buttons {"OK"} default button "OK"
   end try
   
end handle_string