AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Open File in Editor
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
moorpipe



Joined: 04 Oct 2006
Posts: 19

PostPosted: Mon Oct 09, 2006 12:27 am    Post subject: Open File in Editor Reply with quote

Is there a way to open a text file in a running Notepad (or any other editor) automatically (w/o user intervention)?

I am not looking for things like:
- Run, Notepad.exe c:\temp.txt
Opens a new instance of Notepad.
- Anything that opens a dialog e.g. WinMenuSelectItem or Send, {ALTDOWN}f{ALTUP}{DOWN}{ENTER} etc.
Even if I paste the file name into the edit box, I see the dialog for a moment.
- Anything that replaces text via functions like ControlSetText or their DllCall equivalents (e.g. SendMessage)
Although this is nice and quick, it doesn't really open the text file. Furthermore it doesn't really work in some editors (tested with EditPlus)

Hopefully my request makes any sense.
Thanks!
Back to top
View user's profile Send private message
d-man



Joined: 08 Jun 2006
Posts: 247

PostPosted: Mon Oct 09, 2006 12:38 am    Post subject: Reply with quote

I don't think so. You eliminated all the possibilities I know of with AHK. What else could there be?
Back to top
View user's profile Send private message
i3egohan



Joined: 18 Jul 2006
Posts: 201

PostPosted: Mon Oct 09, 2006 12:41 am    Post subject: Reply with quote

I think the following code is what your after

Code:

FileRead, Mytext, Textfile.txt ; Textfile.txt should be the text file you wish to open.
WinWait, Untitled - Notepad ; Waits until window is avalible
WinActivate, Untitled - Notepad ; Activates the window.
Send, %Mytext% ; Sends your textfile to notepad
ExitApp ; Exits the application


Hope this will work for you.

Regards, I3egohan
_________________


I3egohan
Back to top
View user's profile Send private message
moorpipe



Joined: 04 Oct 2006
Posts: 19

PostPosted: Mon Oct 09, 2006 1:34 am    Post subject: Reply with quote

Comes close. Just had to change the title into a window handle, because the title isn't fixed in my case. I also have to find an alternative to the Send command, because I don't want to append key strokes to existing text. Besides Send is slow.

Thanks for your help. It's hard to believe the solution is that simple.
Back to top
View user's profile Send private message
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Mon Oct 09, 2006 1:40 am    Post subject: Reply with quote

Code:
SendInput

Give that a try, it buffers input until it is finished, and then sends the keys in the buffer. It is a lot faster than Send.
Back to top
View user's profile Send private message Visit poster's website AIM Address
d-man



Joined: 08 Jun 2006
Posts: 247

PostPosted: Mon Oct 09, 2006 1:43 am    Post subject: Reply with quote

i3egohan wrote:
I think the following code is what your after

Code:

FileRead, Mytext, Textfile.txt ; Textfile.txt should be the text file you wish to open.
WinWait, Untitled - Notepad ; Waits until window is avalible
WinActivate, Untitled - Notepad ; Activates the window.
Send, %Mytext% ; Sends your textfile to notepad
ExitApp ; Exits the application


Hope this will work for you.

Regards, I3egohan


yeah but it doesn't open the file in the editor like he wanted
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5068
Location: imaginationland

PostPosted: Mon Oct 09, 2006 1:47 am    Post subject: Reply with quote

Other than the ways you've listed I don't think it's possible. I know it's not what you want but this is the WinMenuSelectItem method, there's only a half a second lag period where you catch a glimpse of the File Open dialog:
Code:
SetBatchLines, -1 ; make the script run fast
SetWinDelay, -1 ; no window delay

new = %A_MyDocuments%\file.txt ; new file to open

SetTitleMatchMode, 2
win := WinExist("Notepad") ; window id of Notepad
If !win ; if Notepad isn't open:
   ExitApp ; quit
WinMenuSelectItem, ahk_id %win%, , File, Open ; otherwise select File > Open
WinWait, Open ahk_class #32770 ; wait for File Open dialog
ControlSetText, Edit1, %new% ; set path of new file
ControlClick, Button2 ; click the Open button
Return

_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Mon Oct 09, 2006 2:11 am    Post subject: Reply with quote

Quote:
yeah but it doesn't open the file in the editor like he wanted

I assumed he knew to modify the last posted code. Who wouldnt?
Its obvious if you see "SendInput" infrount of you as a command that it should replace "Send".

Code:

FileRead, Mytext, Textfile.txt ; Textfile.txt should be the text file you wish to open.
WinWait, Untitled - Notepad ; Waits until window is avalible
WinActivate, Untitled - Notepad ; Activates the window.
SendInput, %Mytext% ; Sends your textfile to notepad
ExitApp ; Exits the application
Back to top
View user's profile Send private message Visit poster's website AIM Address
moorpipe



Joined: 04 Oct 2006
Posts: 19

PostPosted: Mon Oct 09, 2006 2:16 am    Post subject: Reply with quote

@.AHK
Quote:
Give that a try, it buffers input until it is finished, and then sends the keys in the buffer. It is a lot faster than Send.

SendInput indeed is lighting fast, but it still appends text and even more disturbing it adds empty lines. Say the contents of my text file is:
qwerty
asdfgh
zxcvbn
then SendInput results in:
qwerty

asdfgh

zxcvbn

@d-man
Quote:
yeah but it doesn't open the file in the editor like he wanted

you're right, but when I try to close Notepad (or EditPlus) it warns me to save the text. So, I guess it works like I had opened it. I think that would do for me now. Thanks for reminding.

