Shoutbox

Whats wrong with my 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: Whats wrong with my script? (/showthread.php?tid=87056)

Whats wrong with my script? by Bluestar on 11-03-2008 at 07:41 PM

Hello!

I would like to to write a little script for my PC locker application, that would be able to change the Windows Live Messenger's state to "Away" when the PC is locked by the user. The program should read the lock value (is the pc locked or not atm) from a file, for example an "ini", but a plain text file would be good also (that plain-text is the way I've tried to write that script too).

So could someone help me please? (A)
Thats my (first) script, and as I said before the debugger says "fsObj isn't defined". :chrongue: The status change function is working anyway.




jscript code:
function OnEvent_Initialize(MessengerStart)
{
    addTimer();
    Old_Status = 0;
    isGone = false;
    locked = false;
   
    if (ReadLineFromFile('lockstatus.ini', 1) == "true")
    {
        // status changing code need to be here...
    }
       
    if (locked == false)
    {
        if (Messenger.MyStatus == STATUS_INVISIBLE)
            {
                return;
            }
           
            Old_Status = Messenger.MyStatus;
            Messenger.MyStatus = STATUS_AWAY
            isGone = true;   
    }   
}

function addTimer()
{
    MsgPlus.AddTimer("Lock",10000);
}

function OnEvent_Uninitialize(MessengerExit)
{
}

function ReadLineFromFile (file, line)
{
  var fileObj = fsObj.OpenTextFile(MsgPlus.ScriptFilesPath + '\\' + file, 1);
  var lineResult = fileObj.ReadAll();
  lineResult = lineResult.split('\r\n');
  fileObj.Close();
  return lineResult[line];
}


function OnEvent_Timer(timerID){

    if(timerID == "Lock")
    {
       
        if (locked == false)
        {
            if (Messenger.MyStatus == STATUS_INVISIBLE)
            {
                return;
            }
           
            Old_Status = Messenger.MyStatus;
            Messenger.MyStatus = STATUS_AWAY
            isGone = true;   
        }   

    }
   
    addTimer();
}

RE: Whats wrong with my script? (debugger says fsObj isn't defined) :( by ArkaneArkade on 11-03-2008 at 07:46 PM

The problem is that fsobj isn't defined.

jscript code:
var fsObj = new ActiveXObject("Scripting.FileSystemObject");
If you define that at the top of the script, it'll be defined for when you call it in ReadLinefromFile().
RE: Whats wrong with my script? (debugger says fsObj isn't defined) :( by Bluestar on 11-03-2008 at 08:21 PM

Thank you, now it works like a charm! ;) :)


