Jump to content
Macro Express Forums

Reading and opened notepad file


ehercun

Recommended Posts

I'm trying to read all the text from an opened notepad file via controls and I'm running into some problems. I tried using the following code to get control of the notepad file, get control text, and then used the split string command on <Enter> to enter the text into an array.

 

<GET CONTROL Flags="1" Program_Name="NOTEPAD.EXE" Class="Notepad" Title="MyStrat.txt - Notepad" Control="\"Edit\",\"1\"" Variable="%C1%" Caption="MyStrat.txt - Notepad" Partial="FALSE" Wildcards="FALSE"/>
<GET CONTROL TEXT TextVar="%Test2%" ControlVar="%C1%"/>
<SPLIT STRING Source="%Test2%" SplitChar="<ENTER>" Dest="%Log2%" Index="1"/>

 

When I test run the code, then click "Debug" then "Show variable values", I only see the first entry in my notepad file in the variable Test2. However, for troubleshooting purposes, I tried separating each entry by a space and then I see all the values under Test2. Unfortunately, I am unable to change the way they are being entered into notepad.

 

Is there any way to get ME to monitor when changes are made to the opened (not saved) notepad file on the fly and only then activate the macro? Or do I just have to run this every few seconds to see if anything has been added to the notepad file.

 

Thanks!

Link to comment
Share on other sites

I don't think Enter is a valid character to split on. You got multiple values with spaces because they are valid. I don't know how to split on a new line, if it's even possible with this command and plain text.

 

I ran your macro which worked fine (my split variable was %log% with an array size of 25). I chose "the" to split my file with. I used large text boxes to look at the values of %Test2% and %log[1]%, %log[2]% etc after the Control Text and the Split String lines. The debug window only shows a small amount of text.

 

You want to monitor the file for changes but if it is not being saved, how is the text being changed? Any actions by ME directly will be known to you or by the application that's doing it.

Link to comment
Share on other sites

Hi John,

 

Thanks for your reply.

 

You want to monitor the file for changes but if it is not being saved, how is the text being changed?

 

A program is creating those new lines to the opened text file at random times. It sounds strange but that's what's happening and I really would like to be able to store each new line into an array. Hopefully I won't have to create a separate macro to save the file every few seconds and analyze the file itself.

 

Regards.

Link to comment
Share on other sites

I've off for a few hours. Is this text file visible? Will the entries keep been added out of sight? What sort of text is being added? Do you have to save each line as it appears or can you do it at the end? To tell if there is a new entry with a visible file you could activate the file every so often, Ctrl+End (end of text), get the cursor coordinates and see if it has changed from last time. It's very dependent on the writing mechanism.

 

It sounds as though there are blank lines between entries (from your use of <Enter>). I tested a clumsy macro that would separate by CR/LF when done, using the ASCII values but I'm sure someone else will have something that will work.

Link to comment
Share on other sites

John,

 

This text file is visible like you would be writing to it yourself. It seems that every time the program writes to the text file, the text file becomes "active" for a split second and then back to whatever program that was focused (selected in other words) beforehand. Haha, it just came to me right now that I could try to activate this macro based on a window becoming visible/focused; but I digress.

 

Here's a sample of the entries:

 

lvlt

lvlt

lvlt

lvlt

cby

cby

cby

lvlt

lvlt

lvlt

pde

pde

pde

pde

cby

ilmn

lvlt

lvlt

game

swhc

cby

cby

tk

jdsu

lava

lava

lava

cby

cby

cby

klic

reg

reg

reg

dltr

cby

life

cell

life

mea

mea

pde

pde

pde

alth

oii

tco

tco

 

There's probably several thousand of these new lines added per day.

 

I would like to have as little delay as possible between a new entry being added and storing the entry in an array.

 

Regards.

Link to comment
Share on other sites

I should also add that new entries are added to the bottom of the text file and sometimes, blocks of new entries are added at once.

 

I should have also mentioned what it is that I'm trying to accomplish, and that is that I have to see if the newest line is not equal to the previous one. Which is all that I'm after.

