Hi there! This tutorial is going to show you how to post “tweets” to Twitter. The following code comes directly from my ASTwitterLibrary, which is a complete library of methods for Applescript that you can use to interact with Twitter.
Posting a tweet is very simple. First we need to set our username and password as properties. Let’s go ahead and do this.
property thePassword : "passwordhere" – your password
Now, since we are such nice tidy coders, and we appreciate convenience, we are going to put all of our main code in a method (or subroutine as some call it.) The reason for this is because it’s a chunk of code we could be calling over and over again and we want to make it easy to post the tweet. So, let us first create the methods name and add its parameters.
– Our code shall go here!!
end postTweet
Cool beans. So now we need to add some content to this method. We are simply going to use a cURL shell script to post our tweet. This will suffice. It sends a cURL call to Twitter’s update.xml file using the given username, password and message.
set TwitterUpdate to "/usr/bin/curl" & ¬
" –user " & quoted form of (theUser & ":" & thePassword) & ¬
" –data status=" & quoted form of encodeURL(theMessage) & ¬
" http://twitter.com/statuses/update.xml"
end postTweet
Now you may have noticed a little method called in that shell script called encodeURL. This is a little method we must make to encode our URL before sending it with cURL. We’re going to call a little python script through a shell script to do this for us. Here’s the method.
return do shell script "/usr/bin/python -c ‘" & ¬
"from sys import argv; " & ¬
"from urllib import quote; " & ¬
"print quote(unicode(argv[1], \"utf8\"))’ " & quoted form of someURL
end encodeURL
Next we need to actually execute the shell script, and throw in a little error handler as well.
set TwitterUpdate to "/usr/bin/curl" & ¬
" –user " & quoted form of (theUser & ":" & thePassword) & ¬
" –data status=" & quoted form of encodeURL(theMessage) & ¬
" http://twitter.com/statuses/update.xml"
– Now try sending the Tweet to Twitter
set TwitterResponse to do shell script TwitterUpdate
if TwitterResponse contains "Could not authenticate" then
return "Tweet failed to post. Your username or password may be incorrect, or Twitter might be expieriencing difficulties."
else
return "Tweet successfully posted"
end if
end postTweet
And there you have it! Now if you want to post a tweet just call the method!
property thePassword : "passwordhere" – your password
on postTweet(theUser, thePassword, theMessage)
set TwitterUpdate to "/usr/bin/curl" & ¬
" –user " & quoted form of (theUser & ":" & thePassword) & ¬
" –data status=" & quoted form of encodeURL(theMessage) & ¬
" http://twitter.com/statuses/update.xml"
– Now try sending the Tweet to Twitter
set TwitterResponse to do shell script TwitterUpdate
if TwitterResponse contains "Could not authenticate" then
return "Tweet failed to post. Your username or password may be incorrect, or Twitter might be expieriencing difficulties."
else
return "Tweet successfully posted"
end if
end postTweet
on encodeURL(someURL)
return do shell script "/usr/bin/python -c ‘" & ¬
"from sys import argv; " & ¬
"from urllib import quote; " & ¬
"print quote(unicode(argv[1], \"utf8\"))’ " & quoted form of someURL
end encodeURL
postTweet (theUser, thePassword, "I’m posting this tweet through Applescript! Awesome!")
And that’s it! A simple way to post a tweet using Applescript and a clever little cURL call. Have fun!









7 Responses
Beware: “–” indicating a comment has been replaced with “–” preventing the script from compiling. Easily fixed, though
Ah, it’s the software running this site that’s the problem. It’s converting two minus signs into a dash.
Yes, it’s the syntax highlighting script this site uses that converts the double minus signs to a dash.
Your Comments
how can I make this script to call a text file that contains the tweets and send each of the tweets let’s say in 10 minutes time frame?
no rx valium scans ultram by money order Assoc ultram fedex StoryModul order Topamax without prescription subjects ambien buy in UK Klien tramadol discount baseball tramadol free shipping expectations phentermine purchase MEMORIES soma free shipping Tucows tramadol buy in UK CORPUS soma online medication convertions
tell current application
do shell script “/usr/bin/python -c ‘from sys import argv; from urllib import quote; print quote(unicode(argv[1], \”utf8\”))’ ‘I’m testing this tweet through Applescript! Awesome!’”
“sh: -c: line 0: syntax error near unexpected token `(’
sh: -c: line 0: `/usr/bin/python -c ‘from sys import argv; from urllib import quote; print quote(unicode(argv[1], \”utf8\”))’ ‘I’m testing this tweet through Applescript! Awesome!””
sorry, that post was mine..Getting an error..The error was the original post.