How to obtain textual contents from a window - autohotkey

I have a window that displays a book. In two smaller boxes below, there is page number and volume information of the book that is open. I can get that information easily as follows:
ControlGetText, volume, ThunderRT6TextBox3
ControlGetText, page, ThunderRT6TextBox2
Then my script makes my mouse pointer move to a button. It clicks it, and a new window pops open. In that window, there is more textual information related to the book, such as publisher, name author, edition etc. I want to retrieve that information. But when I try the same strategy it is not working, eg:
ControlGetText, data, RichTextWndClass3
The only difference between the two cases, is that those two small boxes are editable, you can enter text whereas this window is static.
I tried many other options such as:
SendEvent ^a
Which is equivalent to control + a, which should select everything. I tried putting pauses but it would never select. I tried the script to manually double click on that window. It works, and one word gets select like that. Even then SendEvent ^a doesn't do anything.
However, if I do SendEvent ^{insert}, then the selected word gets copied to my clipboard.
I experimented with more combinations:
ControlSend ahk_class ThunderRT6FormDC, ^a
ControlSend ClassNN RichTextWndClass3, ^a
and
ControlSend ThunderRT6FormDC, ^a
ControlSend RichTextWndClass3, ^a
None of them work. All text selection does not manifest itself in that window.
The only alternative remaining for me is to make the script do a manual selection of the entire text. However, this is slow and very ridiculous. Moreover, in Window Spy under the section: Visible Window Text, the text is all there. I tried many other possibilities and I am at the end of my wits. How am I to harvest that text directly?
EDIT--
The text of the window shows in Window Spy under the heading: TitleMatchMode=slow Visible Text, NOT the heading: Visible Window Text
EDIT--
I spoke to you about two windows. The first one in which i get volume and page number. The second one which needs to pop up by pressing a button.
Both these windows have the same class-name:
ahk_class ThunderRT6MDIForm
Is that problematic in any way?
EDIT--
The conclusion is that it is impossible for me to get that text from the second window directly. As such, I opted for the lame, boring manual method. I send out a {shift down} to the active window and then do a click at the bottom of the window. Then I save the selection to my clipboard. It works, but it is just stupid. Please help me find a more elegant solution than this one.
This is the code I used:
ControlGetText, volume, ThunderRT6TextBox3
ControlGetText, page, ThunderRT6TextBox2
Click, 110, 70
sleep 1000
SendInput {shift down}
click 29, 490
SendInput {shift up}
sleep 1000
SendInput, ^{ins}
sleep 100
It is funny to note that real keyboard keys, such as a b c are not possible. But I am able to send a ctrl, shift and an ins. As I noted above, ^c was also giving issues just like ^a was giving issues.