Link to comment
Share on other sites

When you type the ENTER key in notepad to end a line, what is actually inserted into the notepad file is a two-character sequence Carriage Return / Line Feed. These are ASCII character values of 13 / 10.

 

So instead of splitting on <ENTER>, first set two one-character string variables like so:

Variable Set to ASCII Character 13 to %CRLF%

Variable Set to ASCII Character 10 to %LF%

Then append the LF to CRLF to make a two-character string variable, %CRLF%

 

Split on %CRLF% rather than on <ENTER>

 

<VARIABLE SET TO ASCII CHAR Value="13" Destination="%CRLF%"/>
<VARIABLE SET TO ASCII CHAR Value="10" Destination="%LF%"/>
<VARIABLE MODIFY STRING Option="\x07" Destination="%CRLF%" Variable="%LF%"/>

 

P.S. Your plan to activate the macro based on the window gaining focus, should work nicely. You will still have the issue of figuring out whether one line has been added since last time, or more than one line. Cory and I suggested ways to do that in your previous thread.

Link to comment
Share on other sites

Bob,

 

That worked beautifully! I feel like I'm actually slowly progressing toward my goal (and learning on the way of course!) thanks to you guys.

 

Well, surprise, surprise, I ran into another problem.

 

I was attempting to sort my text file to rid it of any consecutive duplicates (ie. if consecutiveEntry <> previousEntry copy to text file, counters++) and it turns out that after running the macro, consecutive duplicates remained. Here is the code:

 

<VARIABLE SET TO ASCII CHAR Value="13" Destination="%CRLF%"/>
<VARIABLE SET TO ASCII CHAR Value="10" Destination="%LF%"/>
<VARIABLE MODIFY STRING Option="\x07" Destination="%CRLF%" Variable="%LF%"/>
<GET CONTROL Flags="1" Program_Name="NOTEPAD.EXE" Class="Notepad" Title="MyStrat.txt - Notepad" Control="\"Edit\",\"1\"" Variable="%C1%" Caption="MyStrat.txt - Notepad" Partial="FALSE" Wildcards="FALSE"/>
<GET CONTROL TEXT TextVar="%Test2%" ControlVar="%C1%"/>
<SPLIT STRING Source="%Test2%" SplitChar="%CRLF%" Dest="%Log2%" Index="1"/>
<GET ARRAY LENGTH Array="%Log2%" Dest="%ARlength%"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%counter%" Value="1"/>
<VARIABLE SET INTEGER Option="\x00" Destination="%counter2%" Value="0"/>
<GET CONTROL Flags="1" Program_Name="NOTEPAD.EXE" Class="Notepad" Title="temp1.txt - Notepad" Control="\"Edit\",\"1\"" Variable="%C2%" Caption="temp1.txt - Notepad" Partial="FALSE" Wildcards="FALSE"/>
<REPEAT UNTIL Variable="%counter%" Condition="\x04" Value="%ARlength%"/>
<IF VARIABLE Variable="%counter%" Condition="\x00" Value="1" IgnoreCase="FALSE"/>
<VARIABLE MODIFY STRING Option="\x12" Destination="%Log2[%counter%]%" Filename="C:\\Users\\Eugene\\Desktop\\temp1.txt" CRLF="TRUE"/>
<ELSE/>
<IF VARIABLE Variable="%Log2[%counter%]%" Condition="\x01" Value="%Log2[%counter2%]%" IgnoreCase="TRUE"/>
<VARIABLE MODIFY STRING Option="\x12" Destination="%Log2[%counter%]%" Filename="C:\\Users\\Eugene\\Desktop\\temp1.txt" CRLF="TRUE"/>
<END IF/>
<END IF/>
<VARIABLE MODIFY INTEGER Option="\x00" Destination="%counter%" Value1="%counter%" Value2="1"/>
<VARIABLE MODIFY INTEGER Option="\x00" Destination="%counter2%" Value1="%counter2%" Value2="1"/>
<END REPEAT/>

 

