Send text to non-focus Notepad in one chunk

Get help with using AutoHotkey and its commands and hotkeys
ndiaz
Posts: 50
Joined: 10 Jan 2017, 17:05
Google: Nora Diaz
GitHub: Nora

Send text to non-focus Notepad in one chunk

18 Oct 2018, 09:33

Hello,

I was hoping to get help with two questions.

Question 1
I've created a very basic script to send a variable to Notepad without giving Notepad focus. The script is working fine, but the text is typed out one letter at a time (I'm using ControlSend) and I would like to have it all appear at once, as when you do a regular paste. Is that possible? It's key to keep focus on my main program and not give focus to Notepad.

This is my script (apologies in advance as it must look very inelegant to all the experts, I put this together taking bits and pieces from several places):

Code: Select all

#IfWinActive, ahk_exe SDLTradosStudio.exe

~!q::

OriginalCb := ClipboardAll ;~ back up current clipboard content

Clipboard := "" ;~ Empty clipboard

SendInput, {F6}^+{Down} ;activate source and select all text

SendInput, ^c

ClipWait, 3 ;~ Wait for content to appear on clipboard; after 3 seconds, give error message

If ErrorLevel = 1

{

ToolTip, Error`nNo text was copied to the clipboard.

SetTimer, ToolTipOff, 3000

Return

}

Source := Clipboard ;~ copy source text to variable "Source"

Clipboard := ""

SendInput, {Left}

SendInput, {F6}


Clipboard := OriginalCb ;~ restore original clipboard content


ControlSend, ,^a{Delete}, ahk_class Notepad ; delete contents of Notepad before pasting the new text
ControlSend, ,`n%Source%, ahk_class Notepad ; paste the contents of the Source variable 
Return
Question 2
I have a second script, also assembled from things I've found in different places, that I'm using to make Notepad stay always on top, remove the title bar and menu bar. I activate this with a hotkey when the program is active, but I'm wondering if these actions could be incorporated into the above script so I don't have to run two separate scripts. This is the script I'm currently using:

Code: Select all

^SPACE::  
Winset, Alwaysontop, , A
WinSet, Style, -0xC00000, A  ; Remove the active window's title bar (WS_CAPTION)
WinWait Untitled - Notepad
DllCall("SetMenu", uint, WinExist(), uint, 0)  ; Remove menu bar of "last found window".
Return
Thank you in advance for taking a look!

Nora
eelrod
Posts: 48
Joined: 10 Apr 2018, 11:17

Re: Send text to non-focus Notepad in one chunk

18 Oct 2018, 10:23

For question 1, perhaps some of the SendInput & ControlSend commands could be combined? Also, maybe look into seeing if modifying SetBatchLines and SetControlDelay helps.

Code: Select all

SetBatchLines , -1
SetControlDelay , 0
#IfWinActive, ahk_exe SDLTradosStudio.exe
~!q::
OriginalCb := ClipboardAll ;~ back up current clipboard content
Clipboard := "" ;~ Empty clipboard

SendInput , {F6}^+{Down}^c ;activate source and select all text & copy
ClipWait , 3 ;~ Wait for content to appear on clipboard; after 3 seconds, give error message

If ErrorLevel = 1
{
    ToolTip, Error`nNo text was copied to the clipboard.
    SetTimer, ToolTipOff, 3000
    Return
}

Source := Clipboard ;~ copy source text to variable "Source"
Clipboard := OriginalCb ;~ restore original clipboard content

SendInput , {Left}{F6}
ControlSend ,,^a{Delete}`n%Source% , ahk_class Notepad ; delete contents of Notepad & paste the contents of the Source variable
Return
For question 2, both hotkeys can be in the same script. Or, are you asking if they can be combined into one hotkey?
ndiaz
Posts: 50
Joined: 10 Jan 2017, 17:05
Google: Nora Diaz
GitHub: Nora

Re: Send text to non-focus Notepad in one chunk

18 Oct 2018, 11:30

Thank you! Unfortunately, using this code doesn't send the variable to Notepad at all. I'm wondering, can one do a "Ctrl+V" with ControlSend? I can't seem to make it work. Also, is ControlSend the only option to send a variable to an app that doesn't have focus?

Regarding question 2, sorry if I wasn't clear, I meant combining everything into one hotkey, so that when the script runs, if Notepad is running, it will bring it to the forefront and keep it always on top, with the title and menu bar hidden-
User avatar
mshall
Posts: 35
Joined: 13 Jul 2018, 16:42
GitHub: MattAHK

Re: Send text to non-focus Notepad in one chunk

18 Oct 2018, 11:31

Code: Select all

Sendmode, Input
usually is the best way to send text. Put it at the top of your script, otherwise it sends one character at a time and it is pretty slow. This is usually instant.
ndiaz
Posts: 50
Joined: 10 Jan 2017, 17:05
Google: Nora Diaz
GitHub: Nora

Re: Send text to non-focus Notepad in one chunk

18 Oct 2018, 11:52

Thank you, Sendmode Input already appears at the top of my script file.
User avatar
jeeswg
Posts: 5276
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Send text to non-focus Notepad in one chunk

18 Oct 2018, 12:11

- Consider using EditPaste, it will immediately insert text into Notepad. Although, note: when you use EditPaste, just like when you send key presses, if any text is selected, it will be overwritten.
- EditPaste works on Edit controls.

Code: Select all

q::
Control, EditPaste, abc, Edit1, A
return
ndiaz
Posts: 50
Joined: 10 Jan 2017, 17:05
Google: Nora Diaz
GitHub: Nora

Re: Send text to non-focus Notepad in one chunk

18 Oct 2018, 12:29

Thank you! That worked perfectly.

Return to “Ask For Help”

Who is online

Users browsing this forum: grimboto, Mipha, wryyymuda and 33 guests