Jump to content
Macro Express Forums

System Event


Recommended Posts

I was wondering if someone could explain (or perhaps send me a link to read more about) System Events for macro activation.

 

I'll try explaining what I'm wishing to accomplish and perhaps someone knows of an easier way so I could possibly do without system events for activation.

 

Basically what it is that I need to do is copy the newest line (if it does not equal the previous line) from an open notepad file (these newlines are being written by another program several times every few minutes) switch to third program, paste the line, press "Enter", and then press "ALT+'right arrow'" OR use a mouse click that cycles through a predetermined set of locations each time this occurs.

 

I don't really know the easiest way to go about this, but what's critically important is that every time a newline has been added to the open notepad file I need the above process to occur as fast as possible.

 

Thanks, and kind regards.

Link to comment
Share on other sites

System events are an advanced tactic.

.............................................................

Unless you are a programmer and understand what goes on under the hood of Windows it’s best to stay away. Having said that there are some really cool things you can do in here. However I see nothing in your description that would lead me to believe that System Events would do anything for you.

 

I don’t think you want to open that text file in Notepad. Instead process that text file using the Text File Process command. You haven’t given me very specific info so let me make a hypothetical.

 

Let’s say I have a program that creates a log file when certain things happen. EG an RSS aggregator that logs when it downloads an MP3 from my favorite morning show. Let’s say I want to move that file to my thumb drive with a certain naming convention based on what MP3 was download and at what time. So I would set the activation to Directory Modification. This way when the file is modified the macro will fire. Now with Text File Process I would plow thru the log to find the new entry, maybe do some tests on it, then use that information to move and rename the file based on the last entry in the log. Nice thing about this approach is that it happens completely invisible to the user, lightning fast, and no issues like timing errors.

 

Does this sound anything like what you are trying to do? Perhaps if you gave a little more detail and explained what it is you are doing and why you think you need Notepad open.

Link to comment
Share on other sites

Hi Cory,

 

Thanks for the prompt response.

 

Unfortunately, I need to have Notepad open because another program is dumping information (stock quotes to be specific) to it on a continual basis. I looked through the documentation for the program that's doing the dumping and there doesn't seem to be a way to save it as a log file which is too bad because I could just use the Directory Modification for activation as you were saying.

 

The copy/paste plus keyboard shortcut routine looks like it will be the easy part, however, it would be great if this could somehow operate in the background so I could still use my computer but I'll live if that's not possible.

 

I hope this clears things up.

 

Thanks again for your help.

Link to comment
Share on other sites

The tricky thing is determining which lines are new when your macro runs. Here's a process to try.

 

You have a file A that another program is dumping lines into. Make sure it is empty at the beginning of the day.

Do NOT open this file in Notepad.

 

You will use text files B and C which should not exist when you start the process (beginning of the day, perhaps). B and C will be created automatically when the macro appends lines to them.

 

Write a macro and schedule it to run every few seconds. The macro will:

1. Use Text File Process to read B from beginning to end, counting the lines.

2. Use Text File Process to read A from the beginning, counting the lines and comparing the count (at each line) to B's count.

3. Any line in A which was not in B (based on counters) you will append to both files B and C. (ME command is Variable Modify String [Append to Text File with CR/LF]).

4. When you reach the end of file A, the new lines (since the previous macro run) are in C. All lines since the beginning of the day are in both A and B.

5. Use Text File Process to read C from beginning to end. For each line, activate the window where you need to paste it, do the paste and the other arrow and mouse actions.

6. When you reach the end of C, delete file C. (The next scheduled run of the macro will create it again if there are new lines.)

 

Edit: if the stock-quote program is truly dumping lines into a Notepad window, rather than into a file, then each time the macro runs its first step would be to activate the Notepad window and execute the keystrokes to save Notepad to file A (Alt-File-SaveAs-A-ENTER).

Link to comment
Share on other sites

Are you sure it's not writing to a file?

....................................................

 

If the app is sending text to the Notepad then it’s a program like MEP, I would work on seeing how you could tap into that and redirect its output. Whatever the case it sounds hoaky if it’s using Notepad to display text instead of a proper form.

 

If it is as you say that it’s sending text to Notepad and not saving it to a file then I would look for something visible it does to activate off of. Now one thing to check would be to see if it’s using the clipboard. If it is and there’s some consistent hallmark you can use the clipboard text activation. Otherwise I would simply activate on a schedule.

 

Now instead of activating and messing around in Notes pad ever time the schedule fires I would use controls instead. The editing area in Notepad is a control and you can directly access the ext therein without having to activate Notepad or do anything with the clipboard. Not only that but Notepad doesn’t need to have focus or even be visible. But the really cool part is that it will not interfere with other things you are doing. Oh, and it’s one line, is instantaneous, and flawless. AND it’s only one command! See the sample here. The first command just defines the control variable and can be set at the beginning of the macro. The second got so an untitled Notepad and sets %Test2% to the contents of the Notepad.