This routine will do the job of getting and returning from the active window the following text sections:
- EdtWindowTextFastVisible
- EdtWindowTextSlowVisible
- EdtWindowTextFastHidden
- EdtWindowTextSlowHidden
MyGetWindowText(ByRef EdtWindowTextFastVisible, ByRef EdtWindowTextSlowVisible, ByRef EdtWindowTextFastHidden,ByRef EdtWindowTextSlowHidden)
{
; Source: https://code.google.com/p/autohotkey-cn/source/browse/trunk/Source/AHK_Window_Info/AHK_Window_Info_v1.7.ahk?r=6
EdtWindowTextFastVisible =
EdtWindowTextSlowVisible =
EdtWindowTextFastHidden =
EdtWindowTextSlowHidden =
WindowControlTextSize = 32767
VarSetCapacity(WindowControlText, WindowControlTextSize)
WinGet, WindowUniqueID, ID, A
;Suggested by Chris
WinGet, ListOfControlHandles, ControlListHwnd, ahk_id %WindowUniqueID% ; Requires v1.0.43.06+.
Loop, Parse, ListOfControlHandles, `n
{
text_is_fast := true
If not DllCall("GetWindowText", "uint", A_LoopField, "str", WindowControlText, "int", WindowControlTextSize)
{
text_is_fast := false
SendMessage, 0xD, WindowControlTextSize, &WindowControlText,, ahk_id %A_LoopField% ; 0xD is WM_GETTEXT
}
If (WindowControlText <> ""){
ControlGet, WindowControlStyle, Style,,, ahk_id %A_LoopField%
If (WindowControlStyle & 0x10000000)
{ ; Control is visible vs. hidden (WS_VISIBLE).
If text_is_fast
EdtWindowTextFastVisible = %EdtWindowTextFastVisible%%WindowControlText%`r`n
Else
EdtWindowTextSlowVisible = %EdtWindowTextSlowVisible%%WindowControlText%`r`n
} Else
{ ; Hidden text.
If text_is_fast
EdtWindowTextFastHidden = %EdtWindowTextFastHidden%%WindowControlText%`r`n
Else
EdtWindowTextSlowHidden = %EdtWindowTextSlowHidden%%WindowControlText%`r`n
}
}
}
;EdtWindowTextFastVisibleFull := ShowOnlyAPartInGui("EdtWindowTextFastVisible", EdtWindowTextFastVisible, 400)
;EdtWindowTextSlowVisibleFull := ShowOnlyAPartInGui("EdtWindowTextSlowVisible", EdtWindowTextSlowVisible, 400)
;EdtWindowTextFastHiddenFull := ShowOnlyAPartInGui("EdtWindowTextFastHidden", EdtWindowTextFastHidden, 400)
;EdtWindowTextSlowHiddenFull := ShowOnlyAPartInGui("EdtWindowTextSlowHidden", EdtWindowTextSlowHidden, 400)
Return
}

There is an autohotkey script that emulates most of the window spy logic. It is called AHK_Window_Info_v1.7.ahk. The nice thing is... you can run it to see if your second window text if visible to this script and if so... the logic needed to pull the information is available inside the script. Here is a link to the webpage and the script is available through SKANs dropbox link on that page. http://www.autohotkey.com/board/topic/8204-ahk-window-info-17/

Related

Detecting Joystick buttons

I have this simple script where if a button is pressed I display a message. Pretty simple script but even though I pressing the right button nothing shows up on my screen.
Joy1::
MsgBox, Joy1
Joy2::
MsgBox, Joy2
Joy3::
MsgBox, Joy3
Joy4::
MsgBox, Joy4
Joy5::
MsgBox, Joy5
Here is a proof that I'm actually pressing the buttons, in this case 1 and 2.
Looks like its due to AutoHotkey using an old API to detect the keys, at least with windows 10, reference. It does detect them if an AutoHotkey window is active, like when you right click on the AutoHotkey icon and select 'Open'. I was also able to cook something up with the xinput library by Lexikos, and some posts from that forum topic, the below makes a MsgBox when 'a' is pressed on my controller(xbox one).
#Include path to xinput.ahk
SetTimer, checkForJoy1, 100
checkForJoy1:
XInput_Init()
state := Xinput_GetState(0)
if(state) {
a_pressed := state.wButtons & 4096
if(a_pressed) {
Msgbox, Does this work [%a_pressed%]
}
}
return

Automatic translation

im really new to all this and i was trying to make an Autohotkey for translation. i was digging for some time looking for examples that only confused me more, even if the code looked simple, i didn't understand half of it.
So, what I'm trying to do is: select a paragraph and replace it automatically with its translation.
i was hooping it to be somenthing as simple as CTRJ + C, Translate, CTRL + V, but i can't find the command to go to google translate or somenthing similar, it's not on the autohotkey help file so i'm guessing i don't have libraries?
I'm at my wits end, please help.
You came to the right place. Check out AutoHotKey for sure.
First, how to do it by hand? Those are the steps for ahk. So, lets say you have a paragraph of text selected. You will hit the ahk shortcut and that shortcut will:
first ahk figures out what window its in (using WinGetActiveTitle) and then sends the keystrokes Ctrl+c to copy the selection ("send, ^c" and "Clipwait"), then
ahk can access the clipboard containing the text, do a string manipulation or regex to replace all spaces with the html escape sequence %20 (eg, transtext := StrReplace(Clipboard, " ", "%20")) and
construct a URL to do the Google Translate, something like (where sl is source language and tl is translation language, and text is what you want translated): transurl := "https://translate.google.com/#view=home&op=translate&sl=en&tl=es&text=" . transtext
AHK runs that url and opens a browser window showing result (run % transurl).
This part sucks. Now, you need to use a mouse click at a location (or maybe you can find a controlsend or a combination of keystrokes moving the cursor with tabs and such) to land on the "Copy translation" button. Or how bout you do it manually (try sleep, 8000 to wait while you hit the button)
then have ahk close the window (optionally, or you just do it by hand during the sleep time) and
ahk switches back to the application with the original selected paragraph (WinActivate or do it yourself) and
send ctrl+v to paste the translated text over the original (send ^v).
A starter pack of AHK code (edited per user comments):
WinGetActiveTitle, activewin
Clipboard =
SendInput, ^c
ClipWait
transtext := StrReplace(Clipboard, " ", "%20")
transurl := "https://translate.google.com/#view=home&op=translate&sl=en&tl=es&text=" . transtext
Run, % transurl
Sleep, 6000 ; adjust to taste.
SendEvent, {tab 10} ; adjust to taste.
Sleep 1000
SendInput, {enter}
Sleep, 1000
SendInput, ^{F4}
WinActivate, activewin
sleep, 1000
SendInput, ^v
Try it and let us know how else to help.
OKOK, first of all, thank you all, the script works just fine now. I'm able to copy, translate and paste any text now. Only a few questions lingering.
1) i'm not sure i get what the step number 5 is suppose to do. whatever it is, it works so i don't touch it.
2) is there a way to reset google.translate so it dosent open a new window every time? that could save a lot of time.
3) this one doesn't have a chance, but i ask anyway. Is there a way to not open google chrome at all? because i know that u can translate from excel automatically. (i know that if it is possible will be super hard)
This is the code i ended with:
^a::
clipboard := ""
sendinput, ^c
ClipWait [,,Waitforanydata]
transtext := StrReplace(Clipboard, " ", "%20")
transurl := "https://translate.google.com/#view=home&op=translate&sl=en&tl=es&text=" .
transtext
run % transurl
Sleep, 4000
SendEvent, {tab 9}
SendEvent, {enter}
Winactivate, NAME.pdf - PROGRAM
sendinput, ^v

Autohotkey: task to copy from browser and paste on text editor

I'm trying to build a script that uses both a browser and a texteditor. The workflow I can summarize as follows:
Right click on a video for streaming
Click on the option to copy the redirection link
Switch to a text editor (Slickedit in my case)
Paste the copied link
Go back to the browser and await next command.
I want to automate this with a single key press while I am standing on the link with the mouse. This has been
my attempt:
^+!a::
Click Right, 392, 64 ;execute in browser
Click Left, 410, 79 ;
Send, !{Tab} ;switch to text editor
Send, ^V ;paste in text editor
Send, !{Tab} ;switch back to browser
return
The script isn't working correctly because it appears not to be executing the ^V command.
I suspect it's because it is executing it before Slickedit is even active. How can I syncronize
these KeyPreses so that they are executed at the right times? Also is there a nicer way for me to
switch to Slickedit without relying on the alt-tab?
There are a few things you could use to make your script better. WinActivate, clipboard, and improved mouse movement seem to be good ones to add.
^+!a::
clipboard = ; clears clipboard
Click Right ; execute in browser
MouseMove, 18, 15, 50, R ; Moves mouse relative to start location
Click Left
ClipWait, 2 ; Waits 2 seconds for clipboard to contain something
WinActivate, Slickedit ; Switch to text editor
WinWaitActive, Slickedit
Send % clipboard ; paste in text editor
WinActivate, ahk_class Chrome_WidgetWin_1 ; or your browser of choice
Return
Use the included Window Spy to find the correct Window titles or classes to be used in the WinActivate commands.

can somebody help to write a autohotkey script for select all the pasted content after paste?

the autohotkey script should do follows:
after I tap the hotkey,
it will paste the content from the clipboard, then immediately select all the pasted content?
I write a script as follows:
^+p::
clipvar:=Clipboard
num:=strlen(clipvar)
send ^v
send +{left %num%}
return
This script works.
But the selecting process is too slow!!!
Can somebody write a better script?
SendMode, Input optionally combined with SetBatchLines, -1 and variations of SetKeyDelay can accelerate key sequences.
However, the selection of large texts will still take some time, and slow machines may slow it down even further.
Here's another approach which - in terms of sending keystrokes - is more efficient:
^+p::
oldCaretX := A_CaretX
oldCaretY := A_CaretY
Send, ^v
WaitForCaretChange()
MouseGetPos, mX, mY
MouseClickDrag, Left, %A_CaretX%, %A_CaretY%, %oldCaretX%, %oldCaretY%
MouseMove, %mX%, %mY%
return
WaitForCaretChange() {
oldCaretX := A_CaretX
oldCaretY := A_CaretY
while(A_CaretX = oldCaretX && A_CaretY = oldCaretY) {
Sleep, 15
}
}
This code relies on the window to expose the caret position, which unfortunately, not every window does. It remembers the caret position before the paste and selects text up to the old position after pasting new text; this should be equal to selecting all the newly inserted text. If you're only working with editors that expose their caret position, I recommend you go with this one since it's faster. Otherwise, you can still think about using both your method and this one, depending on the window and/or the text length.

Is it possible to create a macro like that with AHK?

I'm something like a GM in a MMORPG game. Our job is reporting people who using cheat and sending them to jail. But leaving that jail zone is not really hard so we have to send them again and again. I have a loooong nickname list (I have about 400 nicknames to report repeatly) so It's really boring.
What i wanna ask is, I don't know anything about AHK. If that kind of macro is possible, I'll do a loooong research to create that macro. But if It's not possible, I'm not even gonna try.
What i need is; The Macro will press "enter" to activate chat mode. Then will write "/report -cheater nickname-" and remember there's 400+ nicknames exist so I need to repeat the macro for different nicknames. After it write "/report -cheater nickname-" Macro will press enter. Then a little chat box will pop-up. Macro will click to the box, will write the report reason, then click confirm. then another chat box will pop-up to say something like "your report is received." And macro will click to confirm for that too. And will do it for 400+ nicknames with 400+ different reasons. Is that actually possible to do? Just wondering that. Not asking you to creating this macro. If you answer that, I'll try to make it myself :D
Thanks.
This script is to perform a series of searches on Google. The search strings are stored in a text file and read into an array, they are then executed one by one, based on hitting the {Tab} key (you can make this repeat automatically).
When the script is interrupted, you can start it again and give it a (new) starting number, or tell it to start from 1 again.
Not exactly what you were looking for, but it gives you a lot of starting points.
#Persistent
#SingleInstance Force
#installKeybdHook
SetWorkingDir %A_ScriptDir%
TempDir = C:\Temp
Menu, Tray, Icon , %A_AhkPath%, 2, 1
TrayTip, JobSearch, Started, 1
SetTitleMatchMode, 2
TextCounter = 0
Return
+Launch_App1::
Run, Notepad %TempDir%\Google.txt
Return
Launch_App1:: ; vacatures Job Search
+CapsLock::
Restart:
MouseGetPos, XPos2, YPos2
XPos3 := 50
YPos3 := 100
IniRead, TextCounter, %TempDir%\GoogleCounter.ini, Counter, Nr
ArrayCount = 0
Loop, Read, %TempDir%\Google.txt ; This loop retrieves each line from the file, one at a time.
{
ArrayCount += 1 ; Keep track of how many items are in the array.
Array%ArrayCount% := A_LoopReadLine ; Store this line in the next array element.
}
MaxSearchCount = %ArrayCount%
TextCounter += 1
If (TextCounter > 1)
InputBox, TextCounter , Start, Number (1..%MaxSearchCount%),,,,,,,10,%TextCounter% ; InputBox, OutputVar [, Title, Prompt, HIDE, Width, Height, X, Y, Font, Timeout, Default]
TextCounter += 0
IniWrite, %TextCounter%, %TempDir%\GoogleCounter.ini, Counter, Nr
SearchText:=Array%TextCounter%
MouseClick, left
gosub, SendNewSearch
Return
;=======================================================================================================================================
Browser_Favorites:: ; Search for next Vacature string (Vacatures)
CapsLock::
If (TextCounter = 0) ; Restart with previous script if Textcounter is set to 0
{
GoSub, Restart
Exit
}
IniRead, TextCounter, %TempDir%\GoogleCounter.ini, Counter, Nr
TextCounter += 1
IniWrite, %TextCounter%, %TempDir%\GoogleCounter.ini, Counter, Nr
SearchText:=Array%TextCounter%
If (SearchText = "")
{
TextCounter := 0
IniWrite, %TextCounter%, %TempDir%\GoogleCounter.ini, Counter, Nr
Send, ^{F4}
SplashTextOff
ExitApp
}
Sleep, 200
Send, {Home 2}
Sleep, 700
Send, {WheelUp 10}
Sleep, 400
gosub, SendNewSearch
Exit
SendNewSearch:
MouseGetPos, XPos3 ,YPos3
SetTitleMatchMode, 2
IfWinActive, Chrome
{
while (A_Cursor = "AppStarting")
Sleep, 200 ; Continue
Sleep, 100
SplashTextOff
MouseClick, left, %XPos2%,%YPos2%
WinGetTitle, this_title, A
IfInString, this_title, Google
{
Send, {Home}+{End}{DEL}%SearchText%{Enter}
}
ToolTip, Waiting....
DisplayText = Nr%TextCounter% %SearchText%
Sleep, 500
SplashTextOn, 200, 0,%DisplayText%
WinMove, %DisplayText%, , 800, 25
ToolTip
;MouseMove,(50),(500)
MouseMove,%XPos3%,%YPos3%
ClipBoard = %SearchText%
}
Exit
Exit
+Browser_Favorites::
run, %TempDir%\Google.txt
Return
It is possible to do. You can create two txt files which have list of +400 user names and +400 different reasons. Macro can read lines one by one and can makes all things, what you want, more than 400 times.
You will need this loop for writing lines from txt file to an array, a loop in a function for checking expected color at the specified pixel with PixelGetColor (http://www.autohotkey.com/docs/commands/PixelGetColor.htm) for detecting buttons. You may also use PixelGetColor command or AutoIt3 Window Spy, which will be installed with autohotkey, to see colors of buttons. Finally you can start to code from here (http://www.autohotkey.com/docs/).
PS. Sorry, site did not allow me to use more than 2 hyperlinks.
Basically you are trying to write a script that types enter, then a list of characters then enter again?
Something quite simple you could do would be to create a .txt file that includes everything you want it to type (excluding enters, except between lines), and create a macro like this:
#n::
Loop, Read, inputFile.txt
{
Send {Enter}%A_LoopReadLine%{Enter}
}
return
Basically you run the macro and open the game to the point that you can start typing enter, character information, enter, but press the windows key and the 'n' key. The macro would then loop through each line of 'inputFile.txt' and stimulate typing an enter, the line, and then an enter.