Shoutbox

if = true and also executing else - 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: if = true and also executing else (/showthread.php?tid=95442)

if = true and also executing else by dfnkt on 09-10-2010 at 08:47 PM

So I'm really new to jscript and looking for some input. I've got a script i'm working on that in the end will both lock the computer and set the status to away and the PSM to "I'm not here right now" or something of the sort.

I haven't exactly worked out how I want to do this yet but I'm already coding it. I'm not sure if I can tie this script to a shortcut key or if I can add a button on the WLM interface or create another menu entry on the Messenger Plus Live menu...

Anyway, here is the issue i'm having with my dialog:

I have the simple interface XML created and saved using UTF-16 encoding (little endian).

code:
var Wnd = MsgPlus.CreateWnd("lock.xml", "WndLock")

that fires up the window (for right now as part of the OnEvent_Initialize function)

The dialog is a simple "Lock Computer?" with a 'yes' and 'no' button. Next I have a function setup to handle the clicking of 'yes' and 'no' that looks like this:

code:
function OnWndLockEvent_CtrlClicked(Wnd, ControlId)
{
        if(ControlId == "BtnNo")
            Wnd.Close(7);
       
        else(ControlId == "BtnYes")
            Interop.Call("User32.dll", "LockWorkStation", "", "N");
}

Separately these two statements work just fine. However when you combine them using if/else you get some unwanted behavoir. Run the script and you can click "yes" and it locks the computer, if you click "No" the dialog closes but the computer also locks.

How do you prevent it from running the if and else statements when you click No?


Changing the else to an if on the btnyes section fixed this..

Is that really considered good to to say:

If(blah)
   blah

If(blah2)
  blah

I've just always been accustomed to doing if/then/else/elseif type of a flow.
RE: if = true and also executing else by CookieRevised on 09-10-2010 at 10:47 PM

For starters remove the ; at the end of Wnd.Close(7) if you want to use the short form.

If you write IF THEN ELSE code in that way, thus seperated over multiple lines, remember that all those lines are actually always seen as 1 command line. But ; donates the end of a command line. So, it is a small syntax error, which can confuse the interpreter in some cases. It isn't realy wrong wrong in this case though since the interpreter is smart enough to interpret it properly in this particular case, but just be aware of it that it shouldn't be there.

But the real fault is in your Else syntax.
As you said so yourself, it should be IF THEN ELSE or IF THEN ELSEIF. But what you did is a IF THEN ELSEIF but without the IF.