Get Control: (NOTEPAD.EXE) -> %CTRL Test%
Get Control Text from %CTRL Test% into %Test2%

This one you can copy and paste.

<GET CONTROL Flags="1" Program_Name="NOTEPAD.EXE" Class="Notepad" Title="Untitled - Notepad" Control="\"Edit\",\"1\"" Variable="%CTRL Test%" Caption="Untitled - Notepad" Partial="FALSE" Wildcards="FALSE"/>
<GET CONTROL TEXT TextVar="%Test2%" ControlVar="%CTRL Test%"/>

Once I have the contents on the clipboard I would read a file that I created last time and cached to the temp folder and compare the two variables. If they are the same abort. If not use the Split String command and split on CRLF (end of line characters) into an array, say %Log%. Now previously I would have saved an integer value of the last line. EG if last time there were 6 lines I would save the number 6. Now read that in, say to %Line Number%, and increment and use that as the element number for whatever it is you’re needing to do. EG %Log[%Line Number%]%. Of course you could expand this to be a repeat if it’s possible multiple new lines will be encountered.

 

Finally you can also probably use controls to send whatever text you need to or interact in whatever way you need to with the destination application. It’s quite possible it could all be done in the background.

 

This might seem really complicated but it’s not. This requires very few lines and is noninvasive. I can see a few concepts here with are often beyond the experience of many macro writers and those are arrays, non-printing characters like CR&LF, and controls. If you tell me which you are unfamiliar with I can explain them more in detail. Also I could create a simple dummy script if need be. And as always I’m available for hire if you would like me to simply write it for you and teach you how to do it yourself. Just send me a PM.

Link to comment
Share on other sites

Hey guys,

 

Thanks for your input. I really appreciate it. I've spent part of the weekend skimming through Macro Express Explained (which I think would have been better if they presented the material more like other typical programming books but I digress).

 

Wouldn't I be able to avoid arrays if I simply use two variables? Every time a newline is written, notepad becomes visible and the program doing the writing makes a system sound either which could probably be used to activate the macro. The macro would go to the last line in notepad, and assign it to T1. Next time around, I would assign the new (last) line to T2. If T2 <> T1, use controls to send the value of T2 to the other program then set T1 equal to T2 and repeat the process.

 

The only way I could see this getting messed up is that sometimes things get dumped quickly and it almost seems like a block of newlines get written all at once. Can't I get macro express to somehow listen to the text file so that every time a line is written it performs the above process?

 

If the app is sending text to the Notepad then it’s a program like MEP, I would work on seeing how you could tap into that and redirect its output. Whatever the case it sounds hoaky if it’s using Notepad to display text instead of a proper form.

 

I can redirect the output so it doesn't necessarily need to be notepad. The program doing the dumping has a linking feature where you drag cross hairs (like MacroExpress) to the target destination for the output. It actually has it's own interface that displays the list of stocks quotes but I think that for my purposes it's probably easier to manipulate symbols in a text file.

 

Here's a link to what the interface looks like in case you are wondering,

 

http://www.trade-ideas.com/Application/Help.html

 

Surprisingly the window is recognized as a control when I drag the ME cross hairs over the window but I don't know if it would serve me in any way.

Link to comment
Share on other sites

The only way I could see this getting messed up is that sometimes things get dumped quickly and it almost seems like a block of newlines get written all at once.

That's why Cory and I suggested keeping track of the last line processed, so your macro will adapt automatically to one or many lines dumped into Notepad since the last macro execution.

 

Wouldn't I be able to avoid arrays if I simply use two variables?

My suggested method avoids arrays, Cory's uses arrays. Otherwise the techniques are largely the same. Arrays would likely be faster, but whether the extra speed buys you anything in the real world, I don't know. The file method has the advantage of surviving restarts of ME or reboots of the PC without losing any data -- you could do that with arrays also by saving the data somewhere, but it's not quite as straightforward.

 

I can redirect the output so it doesn't necessarily need to be notepad.

Might be useful if you can send it directly into a file. All the file processing can run in the background without interacting with a visible window. If you can use Cory's suggestion of pasting into the target window via Windows controls, then there would be nothing to disrupt your hands-on use of the PC. If your stock ticker can only redirect into an active (on-top) window, then it doesn't buy you much.

Link to comment
Share on other sites

  • 3 weeks later...

Hey Cory,

 

I hope you enjoyed your holidays.

 

Anyway, I tried your code to get control of the notepad file, and copy the text from the notepad file. I test ran the code, then clicked "Debug", "Show Variable Values" and I only see the first entry of the text file (which has several hundred entries).

 

Any ideas? Perhaps I am not using an array.

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...