Update Twitter from a HTA file

Wrapping up the basic script in a HTA file gives you the possibilities to provide your users or yourself with good looking, more userfriendly and more intuitive design.

A more userfriendly interface with HTA's

This is a again the same basic twitter script, but this time wrapped up in a HTA file. A HTA file is based on Internet Explorer, it is basically a webpage, but it has capabilities to incorpare VbScript code to perform specific script action om the users pc. Something a regular HTML file does not have. It's a nice and easy way to provide your user with a clear and simple looking user interface.

Open notepad and copy 'n paste the code below into a new txt file. To test the script go to line 101 and 102 in the script and there you will find the two constants you have to change into you own Twitter login information to get it working. Then save it as "UpdateTwitter.hta" (or whatever filename you want, as long as it end with .hta). Double click the script file and you will see the interface asking for your input. Type in your message and it appears on your Twitter. It has some basic validation and I kept the design low profile, otherwise it gets harder to understand the script code. The script is heavily commented, so start at the top and work you way down, then you will probably understand what´s going on.


     <title>VbScript Tweets - Update your Twitter status</title>


     <style type="text/css">
     /* General styles */
     body     {     
          margin: 0px;
          padding: 0px;

     /* format specific */     

     #header {
          border-bottom: 1px solid #000000;
          padding: 10px;
          font-family: verdana, arial, helvetica, sans-serif;
          font-size: 10px;

          padding: 20px;
          font-family: verdana, arial, helvetica, sans-serif;
          font-size: 20px;

     #inputboxed {
          font-family: verdana, arial, helvetica, sans-serif;
          font-size: 20px;
          font-weight: bold;
          background-color: #F3F3F3;

     #twitterresponse {
          padding: 10px;
          font-family: verdana, arial, helvetica, sans-serif;
          font-size: 10px;
          text-align: center;
     .vbtweetstd {
          vertical-align: text-top;
          font-family: verdana, arial, helvetica, sans-serif;
          font-size: 10px;
     .buttonclass {
          font-family: verdana, arial, helvetica, sans-serif;
          font-size: 12px;
          font-weight: bold;
          border: 1px solid #000000;
          width: 200px;
          height: 50px;
          background-color: c6e2ee;


<script language="vbscript">

     OPTION EXPLICIT 'Be sure you have al your variables declared.

     ' The below values are the only values you have to change to get this script working

     Const conUsername = "Twitter username" ' Your Twitter username
     Const conPassword = "Twitter password" ' Your Twitter password

     Sub DoTwitter
          ' This sub is built in to perform some checks
          Dim strTwitterMessage
          strTwitterMessage = TxtTwitter.value
          If Len(strTwitterMessage) > 140 Then
               ' In case you message is too long, it drops a warning..
               twitterresponse.InnerHTML = "Your message is more then 140 characters, Twitter will post it, "& _
                    "but your followers have to click on your message to read the entire story. " & _
                    "It is recommended to make your message shorter then 140 characters...<br /><br /><br />" & _
                    "To continue anyway, click yes, otherwise change your message and click the &quot;Send to twitter&quot; button again.<br /><br />" & _
                    "<input class=""buttonclass"" type=""button"" value=""Yes... (please continue!)"" OnClick=""SendToTwitter"" />"
          ElseIf Len(strTwitterMessage) = 0 Then
               ' In case you did not enter a message at all...
               twitterresponse.InnerHTML = "You did not enter a message..."
               ' Otherwise, call the sub to post your message to Twitter
          End If

     End Sub

     Sub SendToTwitter     

          ' This is the function wicht does all the work.
          ' It uses XMLHTTP to post your message to Twitter..
          Dim objHTTP , strSendToTwitter
          Set objHTTP = CreateObject("Microsoft.XMLHTTP")
               objHTTP.open "POST", "http://twitter.com/statuses/update.xml", false, conUsername, conPassword
               objHTTP.send "status=" & TxtTwitter.value & " test.."          
               ' The function stores the Twitter response to a variable so you can use that response later
               strSendToTwitter = objHTTP.responseText
          Set objHTTP = nothing 'Release the object
          ' Now do something with the result. It's not
          ' necessary for the script to work, but it is nice to use it later
     End Sub

     Sub ReadXMLFile(strXML)
          ' This sub make the XML response readable.
          ' it formats the XML into a table layout, but you can
          ' easily change this to a more advanced solution like
          ' a logfile or something.
          Dim xmlDoc
          Set xmlDoc = CreateObject("Microsoft.XMLDOM")
          xmlDoc.loadXML strXML
          Dim strMessage
          strMessage = "<table border=""0"">"
          ' Perform a loop through all the childnodes
          Dim x
          For Each x In xmlDoc.documentElement.childNodes

               strMessage = strMessage & "<tr><td class=""vbtweetstd""><b>" & x.nodename & "</b></td><td class=""vbtweetstd"">: </td><td class=""vbtweetstd"">" & x.text & "&nbsp;</td></tr>" & VbCrlf
          Set xmlDoc = nothing ' Release the object
          ' Close the table
          strMessage = strMessage & "</table>"
          ' Drop the formatted HTML table into the message area..
          ' It is still some pretty rough output a user can't do anything with,
          ' but I don't want to make the code too long..
          twitterresponse.InnerHTML = "<strong>Twitter responded:</strong><br /><br /><br />" & strMessage
     End Sub

     Sub CountCharacters
          ' Show how many characters are used so far.
          ' This sub is called each time you release a keyboard button.
          twitterresponse.InnerHTML = Len(TxtTwitter.value) & " characters used.."
     End Sub



<!-- Start Header, modify or delete as you like! -->
<div id="header">

     <strong>SITEJUNCTION</strong><br />
     <small>TWITTER STATUS UPDATE</small>
     <br /><br />
     With this hta script you can update your Twitter status <br />
     more info: <a href="http://sitejunction.awardspace.com/vbscript_tweets/hta_script/" target="_blank">http://sitejunction.awardspace.com/vbscript_tweets/hta_script/</a>
<!-- End Header -->

<!-- Start Twitter message box-->
<div id="twitterboxarea">

     Enter your Twitter message in the box below;
     <br /><br />
     <input type="text" id="inputboxed" size="50" name="TxtTwitter" OnKeyUp="CountCharacters" maxlength="400" /><br /><br />
     <input class="buttonclass" type="button" onclick="DoTwitter" value="Send to Twitter.." /><br /><br />
<!-- End Twitter message box-->

<!-- Messages appear here-->
<div id="twitterresponse"></div>
<!-- End of Messages box -->


You can also download the script directly here. Right click with you mouse on the download link, choose "save as" and save the file. To edit the file you can open it using notepad of your favorite scrpit editor.

Do you have got problems with this script? You can send me an e-mail.

A quick question answered
I received a couple of questions about changing the size of the HTA.

Paste this into the scriptcode;

<script type="text/javascript">
     window.resizeTo(715,535); // resize the current window

Where 715 is the width and 535 is the height.. Paste it in the script as shown below;