What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Saving variables ActiveXObject & shell

Saving variables ActiveXObject & shell
Author: Message:
PedroGabriel
Junior Member
**


Posts: 22
– / – / Flag
Joined: Jun 2010
O.P. Huh?  Saving variables ActiveXObject & shell
I'm making a script
but the var's don't save, If I relog and return my vars desapear...

how to make vars don't reset?


I'm trying...

my code:

code:
var score=0
function scorewrite(score){
    var Shell = new ActiveXObject("WScript.Shell");
    var ValRegPath = MsgPlus.ScriptRegPath + score;
    Shell.RegWrite(ValRegPath, 0);
}
function scoreread(Score){
    var score = Shell.RegRead(ValRegPath);
    var score = Shell.RegRead(MsgPlus.ScriptRegPath + score);
}


function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, rMessage, MsgKind){
    if (rMessage=='+1'){score+=1; scorewrite(score);}
}
function OnEvent_Timer(Timer){
    if (Timer=='rato'){
        Messenger.MyPersonalMessage=score
        MsgPlus.AddTimer('rato',1000)
    }
}

function OnEvent_Initialize(MessengerStart){
MsgPlus.AddTimer('rato',1000)
score = scoreread(score);
}
function OnEvent_SignIn(){score=scoreread(score);}

function OnEvent_Uninitialize(MessengerExit){scorewrite(score);}  

with my code

when you say +1
add +1 to score and show on the psm

if I leave from the Messenger and return

when I say +1 nothing happend...

I really want save var's to close msn and don't lose the values


Sorry my bad inglish...

Thanks!
06-19-2010 05:48 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Saving variables ActiveXObject & shell
It appears that your registry path is faulty. From what I understand, you want to read and write the key with the following path:
code:
HKEY_CURRENT_USER\Software\Patchou\Messenger Plus! Live\GlobalSettings\Scripts\My Script\score
Now look at what you wrote:
Javascript code:
1. function scorewrite(score){
2.    var Shell = new ActiveXObject("WScript.Shell");
3.    var ValRegPath = MsgPlus.ScriptRegPath + score;
4.    Shell.RegWrite(ValRegPath, 0);
5. }

On line 3 you are trying to build the key path, however you are doing this by concatenating the script's registry path with the value of "score". Thus, you get key paths such as:
code:
HKEY_CURRENT_USER\Software\Patchou\Messenger Plus! Live\GlobalSettings\Scripts\My Script\0
HKEY_CURRENT_USER\Software\Patchou\Messenger Plus! Live\GlobalSettings\Scripts\My Script\1
HKEY_CURRENT_USER\Software\Patchou\Messenger Plus! Live\GlobalSettings\Scripts\My Script\2
...
Since these are all different keys, you are reading from and writing to different keys. No wonder you get different results!

For more details about variables and strings, I'd suggest learning about the basic rules and concepts of JScript. There are very good tutorials on the internet, for example W3School's JavaScript tutorials.

Some other important remarks:
  • You are defining Shell as a local variable in scorewrite(), but you are trying to access it from scoreread() as well. This won't work, as Shell doesn't exist in the function scope of scoreread(). A solution would be to make Shell a global variable by defining it outside any function, making it accessible for all functions in your script.
  • In OnEvent_Initialize(), you are trying to get a return value from scoreread() and store this in "score". However, scoreread() doesn't have a return statement and will return just void, disposing any value previously stored in "score".
  • The timer will still continue to run after the user has signed out in which case it will fail to set a personal message. You should either cancel the timer in the sign out event or check whether the user is still logged in every time the timer event is fired.
  • The timer interval is way to low to be used safely. The Messenger protocol allows you to change your status info (name, message, status and display picture) maximum 10 times per minute. By changing the message every second, the risk of passing that limit is very real. I'd suggest increasing the interval to 6 to 10 seconds.

I did my best to "fix" your code and added comments explaining how everything is done. Please spend some time studying how this works and learn from it to write better code yourself. :)
Javascript code:
// Global variables
var Score = 0;
var ScorePath = '';
var Shell = new ActiveXObject('WScript.Shell');
 