Even in a language like Visual Basic (I assume you're used to that seeing that you said "if/then/else/elseif"), you need an IF codeword if you want to compare something else on the Else line:

code:
If ControlId = "BtnNo" Then
     CloseWindow Wnd, 7
ElseIF ControlId = "BtnYes" Then
     LockWorkStation "", "N"
End If

Thus what you actually wrote is (in Visual Basic terms):
vb code:
Function OnWndLockEvent_CtrlClicked(Wnd, ControlId)
     If ControlId = "BtnNo" Then
          CloseWindow Wnd, 7
     Else
          ControlId = "BtnYes"
     End If
     LockWorkStation "", "N"
End Function
Or  (in J(ava)Script Terms):
js code:
function OnWndLockEvent_CtrlClicked(Wnd, ControlId)
{
        if (ControlId == "BtnNo") {
                 Wnd.Close(7);
        } else {
                 ControlId == "BtnYes";
        }
        Interop.Call("User32.dll", "LockWorkStation", "", "N");
}

Hence your unexpected (but logical) behaviour.

Hence also my suggestion to always use brackets { } and not trying to shorten stuff, especially when debugging.
(this is a good example and exactly the reason why I did not agree with Eljay's comment here.)

Also, J(ava)Script doesn't have an ELSEIF statement. If you want to do an ELSEIF you actually must do another IF inside the first ELSE.

Thus either (in short form):
js code:
if(ControlId == "BtnNo")
     Wnd.Close(7)     
else if(ControlId == "BtnYes")
     Interop.Call("User32.dll", "LockWorkStation", "", "N");
Or either (in longer form):
js code:
if(ControlId == "BtnNo") {
     Wnd.Close(7)
} else if(ControlId == "BtnYes") {
     Interop.Call("User32.dll", "LockWorkStation", "", "N")
}
which is a shorter form of:
js code:
if(ControlId == "BtnNo") {
     Wnd.Close(7)
} else {
     if(ControlId == "BtnYes") {
          Interop.Call("User32.dll", "LockWorkStation", "", "N")
     }
}







quote:
Originally posted by dfnkt
Is that really considered good to to say:

If(blah)
   blah
If(blah2)
  blah

I've just always been accustomed to doing if/then/else/elseif type of a flow.
For stuff like this, where there are only 2 things to compare the ControlId to, it depends on what you're used to. All forms are considered good, as long as you don't make any errors in the syntax of course :p

Just remember that using multiple IF's is 'slower' than using the IF ELSE(IF) structure. This because in the first, all IF's will be executed and compared for a possible match, while in the later only the IF's and ELSE's up to a match will be executed.

Either way, if there are many more things to compare ControlId to, then you could consider using a switch structure (equivalent of the Select Case statement in Visual Basic).
RE: if = true and also executing else by dfnkt on 09-11-2010 at 04:04 AM

Thanks for all your info cookie. I'll double check my code and work on refactoring it. I was wondering if jscript supported something like case.

What about the ability of scripts to add a button on the WLM contacts list or the ability of them to add another menu item on the messenger plus menu that could kick this thing off?

I'm trying to work out what makes the most sense for making this work, it needs to be something that makes sense from a usability standpoint.


RE: if = true and also executing else by Matti on 09-11-2010 at 09:19 AM

quote:
Originally posted by dfnkt
What about the ability of scripts to add a button on the WLM contacts list or the ability of them to add another menu item on the messenger plus menu that could kick this thing off?
Scripts can only add menu items to the Plus! Scripts menu using OnGetScriptMenu or with the <ScriptMenu> block in your ScriptInfo.xml. With more advanced coding, you might be able to add a menu item to the right-click menu of the task bar button or system tray icon, but that's about as far as you can go.

Adding a button to the Messenger user interface itself is considered nearly impossible as it requires mad skills with memory hacks and UIB files - something which you can't do in a scripting environment. Even if they could technically allow Plus! scripts to add buttons to the Messenger UI, they wouldn't do it because it could easily be abused by malicious scripts and make Messenger look (even more) bloated. The scripts menu is there to centralize all scripts functionality in one place, making it easier for both users and developers to work with.

You could however assign a system-wide hotkey to fire a script's function using the Windows API. I made a class for this sometime ago, you can find it in Countdown Live (GlobalHotkey.js) or Screenshot Sender 5 (Classes\_global_hotkey.js). Just copy that script file over to your script and use the class' methods wherever you need them. I see that there's little documentation about it, so if you're planning to use that class but you don't know how, just PM me and I'll help you out. ;)
RE: RE: if = true and also executing else by dfnkt on 09-11-2010 at 01:48 PM

quote:
Originally posted by Matti
You could however assign a system-wide hotkey to fire a script's function using the Windows API.

Well, I could see about assigning the script to be initialized when you hit Windows Key + L which is also a call to user32.dll and the LockWorkStation function. I am thinking now that a menu item on the Plus! menu makes the most sense.

Currently I have the script utilizing the messengerStart event for testing, it prompts the user with a dialog and properly handles the yes or no button events. On yes the script closes the window created by the script, locks the computer, sets your presence to away and sets a personal status message of "I'm not here right now."

I thought I remembered seeing something about taking user input? In the end if the user clicks "lock" or whatever it might be on the Plus! menu i'd a small dialog to come up and have them input a custom away message. On no it just closes the dialog.

I've also got a small to-do for storing a pre-existing status message and restoring it and the user presence upon return (if possible).
RE: if = true and also executing else by Eljay on 09-11-2010 at 02:04 PM

I made a small script called OnLock that detects when Windows is locked/unlocked and changes status.
Feel free to use it as a basis or steal bits of it for whatever it is you want to do :P