@Titan
Quote:
there's only a half a second lag period where you catch a glimpse of the File Open dialog

WinMenuSelectItem. Sorry, call me nitpicking, but even the slightest flickering isn't good enough for me. My project should be able to open files quickly one after the other.

Thanks all.
Back to top
View user's profile Send private message
d-man



Joined: 08 Jun 2006
Posts: 247

PostPosted: Mon Oct 09, 2006 2:42 am    Post subject: Reply with quote

moorpipe wrote:
@d-man
Quote:
yeah but it doesn't open the file in the editor like he wanted

you're right, but when I try to close Notepad (or EditPlus) it warns me to save the text. So, I guess it works like I had opened it. I think that would do for me now. Thanks for reminding.


But you said specifically:

Quote:
Although this is nice and quick, it doesn't really open the text file.


Why did you say that if you were happy with it already? Not trying to be anal but... instead of the "send" command just load the file into a variable and use the ControlSetText command to send it to Edit1 ,... like u said, "this is nice and quick"
Back to top
View user's profile Send private message
moorpipe



Joined: 04 Oct 2006
Posts: 19

PostPosted: Mon Oct 09, 2006 4:00 am    Post subject: Reply with quote

Quote:
Why did you say that if you were happy with it already?

I didn't. Before I posted my request I did some tests with ControlSetText and other calls e.g. with a DllCall to SendMessage with WM_SETTEXT. It turned out that subsequent calls just overwrote the previous (changed) contents without any warning.
That's why I asked for another method, a method that would open a file, because I thought that would fix the problem (maybe I'm already mistaken here).

Than I followed the advice to use Send / SendInput (something I didn't do before, because to me these commands were primarily ment to be used to simulate keystrokes). At first it seems to work (besides the append issue), because Notepad (and EditPlus) gave me a warning when I tried to open another file before saving. STUPID ME!
I did the test directly via the menu and not via a subsequent load. ControlSetText also gives a warning if I try to close the file w/o saving via the menu.
So, you're right again. Apologies!

I'll have to find a method that warns me if a file has been changed before I do the next ControlSetText (or whatever).

Regards.
Back to top
View user's profile Send private message
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Mon Oct 09, 2006 4:24 am    Post subject: Reply with quote

Could you explain this more please.
Quote:
At first it seems to work (besides the append issue), because Notepad (and EditPlus) gave me a warning when I tried to open another file before saving. STUPID ME!

what is the append issue?

Why exactly must the text be added directly to the open editor?
Back to top
View user's profile Send private message Visit poster's website AIM Address
moorpipe



Joined: 04 Oct 2006
Posts: 19

PostPosted: Mon Oct 09, 2006 5:01 am    Post subject: Reply with quote

.AHK wrote:
Could you explain this more please. what is the append issue? Why exactly must the text be added directly to the open editor?

Maybe some code will clarify (English is not my native language, but AHK is universal isn't it Wink )

method 1:
Code:
(...)
CtrlHandle := GetChildHWND(Editor_Handle, "Edit1")
If CtrlHandle
{
    FileRead, FContents, %SelectedFullPath%\%FName%
    ControlSetText, , %FContents%, ahk_id %CtrlHandle%
    FContents =        ; free memory
}
Else
    MsgBox, Error: Could not retrieve the control's handle

method 2:
Code:
(...)
CtrlHandle := GetChildHWND(Editor_Handle, "Edit1")
If CtrlHandle
{
    WinWait, ahk_id %CtrlHandle%,,5     ; Waits until window is avalible
    if ErrorLevel
    {
        MsgBox, WinWait timed out.
        return
    }
    else
    {
        FileRead, FContents, %SelectedFullPath%\%FName%
        WinActivate, ahk_id %CtrlHandle% ; Activates the window.
        SendInput, %FContents%
        FContents =        ; free memory
    }
}
Else
    MsgBox, Error: Could not retrieve the control's handle

method 3:
Code:
(...)
CtrlHandle := GetChildHWND(Editor_Handle, "Edit1")
If CtrlHandle
{
    WM_SETTEXT := 0x0C      ; replaces the text
    FileRead, FContents, %SelectedFullPath%\%FName%
    DllCall("SendMessageA", "Uint", CtrlHandle, "int", WM_SETTEXT, "str", NULL, "str", FContents)
    FContents =             ; free memory
}
Else
    MsgBox, Error: Could not retrieve the control's handle


All 3 methodes work. But method 2 appends the text if there's already text in the edit control and it adds empty lines after each text line. My project is such that subsequents files could be "opened", like skipping through a directory of files. But if someone changes a file in between, the script should notify that.
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5068
Location: imaginationland

PostPosted: Mon Oct 09, 2006 11:49 am    Post subject: Reply with quote

moorpipe wrote:
WinMenuSelectItem. Sorry, call me nitpicking, but even the slightest flickering isn't good enough for me. My project should be able to open files quickly one after the other.
Try SendMessage.
_________________

RegExReplace("irc.freenode.net/ahk", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Mon Oct 09, 2006 1:49 pm    Post subject: Reply with quote

Frankly, I didn't read all the messages of the topic, but I believe it is not possible to find an universal solution, it will necessarily depends on the target editor. For some, like Notepad, it isn't possible at all. For others, like SciTE, you can, for example, activate the unique instance option (lot of editors have this one) or when knowing well the code, use the mechanism used in this option: when unique instance is activated, a new instance is always created. It checks if there is already a running instance (various methods), then either exit (nothing to do more) or send to the old instance the command line parameters to process (like a new file name/path) and exit.
Some programs use DDE for this (out of fashion), others WM_COPYDATA message, etc.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group