Shoutbox

[Help]Sending a ChatWnd message - 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: [Help]Sending a ChatWnd message (/showthread.php?tid=98061)

[Help]Sending a ChatWnd message by PedroGabriel on 07-31-2011 at 08:33 PM

Hello,
can somebody tell me if theres some way to send a message without using the OnEvent_ChatWndReceiveMessage

Me and my friend was trying to know how for some time but thats hard...

ChatWnd.SendMessage(str);
only works if you know the ChatWnd,but how to send from an event like:
function OnEvent_Timer(Timer)

We can send a message from another function or only from the
OnEvent_ChatWndReceiveMessage
?

thanks for attention


RE: [Help]Sending a ChatWnd message by Spunky on 07-31-2011 at 09:09 PM

In a situation like that, I tend to store the ChatWnd Ogbject as a global variable from a different function so I can access it later


RE: [Help]Sending a ChatWnd message by PedroGabriel on 07-31-2011 at 09:18 PM

I already have tried to do look:

js code:
var window = null
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MsgKind){
    window = ChatWnd;
    Debug.Trace(window);
}

return:

Function: OnEvent_ChatWndReceiveMessage
Error: Type mismatch (code: -2146828275)
An error occurred in function OnEvent_ChatWndReceiveMessage.
Code: -2147352567

Can somebody give me a tip?
RE: [Help]Sending a ChatWnd message by Matti on 07-31-2011 at 10:07 PM

Debug.Trace needs a string as parameter but you're passing in a ChatWnd object. This cannot be implicitly converted to a string and thus JScript throws a type mismatch error.

When debugging, I usually output the handle of a window to check if the variable contains a window:

js code:
Debug.Trace(window.Handle)
You might actually want to use this handle to make a more versatile window storage. Also, by using a OnEvent_ChatWndSendMessage you can properly distinguish sent messages from received messages using a similar storage.
js code:
// Create containers to
var ChatWnds = {}, ChatWndIsReceivedMessage = {};

// Store the chat window object in our container when it is opened
function OnEvent_ChatWndCreated(ChatWnd) {
    ChatWnds[ChatWnd.Handle] = ChatWnd;
    ChatWndIsReceivedMessage[ChatWnd.Handle] = true;
}

// Remove the object from the containers if the window is closed
function OnEvent_ChatWndDestroyed(ChatWnd) {
    delete ChatWnds[ChatWnd.Handle];
    delete ChatWndIsReceivedMessage[ChatWnd.Handle];
}

// If we send a message, mark it in the container
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
    ChatWndIsReceivedMessage[ChatWnd.Handle] = false;
}

// Create a timer when a message is received
function OnEvent_ChatWndReceiveMessage(ChatWnd, Origin, Message, MessageKind) {
    if (ChatWndIsReceivedMessage[ChatWnd.Handle]) {
        // If this block is reached, the message has to be
        // a message received from a contact

        // Set a timer and concatenate the handle in the timer ID
        MsgPlus.AddTimer("DoSomethingAwesome:" + ChatWnd.Handle, 100);
    }
    // Mark as received in the container
    ChatWndIsReceivedMessage[ChatWnd.Handle] = true;
}

// Handle the timers
function OnEvent_Timer(TimerId) {
    if (TimerId.indexOf("DoSomethingAwesome:") === 0) {
        // Get the ChatWnd handle out of the TimerId
        var ChatWndHandle = TimerId.substr("DoSomethingAwesome:".length);
        // Get the ChatWnd object associated with this handle
        var ChatWnd = ChatWnds[ChatWndHandle];
        // Do something awesome here
    }
}
Derived from matty's reply to Reply to any message.

One thing to note: you should generate the containers at OnEvent_Initialize to account for the case where the script is started while the current is already signed in (e.g. when it's just installed or activated). Simply enumerate Messenger.CurrentChats and add them to the objects.