What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Useful Snippets

Useful Snippets
Author: Message:
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
O.P. Useful Snippets
I'm quite bored at the moment so I decided to share a couple of functions that seem to help me out a bit and save time.

This first one is because I was too used to using Flash's trace command. I also like that you can prefix (and suffix) the traced output.
code:
function trace(message){
Debug.Trace("Trace: "+message)
}


This one I deliberatley wanted to be similar to the VB function msgbox. Obviously, I've not given options to set buttons (I only use it for warnings etc)
code:
function msgbox(title, message){
Interop.Call("User32.dll", "MessageBoxW", 0, message, title, 0);
}


I saw a script that shows the percentage of online users so I decided to make a "graphical" version and thought I'd share it :p
code:
var online;
var offline;
var percent;
var str;

function OnEvent_SigninReady(Email){
doIt(Email);
}

function OnEvent_ContactSignin(Email){
doIt(Email);
}

function OnEvent_ContactSignout(Email){
doIt(Email);
}

function OnEvent_Initialize(MessengerStart){
doIt(Messenger.MyEmail);
}

function doIt(Email){
str="----------------------------------------------------------------------------------------------------";
online=0;
offline=0;
var Contacts = Messenger.MyContacts;
var e = new Enumerator(Contacts);
    for(; !e.atEnd(); e.moveNext()) {
        var Contact = e.item();
        if(Contact.Status!=1&&Contact.Status!=0){
            online++;
        }
            offline++;
       
    }

percent = Math.round((online/offline)*100);
    for(var i=0;i<percent;i++){
        str=str.replace("-","|");
    }
    for(var a=0;a<=100;a++){
        str=str.replace("-","");
    }
    str=str+"  "+percent+"% Online";
Messenger.MyPersonalMessage = str;
Debug.Trace(Email +" caused PSM to update");
}

This might not be "the best way" of doing things (like most of my scripts), but it gets the job done ;)

EDIT: The red segment has been revised due to a SLIGHT error ;)

This post was edited on 10-10-2006 at 12:31 AM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
10-10-2006 12:04 AM
Profile PM Find Quote Report
deAd
Scripting Contest Winner
*****

Avatar

Posts: 1060
Reputation: 28
– / Male / Flag
Joined: Jan 2006
RE: Useful Snippets
http://shoutbox.menthix.net/showthread.php?tid=56747

Stickied post for this purpose...
Copy these functions in and then delete the thread?
10-10-2006 12:09 AM
Profile PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
O.P. RE: Useful Snippets
Didn't really think they were *important* enough to go in something like that, which seems to deal with slighlty more complex things... This just really makes it easier to call certain functions :p

EDIT: Spelling.. tut

This post was edited on 10-10-2006 at 12:14 AM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
10-10-2006 12:13 AM
Profile PM Find Quote Report
MicroWay
Senior Member
****

Avatar
Do you want me? Try to get me!

Posts: 571
Reputation: 16
37 / Male / Flag
Joined: Jul 2006
Status: Away
RE: Useful Snippets
quote:
Originally posted by SpunkyLoveMuff
...
    }
percent = Math.round((online/offline)*100);
    for(var i=0;i<percent;i++){
        str=str.replace("-","|");



Sorry, but I think Math is Wrong...

Perc of online people = (online/total number of contacts)*100

;)
10-10-2006 12:28 AM
Profile PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
O.P. RE: Useful Snippets
Thats an error I've just noticed. In my PSM script, offline IS the total number of contacts :p I'll edit original post and highlight it ;)
<Eljay> "Problems encountered: shit blew up" :zippy:
10-10-2006 12:30 AM
Profile PM Find Quote Report
MicroWay
Senior Member
****

Avatar
Do you want me? Try to get me!

Posts: 571
Reputation: 16
37 / Male / Flag
Joined: Jul 2006
Status: Away
RE: Useful Snippets
(Y) Now it's correct

Ohh.. And I edited it a little to display percentage of offline people too. :P I'll post it here:

code:
var online;
var offline;
var percent;
var str;

function OnEvent_SigninReady(Email){
doIt(Email);
}

function OnEvent_ContactSignin(Email){
doIt(Email);
}

function OnEvent_ContactSignout(Email){
doIt(Email);
}

function OnEvent_Initialize(MessengerStart){
doIt(Messenger.MyEmail);
}

function doIt(Email){
str="";
online=0;
offline=0;
var Contacts = Messenger.MyContacts;
var e = new Enumerator(Contacts);
for(; !e.atEnd(); e.moveNext()) {
var Contact = e.item();
if(Contact.Status!=1&&Contact.Status!=0){
online++;
}
offline++;

}
percent = Math.round((online/offline)*100);
for(var i=0;i<percent;i++){
str=str.replace("-","|");
}
for(var a=0;a<=100;a++){
str=str.replace("-","");
}

percent2 = Math.round(100 - percent);
for(var i=0;i<percent;i++){
str=str.replace("-","|");
}
for(var a=0;a<=100;a++){
str=str.replace("-","");
}
str=str+" "+percent+"% of contacts Online and "+percent2+"% of contacts offline" ;
Messenger.MyPersonalMessage = str;
Debug.Trace(Email +" caused PSM to update");
}


