Shoutbox

Useful Snippets - 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: Useful Snippets (/showthread.php?tid=67154)

Useful Snippets by Spunky on 10-10-2006 at 12:04 AM

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 ;)
RE: Useful Snippets by deAd on 10-10-2006 at 12:09 AM

http://shoutbox.menthix.net/showthread.php?tid=56747

Stickied post for this purpose...
Copy these functions in and then delete the thread?


RE: Useful Snippets by Spunky on 10-10-2006 at 12:13 AM

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


RE: Useful Snippets by MicroWay on 10-10-2006 at 12:28 AM

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

;)
RE: Useful Snippets by Spunky on 10-10-2006 at 12:30 AM

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 ;)


RE: Useful Snippets by MicroWay on 10-10-2006 at 05:52 AM

(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 :$ 

RE: RE: Useful Snippets by Spunky on 10-10-2006 at 10:07 AM

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...
RE: Useful Snippets by CookieRevised on 10-20-2006 at 11:03 PM

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...

;);)
RE: Useful Snippets by Spunky on 10-21-2006 at 12:10 AM

* 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


RE: Useful Snippets by Ezra on 10-21-2006 at 12:33 AM

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 :-)