Jump to content

Copy a part of a text file without having the file visible


Akshay07
 Share

Recommended Posts

Hello all,

After spending some time searching in the Help and in the forum, I found out how to open an empty text file, write into it, save it and close it.

I would like now to do something a bit more complicated and I am a bit lost. I am not sure it is possible.

Here is what I would like to automatize (but I want this to be hidden from the user):

1/ Open an existing text file

2/ Paste some text (I don't have any choice here, I need to simulate a PASTE)

3/ Select a specific line and a number of columns (highlight them) and simulate CTRL+C

4/ Paste in another window

I can do that easily using the "Send" command when the window is active. But is it possible to achieve the same thing with the notepad window being hidden?

Link to comment
Share on other sites

I don't think it's possible, if you really, really have to simulate an actual copy and paste operation...I believe it has to be visible to use Send and Control functions.

For instance,

ControlHide("Untitled - Notepad", "", "[CLASSNN:Edit1]")
ControlSend("Untitled - Notepad", "", "[CLASSNN:Edit1]","^v")
sleep(1000)
ControlShow("Untitled - Notepad", "", "[CLASSNN:Edit1]")
doesn't work...or at least it didn't for me.

However, if you just need the data inserted where you want and copied out, and don't actually have to simulate a copy and paste (I know this isn't what you indicated) you can just use FileWrite, some string functions, and FileRead and more string functions to edit and read the file without ever opening Notepad at all...if you want help with this method, just say so ;)

Edited by james3mg
"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110
Link to comment
Share on other sites

You can take the window off screen and activate it and work as usual. The drawback is that the window has to be activated to receive accelerators via keyboard, I believe that sending it messages can overcome this limitation:

#include <Constants.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Run("notepad.exe", "", @SW_HIDE)
WinWait("[CLASS:Notepad]")

$hWnd = WinGetHandle("[CLASS:Notepad]")

_WinAPI_SetWindowLong($hWnd, $GWL_EXSTYLE, BitOR(_WinAPI_GetWindowLong($hWnd, $GWL_EXSTYLE), $WS_EX_TOOLWINDOW))
WinMove($hWnd, "", -65536, -65536)
WinSetState($hWnd, "", @SW_SHOW)
WinActivate($hWnd)

ClipPut("Test 123")
ControlSend($hWnd, "", 0, "^v")
ConsoleWrite(ControlGetText($hWnd, "", "Edit1") & @CRLF)
WinMove($hWnd, "", 0, 0)

;~ WinClose($hWnd)

Edit: If the clipboard content is text only, you can send it using ClipGet() with flag 1 (keys are sent raw), thus the window doesn't have to be activated or visible. I don't think that highlighting is possible though.

Edited by Authenticity
Link to comment
Share on other sites

I'm suggesting:

1. FileRead/ FileReadLine - read file content, select specific line (totally invisible to user)

2. FileWrite/ FileWriteLine - writing operation (totally invisible to user)

3. Send - paste copied material to another window

Hi ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...