Thanks for this code :D
Sorry for coping it and editing it :$ 

This post was edited on 10-10-2006 at 05:54 AM by MicroWay.
10-10-2006 05:52 AM
Profile PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
O.P. RE: RE: Useful Snippets
quote:
Originally posted by MicroWay
Thanks for this code :D
Sorry for coping it and editing it :$ 


Well, thats the reason I posted it :p

EDIT: Think I spotted a mistake in your for loop. If the code works though...

This post was edited on 10-10-2006 at 10:09 AM by Spunky.
<Eljay> "Problems encountered: shit blew up" :zippy:
10-10-2006 10:07 AM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Useful Snippets
quote:
Originally posted by SpunkyLoveMuff
This might not be "the best way" of doing things, but it gets the job done ;)
I'll say... :p

I'll correct and comment everything so you know what you did wrong (or "not so good").
ok, here we go...

code:
var online;
var offline;
var percent;
var str;
All the variables you use are used inside 1 function only, so you don't need global variables.

code:
function OnEvent_Initialize(MessengerStart){
    doIt(Messenger.MyEmail);
}
Messenger.MyEmail will not be defined when Messenger is started for the first time. Since you don't further use the email (except for showing it in the debug window) this is fine. But all this should be taken in consideration and one must be very aware of this and be carefull using it.

code:
online=0;
offline=0;
var Contacts = Messenger.MyContacts;
var e = new Enumerator(Contacts);
for(; !e.atEnd(); e.moveNext()) {
    var Contact = e.item();
    if(Contact.Status!=1&&Contact.Status!=0){
        online++;
    }
    offline++;
}
* The variable name 'offline' is very confusing, you should have named it 'total'.
I know you use offline in your scripts as the total amount, but this is not a good practice to do and will cause errors (as shown in the first code you posted).

* Increasing 'offline' each time is useless. For the total amount of contacts use Messenger.MyContacts.Count
This also makes that you do not need the 'Contact' variable, you can use e.item directly

* Instead of doing two status checks, why don't you just check if the status is equal or higher than 2?

code:
for (var i=0;i<percent;i++) {
    str = str.replace("-","|");
}
for (var a=0;a<=100;a++) {
    str = str.replace("-","");
}
* Why defining another variable "a" when you already have a variable "i" which is used for counting.
* Instead of replacing every "-" with a null string "", you could simply take a substring out of the string.
* Instead of working with a string which has 100 "-"'s, you could simply have build the string with "|".
* If you want to use the replace method to replace all occurances of a certain string within another string, use the regular expression syntax of replace instead of making a loop.

----------------------------------------------------

quote:
Originally posted by MicroWay
Ohh.. And I edited it a little to display percentage of offline people too. :P I'll post it here:
code:
(...)
percent2 = Math.round(100 - percent);
for(var i=0;i<percent;i++){
    str=str.replace("-","|");
}
for(var a=0;a<=100;a++){
    str=str.replace("-","");
}
str=str+" "+percent+"% of contacts Online and "+percent2+"% of contacts offline";
(...)

* percent2 simply equals: (100-percent), since percent is already a rounded integer, you don't need to round it again
* All the graphic manipulation is quite useless, there are no "-" characters anymore in the string to replace.

So all those additions weren't needed. You only needed to do:
str=str+" "+percent+"% of contacts Online and "+(100-percent)+"% of contacts offline";

----------------------------------------------------

The fixed code (including offline percentage):
code:
function doIt(Email){
    var online = 0;
    var Contacts = Messenger.MyContacts;
    for (var e = new Enumerator(Contacts); !e.atEnd(); e.moveNext())
        if (e.item().Status >= 2) online++;
    var percent = Math.round(100*online/Contacts.Count);
    var str = "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||";
    Messenger.MyPersonalMessage = str.substring(0, percent)
                    + "  " + percent + "% Online // " + (100-percent) + "% Offline";
    Debug.Trace(Email + " caused PSM to update");
}

Notice how much smaller (and faster!) it is...

;);)

This post was edited on 10-21-2006 at 01:54 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
10-20-2006 11:03 PM
Profile PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
O.P. RE: Useful Snippets
* Spunky hangs head in shame ;)

Nah, thanks though. Some things are just because it's the way I'm used to working with them (such as using global variables as I don't know if I'm going to later add more functions that will reuse them etc). I've just been able to figure out the regular expression syntax so I'm planning on using that a lot more. I use a different letter for each loop that I make as I (personally) find it less confusing and it makes more sense (again, to me personally).

My original attitude when I started scripting was that if it worked, that was good enough. However, I find myself improving thanks to help people are posting. Things are actually sinking in. I like to help others out also, but obviously it would be better if I knew the best practices first :p

Cheers Cookie (Y) :p
<Eljay> "Problems encountered: shit blew up" :zippy:
10-21-2006 12:10 AM
Profile PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
RE: Useful Snippets
I also suggest to add them to www.mpscripts.net. I know the site isn't fully operational and stuff, but it's good to have a big database filled with snippets when the place opens.

I also added some snippets already :-)
[Image: 1-0.png]
             
10-21-2006 12:33 AM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On