RE: Whats wrong with my script? (debugger says fsObj isn't defined) :( by Bluestar on 11-17-2008 at 04:33 PM

Umm... theres another problem now.
The actual script I've made (based on the original reply there):


jscript code:
var fsObj = new ActiveXObject("Scripting.FileSystemObject");
var statuschanged = false;

function OnEvent_Initialize(MessengerStart)
{
    Old_Status = 0;
    isGone = false;
    addTimer();
}

function addTimer()
{
    MsgPlus.AddTimer("Lock",10000);
}

function OnEvent_Uninitialize(MessengerExit)
{
}

function ReadLineFromFile (file, line)
{
  var fileObj = fsObj.OpenTextFile(MsgPlus.ScriptFilesPath + '\\' + file, 1);
  var lineResult = fileObj.ReadAll();
  lineResult = lineResult.split('\r\n');
  fileObj.Close();
  return lineResult[line];
}

function OverwriteFile (file, content) {
  var fileObj = fsObj.OpenTextFile(MsgPlus.ScriptFilesPath + '\\' + file, 2, 0);
  fileObj.Write(content);
  fileObj.Close();
}

function OnEvent_Timer(timerID){

    if(timerID == "Lock")
    {
       
        if (ReadLineFromFile('lockstatus.ini', 0) == "locked")
        {
            if (Messenger.MyStatus == STATUS_INVISIBLE)
            {
                addTimer();
                return;
            }
           
            Old_Status = Messenger.MyStatus;
            Messenger.MyStatus = STATUS_AWAY;
            isGone = true;   
        }   
       
        if (ReadLineFromFile('lockstatus.ini', 0) == "unlocked")
        {
            if (Messenger.MyStatus == STATUS_INVISIBLE)
            {
                addTimer();
                return;
            }
           
                Messenger.MyStatus = Old_Status;
                isGone = true;   
                OverwriteFile('lockstatus.ini', 'off');
        }

    }
   
    addTimer();
}

The function: the script does exactly the job what it has to (on timer event it reads a file, if it's content is locked, the WLM changes its state to "Away", and if the file's content is unlocked the WLM script changes the state to the original (what it was before), and writes off to the file (lockstatus.ini).

The problem: After some time (sometimes at login to WLM), the script stops running with the following error(s):

...
Calling function: OnEvent_Initialize
Error: unknown (code: -2147418113)
       File: Lock That PC Script.js. Line: 61.

The Line #61 is:

jscript code:
          Messenger.MyStatus = Old_Status;

What may be the problem? *-) Please help me to solve it! (A)
Anyway I don't really know how to use timers. :$ As I know, if I want timer to run, I have to call the addTimer() function again and again in the code. Am I right?
RE: Whats wrong with my script? (debugger says fsObj isn't defined) :( by foaly on 11-17-2008 at 04:37 PM

you should declare Old_Status outside any function... this way it can cause trouble...


RE: Whats wrong with my script? (debugger says fsObj isn't defined) :( by Bluestar on 11-17-2008 at 04:43 PM

Ah, so you mean there should be a line like

jscript code:
var Old_Status = 0;
before the OnEvent_Initialize function if I'm right. :)
Nice idea, thank you! Hope that helps, I'm goin' to test it.
RE: Whats wrong with my script? (debugger says fsObj isn't defined) :( by Bluestar on 11-18-2008 at 01:51 PM

Damn it...
after some time of running, now it stops with the next lines:

Error: uknown (code: -2147418113)
       File: Lock That PC Script.js. Line: 50.

Line #50 is:

jscript code:
            Messenger.MyStatus = STATUS_AWAY;

Any idea?
Maybe I don't use the timer function the right way? (are there any examples available somewhere?) *-)
RE: Whats wrong with my script? by matty on 11-18-2008 at 02:24 PM

Post the entire script with all of the up to date changes and we can take a look.


RE: Whats wrong with my script? by Bluestar on 11-20-2008 at 11:21 PM

Thats the entire script which I've posted above. :)
But I've changed some things atm, so here is the actual one:


jscript code:
var fsObj = new ActiveXObject("Scripting.FileSystemObject");
var statuschanged = false;
var Old_Status = 0;

function OnEvent_Initialize(MessengerStart)
{
    Old_Status = 0;
    isGone = false;
    addTimer();
}

function addTimer()
{
    MsgPlus.AddTimer("Lock",10000);
}

function OnEvent_Uninitialize(MessengerExit)
{
}

function ReadLineFromFile (file, line)
{
  var fileObj = fsObj.OpenTextFile(MsgPlus.ScriptFilesPath + '\\' + file, 1);
  var lineResult = fileObj.ReadAll();
  lineResult = lineResult.split('\r\n');
  fileObj.Close();
  return lineResult[line];
}

function OverwriteFile (file, content) {
  var fileObj = fsObj.OpenTextFile(MsgPlus.ScriptFilesPath + '\\' + file, 2, 0);
  fileObj.Write(content);
  fileObj.Close();
}

function OnEvent_Timer(timerID)
{

    if(timerID == "Lock")
    {
        addTimer();
       
        if (ReadLineFromFile('lockstatus.ini', 0) == "locked")
        {
            if (Messenger.MyStatus == STATUS_INVISIBLE)
            {
                return;
            }
           
            else
           
            {
            Old_Status = Messenger.MyStatus;
            Messenger.MyStatus = STATUS_AWAY;
            isGone = true;   
            }
        }   
       
        if (ReadLineFromFile('lockstatus.ini', 0) == "unlocked")
        {
            if (Messenger.MyStatus == STATUS_INVISIBLE)
            {
                return;
            }
           
            else
           
            {
                Messenger.MyStatus = Old_Status;
                isGone = true;   
                OverwriteFile('lockstatus.ini', 'off');
            }
        }

    }
   
}



I've put the "else" lines after the if (Messenger.MyStatus == STATUS_INVISIBLE) in the code, that way theres no error atm. I've found that there were errors only while I was using WLM with "Invisible" mode, so I hope that problem is fixed for ever, but I'm not sure of it. *-)