To make it easier for testing, here is the list of items in my text file to be sorted.

 

cby

cake

ads

bec

ilmn

etp

etp

tk

smi

oii

tif

tre

dps

slab

tre

rdy

chs

chs

chs

chs

fhn

tk

tk

ctxs

ctxs

ctxs

tk

fst

mir

axs

axs

car

fhn

dnr

cby

tk

axs

smi

cby

utiw

exr

 

Is it just me or is ME a bit slow at processing bigger arrays when compared to other languages? I might be able to keep my array rather short by deleting all the entries in the original text file but I have to make sure that I don't lose any new data that's written to the text file in the process.

 

On another note, should I even bother attempting to use a text file to create some kind of a FIFO buffer? The only potential problem that I can see is that Windows may not like one macro writing to the end of the text file, while another reads the beginning of the text file and subsequently deletes each entry after being "read" (I send each line being read to another program but this is not relevant for now). This would have been so much easier with PERL haha.

 

Also, would anyone happen to know how to detect an empty entry in a string array so that I don't have to process the entire array? I tried using "" for my repeat until condition, but I could see under "show variables" that it still ended up processing the entire array even though only a tenth of the array was actually filled with entries.

 

I must say that I really wasn't expecting my first macro to be THIS complex...

 

Regards.

Link to comment
Share on other sites

I've even "stepped through" and watched how the variables changed through the macro and it seemed like the macro completely disregarded the condition in the IF statement and wrote to the file anyway.

 

I've tried messing with different conditions just to play around with it and it seems that the IF statement completely disregards what I'm telling it to do!

Link to comment
Share on other sites

I've even "stepped through" and watched how the variables changed through the macro and it seemed like the macro completely disregarded the condition in the IF statement and wrote to the file anyway.

 

I've tried messing with different conditions just to play around with it and it seems that the IF statement completely disregards what I'm telling it to do!

 

I'm stumped. I played with your macro for half an hour, and I get the same result you do. It's as if the IF statement were not there at all (or always results in a not-equal condition). I displayed the variable values in a text box immediately before the IF. I changed the "if not-equal" to "if equal" with an "else". I moved the array elements to the old T[xx] variables and displayed those and compared on those instead of on the array elements. I trimmed the elements before the compare. No matter what I do, the output file is simply a copy of the input.

 

Either there's something seriously wrong with Macro Express, or you and I are both missing a really stupid error in the logic. I think YOU should report it to Insight as a bug, so I don't have to take the hit for a dumb question. :rolleyes:

Link to comment
Share on other sites

Are you running version 4.1.3.1? If so, we discovered a problem with this type of variable construct - %Log2[%counter%]% - in this version with the If Variable command. Most likely this is what you are seeing, that it is being ignored. Our plan is to release a new version later this week that fixes this problem. Sorry for this error.

 

So it appears that it was a bug Bob. Strange that no one else has noticed it. Don't people use arrays?

Link to comment
Share on other sites

So it appears that it was a bug Bob. Strange that no one else has noticed it. Don't people use arrays?

Had to be a bug. It was inconceivable to me that you and I could both be that dumb. :lol:

If it affects ONLY release 4.1.3.1 that could explain why others haven't seen it. Your average non-professional user probably does not get into arrays. It's scary, though, that this kind of bug would crop up in a maintenance release. I am testing dozens of macros in applications that register hospital patients, to convert the users over to ME Pro. It does nothing good for my credibility if we are bringing patients into the Emergency Room and the process goes to hell in a handbasket due to a silly ME bug.

Link to comment
Share on other sites

I was exploring along similar lines with decimals (hence my post about how annoying it was entering %Sales[%N[1]%]%). The If statements seemed to work OK for those. I was trying to see how many members of an array were populated. My problem was with the empty members having 0.0 put in them by default - which could also be a genuine value. The only method I found that worked was to fill the array members at the start, each with the same ridiculous number such as 1E12. That could then be used to tell which members had been populated otherwise. It was a somewhat academic exercise because instead of doing that you could just keep track of members as you assigned values.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...