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

Pages: (3): « First [ 1 ] 2 3 » Last »
Read Text from a file
Author: Message:
TehGoatLord
New Member
*


Posts: 12
Joined: Nov 2010
O.P. Read Text from a file
Alright, so I need a script that reads a specific line from a specific file and then parse it and output the parsed text onto the current chat window.

Say it reads line 255 from a text file
Then it removes a certain string from the line
and outputs it!

If it helps anyone, I already did this in mIRC Scripting

code:
$read(C:\Program Files\AssaultCube_v1.1.0.3\config\saved.cfg, 255)
That's basically what I want, the full function I created is:

code:
alias ACServ {
  //say /connect $remove($remove($read(C:\Program Files\AssaultCube_v1.1.0.3\config\saved.cfg, 255), alias "serverinfo" [), ])
}

I'm sorry if I'm unclear :D
11-07-2010 02:51 AM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Read Text from a file
Here are 2 untested options to get get you started.
Javascript code:
function ReadLineFromFile(sFile, nLine) {
    var f = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(sFile, 1 /* ForReading */);
    while (--nLine > 0 && !f.AtEndOfStream) {
        fso.ReadLine();
    }
    var s = fso.ReadLine();
    f.Close();
    return s;
}

Javascript code:
function ReadLineFromFile(sFile, nLine) {
    var f = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(sFile, 1 /* ForReading */);
    var s = f.ReadAll();
    f.Close()
    var FileContents = s.split('\r\n');
    return FileConents(nLine-1);
}


This post was edited on 11-07-2010 at 03:41 AM by matty.
11-07-2010 03:40 AM
Profile E-Mail PM Find Quote Report
TehGoatLord
New Member
*


Posts: 12
Joined: Nov 2010
O.P. RE: Read Text from a file
Sorry but how do I implement it ?
11-07-2010 04:47 AM
Profile E-Mail PM Find Quote Report
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
31 / 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:
Javascript 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.
    Javascript 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:
    Javascript 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:
Javascript 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
TehGoatLord
New Member
*


Posts: 12
Joined: Nov 2010
O.P. RE: Read Text from a file
This is what I did.. But the command isn't recognized D:


code:
function ReadLineFromFile(sFile, nLine) {
    var f = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(sFile, 1 /* ForReading */);
    var s = f.ReadAll();
    f.Close()
    var FileContents = s.split('\r\n');
    return FileConents(nLine-1);
}

function OnEvent_ChatWndSendMessage() {

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' ) {
    var line = ReadLineFromFile("C:\\Program Files\\AssaultCube_v1.1.0.3\\config\\saved.cfg", 255);
// line now contains the 255th line from saved.cfg

line = line.replace('alias "serverinfo [', '');
line = line.replace(']', '');
    }
}
return sMessage;
}



This post was edited on 11-07-2010 at 02:10 PM by TehGoatLord.
11-07-2010 02:02 PM
Profile E-Mail PM Find Quote Report
TehGoatLord
New Member
*


Posts: 12
Joined: Nov 2010
O.P. RE: Read Text from a file
I'm having problem using the event o_o
11-07-2010 02:04 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Read Text from a file
Well first you are doing it wrong. You need to use the OnEvent_ChatWndSendMessage() function. Have a look at the official documents.

Also I made a mistake in my code and the last return line should use [] not ().
11-07-2010 02:10 PM
Profile E-Mail PM Find Quote Report
TehGoatLord
New Member
*


Posts: 12
Joined: Nov 2010
O.P. RE: Read Text from a file
I find the official documents rather confusing :S it doesn't have examples and stuff so it's rather hard to figure out and I wont  say that I'm good in English either. It'd be a great help if someone would create the script for me :(
11-07-2010 04:47 PM
Profile E-Mail PM Find Quote Report
whiz
Senior Member
****


Posts: 568
Reputation: 8
– / – / Flag
Joined: Nov 2008
RE: Read Text from a file
You need to include the parameters (ChatWnd and Message) for OnEvent_ChatWndReceiveMessage().
JScript code:
function ReadLineFromFile(sFile, nLine)
{
    var f = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(sFile, 1 /* ForReading */);
    var s = f.ReadAll();
    f.Close()
    var FileContents = s.split('\r\n');
    return FileConents[nLine - 1];
}
 
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage)
{
    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')
        {
            var line = ReadLineFromFile("C:\\Program Files\\AssaultCube_v1.1.0.3\\config\\saved.cfg", 255);
            // line now contains the 255th line from saved.cfg
            line = line.replace('alias "serverinfo [', '');
            line = line.replace(']', '');
        }
    }
    return sMessage;
}


If you're not sure how to put together and compile the script, have a look at the attachment.

.plsc File Attachment: AssaultCube Read.plsc (1.05 KB)
This file has been downloaded 78 time(s).
11-07-2010 05:24 PM
Profile E-Mail PM Find Quote Report
TehGoatLord
New Member
*


Posts: 12
Joined: Nov 2010
O.P. RE: Read Text from a file
It doesn't work!
"The command you entered was not recognized
if the message was not meant to be a command etc"
11-07-2010 06:09 PM
Profile E-Mail PM Find Quote Report
Pages: (3): « First [ 1 ] 2 3 » 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