Shoutbox

My first script. - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: My first script. (/showthread.php?tid=68965)

My first script. by devilz-fury on 11-29-2006 at 06:39 PM

This is a dead simple script. All it does is it reads a text file every 10 minutes, picks out a random non-blank line from that file and then sets that as your Personal Message on Messenger. You could use this script with IRC logs, a list of quotes, etc. It didn't take long to make.

Source Code
function updateMessage() {    FileSystem = new ActiveXObject("Scripting.FileSystemObject");    file = FileSystem.OpenTextFile("E:/Documents/testfile.txt", 1);    i = 0;    lines = new Array();    while(!file.AtEndOfStream) {        line = file.ReadLine();        if (line != '') {            lines[i] = line;        }        i++;    }    file.Close();    linenum = Math.floor(Math.random()*lines.length)-1;        Messenger.MyPersonalMessage = lines[linenum];}function OnEvent_Initialize(MessengerExit) { }function OnEvent_Uninitialize(MessengerExit) { }function OnEvent_Signin(email) {    if (email == Messenger.MyEmail) {        updateMessage();        MsgPlus.AddTimer('randomtext', 600000);    }}function OnEvent_Timer(timerId) {    if (timerId == 'randomtext') {        updateMessage();        MsgPlus.AddTimer('randomtext', 600000);    }}

Hopefully most of this script should be fairly self-explanatory. OnEvent_Signin is called when a user signs in. This function checks that it is the current user who has just signed in, and if so calls updateMessage() and schedules the next call for in 10 minutes time.

OnEvent_Timer gets called when a timer is triggered - in this case it'll get called after 10 minutes. The function calls updateMessage() and schedules the next call for in another 10 minutes time. updateMessage() opens a text file, reads the whole file putting it into an array and then selects a random line and sets it as the personal message.

Usage
To use it, go to Plus > Scripts and select "Create New". Enter a name for the script and paste the contents of the script above into it and save it. You'll have to sign out/sign in or restart Messenger to make it work. Make sure you've also changed the path to the text file which is E:/Documents/testfile.txt by default.


It'll overwrite your whole personal message. If you want it to appear as part of your personal message, concatenate the random line with something else. For example, you could change:

Messenger.MyPersonalMessage = lines[linenum];To:

Messenger.MyPersonalMessage = 'My Personal Message | '+lines[linenum]; If you want to change the update interval, change the value of 600000 (600000ms is 10 minutes).


RE: My first script. by andrey on 11-29-2006 at 06:58 PM

just added a bit of layout to your script:

code:
function updateMessage() {
   FileSystem = new ActiveXObject("Scripting.FileSystemObject");
   file = FileSystem.OpenTextFile("E:/Documents/testfile.txt", 1);
   i = 0;
   lines = new Array();
   while(!file.AtEndOfStream) {
      line = file.ReadLine();
      if (line != '') {
         lines[i] = line;
      }
      i++;
   }
   file.Close();
   linenum = Math.floor(Math.random()*lines.length)-1;
   Messenger.MyPersonalMessage = lines[linenum];
}

function OnEvent_Initialize(MessengerExit) {
}

function OnEvent_Uninitialize(MessengerExit) {
}

function OnEvent_Signin(email) {
   if (email == Messenger.MyEmail) {
      updateMessage();
      MsgPlus.AddTimer('randomtext', 600000);
   }
}

function OnEvent_Timer(timerId) {
   if (timerId == 'randomtext') {
      updateMessage();
      MsgPlus.AddTimer('randomtext', 600000);
   }
}

RE: My first script. by CookieRevised on 11-30-2006 at 01:45 AM

When you sign out while that script is running, your script goes *poof* kaput...


...


PS1: redundant code:
function OnEvent_Signin(email) {
     if (email == Messenger.MyEmail) {

email will always be Messenger.MyEmail btw


PS2: useless code: You don't need to include empty functions:
function OnEvent_Initialize(MessengerExit) {}
function OnEvent_Uninitialize(MessengerExit) {}


PS3: exercise code: try to optimize the updateMessage function like so:
1) get the filesize
2) get a random number from 1 to filesize
3) start reading from that offset until the next new line
4) use that next new line as your new message

tip: don't forget about methods like Skip()...


;)


RE: My first script. by Menthix on 11-30-2006 at 01:50 AM

quote:
Originally posted by devilz-fury
file = FileSystem.OpenTextFile("E:/Documents/testfile.txt", 1);
Somebody already made a post about this before, I'm 100% sure, but i guess he removed it. Anyway, hardlinking to a file like that will mean it will only work on your PC. By far most people don't even have an E: partition, let alone that they have that file there.