What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Release] Capitals and full stops(Updated!)V1.1

Pages: (2): « First « 1 [ 2 ] Last »
[Release] Capitals and full stops(Updated!)V1.1
Author: Message:
Baggins
Full Member
***

Avatar
B000ALFAZO

Posts: 387
Reputation: 13
29 / Male / Flag
Joined: Oct 2006
RE: [Release] Capitals and full stops(Updated!)V1.1
:$oh:$
10-20-2006 10:45 PM
Profile E-Mail PM Web Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: [Release] Capitals and full stops(Updated!)V1.1
quote:
Originally posted by Zahid™
Already a script that does this: http://www.msgpluslive.net/scripts/browse/index.php?act=view&id=35

I don't call that a script tbh. It consist of only 1 line, doesn't check for anything at all. It will screw up commands, etc, etc. Never should have been put in the database IMO. Even JimboDude's first draft of his first script is better than that.




JimboDude

I'll comment on various things so you know where things can be improved and why, so you can apply those things in your future scripts too.

As further reference I suggest to read the Official JScript 5.6 reference in the msdn library and/or download the Official Scripting Documentation from Windows.

Terms in italic in this post can be found in those references, look them up ;)


Anyways:

code:
if (Message.substr((Message.length)-('?'.length)) == '?' || Message.substr((Message.length)-('!'.length)) == '!' || Message.substr((Message.length)-(':'.length)) == ':' || Message.substr((Message.length)-('.'.length)) == '.' || Message.substr((Message.length)-(';'.length)) == ';' || Message.substr((Message.length)-(','.length)) == ',') {
You can simplify all that....

No need for calculating the length of "?", "!", etc... it's always 1 anyways
thus:
code:
Message.substr((Message.length)-1)
"something-1" can be written as "--something", thus:
code:
Message.substr(--Message.length)
Don't use substr to grab 1 character inside a string, use charAt instead:
code:
Message.charAt(--Message.length)


Instead of grabbing the last character each time again, assign the last character to a variable and compare that variable with whatever you need to compare it with:
code:
var lastChar = Message.charAt(--Message.length);
if (lastChar == '?' || lastChar == '!' || lastChar == ':' || lastChar == '.' lastChar == ';' || lastChar == ',') {
Since you compare that same last character again and again with another character, you can turn this all around and check if that last character can be found into the string "?!:.;,". If so, you have a match if not you don't have a match. Use indexOf for this:
code:
if ("?!:.;,".indexOf(Message.charAt(--Message.length)) != -1) {

So now
code:
if (Message.substr((Message.length)-('?'.length)) == '?' || Message.substr((Message.length)-('!'.length)) == '!' || Message.substr((Message.length)-(':'.length)) == ':' || Message.substr((Message.length)-('.'.length)) == '.' || Message.substr((Message.length)-(';'.length)) == ';' || Message.substr((Message.length)-(','.length)) == ',') {
is reduced to:
code:
if ("?!:.;,".indexOf(Message.charAt(--Message.length)) != -1) {
And if you know regular expressions this can be simplified even more....:
code:
if (/[?!:.;,]$/.test(Message)) {



;)


Other stuff:
- remove that "Message = null" line. It is not needed. And luckally it is not executed either as that will otherwise produce an error. See this post for more info (and of course the Plus! Scripting Documentation).

- the case where (caps=="on" && dot=="on") doesn't check for already existing dots, commas, question marks, etc

Suggestions:
- Instead of having 4 IF THEN ELSE structures, split them up and examine exactly what each function does with the sentence in what case. You'll find that you will only need 2 IF THEN ELSE structures since you will only actually do something when a setting is on, not when it is off. You also do not need the ELSE case at all.

Result:
highlight to see it
code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
        var firstChar = Message.charAt(0);
        var restOfMessage = Message.substr(1);
        if (caps == "on") firstChar = firstChar.toUpperCase();
        if (dot == "on" && /[?!:.;,]$/.test(Message) == false) restOfMessage += ".";
        return firstChar + restOfMessage;
}


Bugs still to fix:
- You need to check if the message is not a command (messages beginning with "/" or even "!").
- Check individual lines (you could do this by splitting your message up into an array using the method split with delimiter "\n")
- If you define an ActiveX object, make sure it is always defined. Your code contains several occasion where you use an ActiveX object (WSH), but which isn't defined since you have placed it inside an IF THEN ELSE structure (see OnEvent_MenuClicked function).


More things you can make better:
code:
  if(MenuId=="caps"){
(...)
  if(MenuId=="dots"){
(...)
  if(MenuId=="about"){
(...)
  if(MenuId=="config"){
(...)
For something like this you better use the switch statement.

code:
if(caps=="on") {
      caps="off";
      WSH.RegWrite(MsgPlus.ScriptRegPath + Messenger.MyUserId + "\\" + "caps",caps);
}
else if(caps=="off") {
      caps="on";
      WSH.RegWrite(MsgPlus.ScriptRegPath + Messenger.MyUserId + "\\" + "caps",caps);
}
Put the same common lines outside the IF THEN ELSE structure. Then you'll also see that you can reduce the IF THEN ELSE structure to only 1 line using the conditional (ternary) operator. eg: to switch between two states:
myvar = (myvar == "1") ? "2" : "1"

But instead of using strings to indicate the states of the options, use booleans. They are way easier to work with and will reduce the code even further. eg: to switch between two states:
myvar = !myvar

Using booleans will extremely shorten all your code if you use it wisely.

This post was edited on 10-21-2006 at 04:12 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
10-21-2006 03:02 AM
Profile PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
32 / Male / Flag
Joined: Jul 2006
O.P. RE: [Release] Capitals and full stops(Updated!)V1.1
Ok thanks for all the help cookie, will try to use booleans and all them things you mentioned in the next update(and maybe som more scripts from me!)
10-21-2006 01:35 PM
Profile E-Mail PM Find Quote Report
MoonGoose
Junior Member
**

Avatar

Posts: 16
– / Male / –
Joined: Oct 2006
RE: [Release] Capitals and full stops(Updated!)V1.1
nice work, but sometimes I don't get a capital in the beginning. It is turned on!!!
10-21-2006 07:08 PM
Profile E-Mail PM Find Quote Report
Jimbo
Veteran Member
*****

Avatar

Posts: 1650
Reputation: 18
32 / Male / Flag
Joined: Jul 2006
O.P. RE: [Release] Capitals and full stops(Updated!)V1.1
erm, try replacing the first function with this:
code:

function OnEvent_ChatWndSendMessage(ChatWnd, Message) {
var firstChar = Message.charAt(0);
var restOfMessage = Message.substr(1);
if (caps == "on") firstChar = firstChar.toUpperCase();
if (dot == "on" && /[?!:.;,]$/.test(Message) == false) restOfMessage += ".";
return firstChar + restOfMessage;
}



This post was edited on 10-22-2006 at 10:08 AM by Jimbo.
10-22-2006 10:07 AM
Profile E-Mail PM Find Quote Report
Pages: (2): « First « 1 [ 2 ] Last »
« 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