/* READ/WRITE SCORE */
function ReadScore(){
    // Check if registry path is set
    if(ScorePath){
        // Read score to global variable
        Score = Shell.RegRead(ScorePath);
    }
    // Return current score
    return Score;
}
function WriteScore(){
    // Check if registry path is set
    if(ScorePath){
        // Store score in registry
        Shell.RegWrite(ScorePath, Score);
    }
}
 
/* MAIN EVENTS */
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, rMessage, MsgKind){
    if (rMessage == '+1'){
        // Increase score by one
        Score++;
        // Write score
        ScoreWrite();
    }
}
function OnEvent_Timer(Timer){
    if (Timer == 'rato'){
        // Set personal message
        Messenger.MyPersonalMessage = 'Score: ' + score;
        // Recreate timer
        MsgPlus.AddTimer('rato', 10000);
    }
}
 
/* STARTUP EVENTS */
function OnEvent_Initialize(MessengerStart){
    // Call sign in event if already logged in
    if(Messenger.MyStatus > 0) OnEvent_SignInReady(Messenger.MyEmail);
}
function OnEvent_SigninReady(Email){
    // Set score registry path
    ScorePath = MsgPlus.ScriptRegPath + '\\Score';
    // Start timer
    MsgPlus.AddTimer('rato', 10000);
}
 
/* SHUTDOWN EVENTS */
function OnEvent_Signout(Email){
    // Stop timer
    MsgPlus.CancelTimer('rato');
    // Write score
    ScoreWrite();
    // Reset globals
    Score = 0;
    ScorePath = '';
}

Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
06-19-2010 06:30 PM
Profile E-Mail PM Web Find Quote Report
PedroGabriel
Junior Member
**


Posts: 22
– / – / Flag
Joined: Jun 2010
O.P. RE: Saving variables ActiveXObject & shell
Thank you for your help, I managed to learn a lot.
eg  the Messenger can only change the psm 10 times per minute

Thanks you!

i'm testing your code and noticed that he has not done exactly what I wanted ...


when I left and return to the Messenger
I write "+1" but the score does not increase ...

is this the only error that has ...
(I can not fix this error, is hard...)

thanks again
06-19-2010 08:30 PM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: RE: Saving variables ActiveXObject & shell
Oops, small typo. :P
Javascript code:
function OnEvent_Timer(Timer){
    if (Timer == 'rato'){
        // Set personal message
        Messenger.MyPersonalMessage = 'Score: ' + Score;        // Recreate timer
        MsgPlus.AddTimer('rato', 10000);
    }
}

The old name "score" should have been replaced by "Score". JScript is case sensitive when it comes to variable and function names. ;)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
06-20-2010 07:51 AM
Profile E-Mail PM Web Find Quote Report
PedroGabriel
Junior Member
**


Posts: 22
– / – / Flag
Joined: Jun 2010
O.P. RE: Saving variables ActiveXObject & shell
oh Thanks for your help, I will try

EDIT:
the code works...

but when I leave from the MSN and return the Score var reset

why?

the code have this line:

code:
function OnEvent_SigninReady(Email){
    // Set score registry path
    ScorePath = MsgPlus.ScriptRegPath + '\\Score';
    // Start timer
    MsgPlus.AddTimer('rato', 10000);
}

I really don't understand why the code don't return the var when Messenger start...

thank you again!

This post was edited on 06-20-2010 at 08:12 AM by PedroGabriel.
06-20-2010 07:56 AM
Profile E-Mail PM Find Quote Report
whiz
Senior Member
****


Posts: 568
Reputation: 8
– / – / Flag
Joined: Nov 2008
RE: Saving variables ActiveXObject & shell
The reason for this is because when you sign in to Messenger, the score variable is defined as 0, but it isn't actually set from the registry each time, so it will just continue from the beginning each time.

Change the sign-in function to this:
Javascript code:
function OnEvent_SigninReady(Email){
    // Set score registry path
    ScorePath = MsgPlus.ScriptRegPath + '\\Score';
    // Set score variable    Score = ReadScore();    // Start timer
    MsgPlus.AddTimer('rato', 10000);
}

06-20-2010 10:00 AM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Saving variables ActiveXObject & shell
Dammit, can't believe I missed that. :P
Oh, and you don't really need to assign the return value of ReadScore() to Score, just call it and it'll set Score itself. ;)
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
06-20-2010 02:07 PM
Profile E-Mail 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