Posted by Catur Nugroho | File under :

Simple Notepad Automation Using Autoit C#

This is a simple example of how to work with Notepad for sending text, copy / paste, and save it as a file. I will try to explain as details so that you can easily understand.
You can adding this code into your project. This is additional function that I have made, you can copy and paste.

private void Type(string strKeys, int nMode, string strControl, bool blnSleepKeys, int nSleep, string strWindow)
        {
            if (blnSleepKeys)
            {
                char[] achKeys = strKeys.ToCharArray();

                foreach (char ch in achKeys)
                {
                    autoit.ControlSend(strWindow, "", strControl, ch.ToString(), nMode);
                    Thread.Sleep(100);
                    Trace.WriteLine("Sent Key: '" + ch.ToString() + "'");
                }
            }
            else
            {
                autoit.ControlSend(strWindow, "", strControl, strKeys, nMode);
                Trace.WriteLine("Sent Keys: " + strKeys);
            }

            for (int i = 0; i < (nSleep / 100); i++)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }

        private void Click(string strButton, int nNumClicks, int nX, int nY, string strControl, int nSleep, string strWindow)
        {
            autoit.ControlClick(strWindow, "", strControl, strButton, nNumClicks, nX, nY);

            for (int i = 0; i < (nSleep / 100); i++)
            {
                Application.DoEvents();
                Thread.Sleep(100);
            }
        }


The first step that must be done is to determine the required parameters like as title, text, control, etc..
You can see the images below.



Parameters :
Title : "[CLASS:Notepad]" or "Untitled - Notepad"
Text : ""
Control : Edit1
nX : 78
nY : 53
private void runButton_Click(object sender, EventArgs e)
        {
            //check if notepad is already exist
            if (autoit.WinExists("[CLASS:Notepad]", "") == 1)
            {
                //notepad closed
                autoit.WinKill("[CLASS:Notepad]", "");
            }
            //execute notepad.exe
            autoit.Run("Notepad.exe", "", 1);
            //wait for max 25 seconds
            autoit.WinWaitActive("[CLASS:Notepad]", "", 25);
            //check if notepad doesn't exist
            if (autoit.WinExists("[CLASS:Notepad]", "") == 0)
            {
                MessageBox.Show("Notepad didn't opened");
            }
            else
            {
                //activate the notepad
                //or set notepad into front

                autoit.WinActivate("[CLASS:Notepad]", "");
            }
            //send some text into text area
            string strText = "autoitsourcecode.blogspot.com";
            Type(strText, 0, "Edit1", true, 500, "[CLASS:Notepad]");
            //try to copy / paste

            //send CTRL+ a
            Type("^a", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + c (Copy)
            Type("^c", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + v (paste)
            Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + v (paste)
            Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");

            //send CTRL + v (paste)
            Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");
            //send CTRL + v (paste)
             Type("^v", 0, "Edit1", true, 1000, "[CLASS:Notepad]");
            //save the file
            //send key alt + f + a

            Type("!fa", 0, "Edit1", true, 500, "[CLASS:Notepad]");
            //wait the "save as" win dialog is opened until 25 seconds
            autoit.WinWaitActive("Save As", "", 25);
            //check if didn't open
            if (autoit.WinExists("Save As", "") == 0)
            {
                MessageBox.Show("The can't saved");
            }


This is images Save As window info



Parameters :
Title : "Save As"
Text : ""
Control : Edit1
nX : 109
nY : 8
            //send key directory folder
            Type(@"C:\autoitDemo.txt", 0, "Edit1", true, 1000, "Save As");
            //check the files
            if (File.Exists(@"C:\autoitDemo.txt"))
                File.Delete(@"C:\autoitDemo.txt");
The image for click Save button


Parameters :
Title : "Save As"
Text : ""
Control : Edit1
nX : 46
nY :13

            //click button save
            Click("left", 1, 42, 15, "Button1", 1000, "Save As");
            //check if notepad is already exist
            if (autoit.WinExists("[CLASS:Notepad]", "") == 1)
            {
                //notepad closed
                autoit.WinKill("[CLASS:Notepad]", "");
            }
        }

Download Full Source Code

4 comments:

  1. You can Download Full Source Code and debug the code. I think the project that I made can work for any computers.

    ReplyDelete
  2. I have problem with autoit script for mouse doubleclick on selected/higlighted item on list view. and I dont want to use control click coordinate. can you give me the clue. terimakasih.

    ReplyDelete
  3. Hi Nugroho, You can try using autoit mouse click. Please remember that it's different with control click. For the mouse position please go to Autoit Window Info - Mouse Tab - Position Value. I hope this is can help you. If you have any problem please contact me at ch4ttur@gmail.com. Semoga pekerjaan anda lancar.

    ReplyDelete