What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Read Text from a file

Read Text from a file
Author: Message:
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Read Text from a file
matty just provided you with two possible implementations of how you could read a single line from a file (what's in a function name?). Basically, you copy one of those functions in your script and call them:
js code:
var line = ReadLineFromFile("C:\\Program Files\\AssaultCube_v1.1.0.3\\config\\saved.cfg", 255);
// line now contains the 255th line from saved.cfg
Of course, the most important part is when you call the function and what you do with it. If I understand correctly, you want to create a new chat command "/acserv" which will read the line, do some modifications with it and then output it as a chat message.
  1. In order to make your script do something when a chat message is sent by the current user, you'll need to create an OnEvent_ChatWndSendMessage function. This function will be called whenever a message is sent, allowing you to modify it before it gets sent. Have a look at the scripting documentation on how to define this event in your script.
  2. Inside this function, you'll want to check whether the message starts with your command "/acserv". There are a lot of cases which you need to take into account when parsing script commands, for example when there is more than one slash before a command, that command should be escaped and thus not parsed at all. Also, there are some limitations on which characters are allowed in the command name and the parameters. Luckily, CookieRevised wrote a regular expression which deals with all these cases.
    js code:
    var m;
    if ( m = /^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)[\s\xA0]?([\s\S]*)/.exec(sMessage) ) {
        var command = m[1].toLowerCase();
        var parameter = m[2];
        if ( command == 'acserv' ) {
            // do something and return a modified message
        }
    }
    return sMessage;
    Source: CookieRevised's reply to Gettin data from "/" commands (small modifications by myself)
  3. Inside the command parser check if ( command == 'acserv' ) { ... }, you place your actual code. That is, call ReadLineFromFile, remove something from the returned string and output it by returning it back to the event function. So you start off with the first code block in this post, then do some replace() on the string, such as:
    js code:
    line = line.replace('alias "serverinfo [', '');
    line = line.replace(']', '');
    and finally return line;.

If you're planning to use the second function matty posted, you'll want to fix a few things in his code:
js code:
function ReadLineFromFile(sFile, nLine) {
    var f = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(sFile, 1 /* ForReading */);
    var s = f.ReadAll();
    f.Close(); // added semicolon
    var FileContents = s.split(/\r?\n/); // matches both \r\n and \n
    return FileContents[nLine-1]; // fixed variable name and needs [ ] instead of ( ) since it's an array accessor.
}
I can't tell which of those implementations is the best, they all look pretty good. In most cases, the first method will be faster since it won't have to read the whole file, yet the second method may be easier to understand.
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
11-07-2010 10:11 AM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Read Text from a file - by TehGoatLord on 11-07-2010 at 02:51 AM
RE: Read Text from a file - by matty on 11-07-2010 at 03:40 AM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 04:47 AM
RE: Read Text from a file - by Matti on 11-07-2010 at 10:11 AM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 02:02 PM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 02:04 PM
RE: Read Text from a file - by matty on 11-07-2010 at 02:10 PM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 04:47 PM
RE: Read Text from a file - by whiz on 11-07-2010 at 05:24 PM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 06:09 PM
RE: Read Text from a file - by matty on 11-07-2010 at 06:30 PM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 06:48 PM
RE: Read Text from a file - by Matti on 11-07-2010 at 07:42 PM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 08:01 PM
RE: Read Text from a file - by matty on 11-07-2010 at 08:07 PM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 08:14 PM
RE: Read Text from a file - by Matti on 11-07-2010 at 08:22 PM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 08:30 PM
RE: Read Text from a file - by Matti on 11-07-2010 at 08:42 PM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 08:51 PM
RE: Read Text from a file - by Matti on 11-07-2010 at 10:36 PM
RE: Read Text from a file - by TehGoatLord on 11-07-2010 at 10:45 PM


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