Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

User Image
hushaq
2 discussion posts
Hi,

My work requires me to collect various snippets of text into a single text file. I have been trying to write a macro, which opens a notepad and pastes the present clipboard text into that notepad. But if notepad is open, then I want it to append the clipboard text to the specified text file. So far I have succeeded in opening the notepad and pasting text in it for first time, but if the notepad is open, I am unable to get focus to it, which in turn does not let the text to be pasted in it.

My code is as follows:

using System;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

public static class ClipboardFusionHelper
{
public static string ProcessText(string text)
{
Process[] localByName = Process.GetProcessesByName("notepad");
int i = localByName.Length;
if(i==0)
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.StartInfo.Arguments = "C:\\123.txt";
myProcess.Start();
myProcess.WaitForInputIdle(2000);
IntPtr hWnd = myProcess.MainWindowHandle;
MacroAPI.PasteText(text+" ");
return text;
}
else
{
System.Diagnostics.Process myproc = localByName[i-1];
myproc.Refresh();
myproc.WaitForInputIdle(2000);
IntPtr hWND = myproc.MainWindowHandle;
MacroAPI.PasteText(text+"next text");

return text+"snippet";
}
}

}

Any help or an alternate way to achieve my goal would be great.
Mar 16, 2011  • #1
User Image
Splat
21 discussion posts
This doesn't directly answer your question, but rather provides an alternative.

Is there any reason that you need to have Notepad open when you are adding to the text file? If not, then you can just use .NET's ability to append text to a file using the StreamWriter (or similar, there are a number of ways to do this). This will just use the file system, not the user interface. This also removes the need to check if processes are running, and all the pauses that you have in your code (waiting for the user interface to respond).

Any of these pages from a Google search should help you out : http://www.google.com.au/search?q=csharp+append+text+to+file

After you have appended all of the snippets using the macro, then you just have to open the text file and it should all be there.

If required, remember to add line breaks or tab/space characters between the snippets.
Mar 16, 2011  • #2
User Image
hushaq
2 discussion posts
First of all I should thank you for the help.

Actually, the first thing I tried when writing this macro, was using streamwriter, but it kept on throwing all kinds of exception. So I had to move to the more complicated way. But after seeing your reply I tried streamwriter once more to see whether I was doing something wrong. To my surprise, the macro with streamwriter ran without any trouble. Then the problem dawned upon me. Earlier I was using windows 7, but this time I had used my desktop which has XP. Win 7 had some security setting, which was not letting the macro access the specified path, but there was no such issue in XP so it ran smoothly.

Thanks again for your suggestion. Everything is looking great now. :-)
Mar 17, 2011  • #3
Jon Tackabury (BFS)'s profile on WallpaperFusion.com
Thanks for helping out, Splat. I'm glad to hear your Macro is working now. :)
Mar 20, 2011  • #4
Subscribe to this discussion topic using RSS
Was this helpful?  Login to Vote(-)  Login to Vote(-)