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

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.

Related

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

How to repeat an ongoing script if the same input is given in autohotkey

i use this code to send more mouse wheel scrolls using one notch, the problem is when an input is sent again before the loop is finished, it is not taken in account and the loop needs to end before relaunching
I tried looking for a way to break up the code if there is an input and relaunch it with no success
WheelUp::
loop 100
{
sendInput {WheelUp}
sleep 2
}
return
Expected behavior: on the 50th loop, if input is given again, reset the loop count (stop and relaunch the script) so the total repetitions would be 150
Try this approach.
You have to change the #MaxThreadsPerHotkey setting.
This setting is used to control how many "instances" of a given hotkey
or hotstring subroutine are allowed to exist simultaneously. For
example, if a hotkey has a max of 1 and it is pressed again while its
subroutine is already running, the press will be ignored.
That will allow the hotkey to "interupt" itself.
Each time you scroll up while the hotkey is already active it will add another 100 scrolls to the loop.
#MaxThreadsPerHotkey 2
WheelUp::
if (counter > 0) ; this means the hotkey is already active
{
counter := counter + 100 ; in that case we just add another 100 scrolls to the loop
return
}
else ; this means the hotkey is not active, we start fresh with 100 scrolls
{
counter := 100
}
while, counter > 0
{
sendinput, {WheelUp}
sleep 40 ; change this to your requirement
counter--
; tooltip, % counter ; un-comment this line for testing
}
return
~WheelDown::counter := 0 ; extra hotkey to stop the loop immediately if needed
I cannot respond to the other comment but WheelUp::Send, {WheelUp 100} won't break the current 100 Wheelups and will add another 100 WheelUps. So if the input was given again on the 50th WheelUp, it would still send 200 WheelUps.
In the loop you will need a conditional statement to detect if a physical scroll up was detected recently, and then restart the loop if it does. For most keys you would use GetKeyState as the check in the if statement, however the mouse wheel has no state to detect.
In my example code, the Enter button will restart the loop. I'm not sure how to restart the loop if trying to use WheelUp again since there is no state to detect, and A_TimeSincePriorHotkey is not reliable because the loop is sending WheelUps.
WheelUp::
loop 100
{
sendInput {WheelUp}
sleep 2
if (GetKeyState("Enter", "P"))
Goto WheelUp
}
return

Parse clipboard without Loop [Autohotkey]

AHK Novice here.
I have a spreadsheet with data that is used to fill forms in an application in IE.
Perhaps there is a better way, but is it possible to parse the clipboard without a Loop function?
I've created this code to copy the selected range in excel and parse the clipboard, pasting each value down a column:
^`::
Send, ^c ; this copies the column in Excel
WinActivate, ahk_exe iexplore.exe ; activate browser
Sleep 30
Loop, parse, clipboard, `n, `r
{
Send, ^a
SendRaw, %A_LoopField%
Sleep 30
Send, {down}
Sleep 30
}
clipboard =
Return
The issue is the data cannot always be pasted down a column. Sometimes a pattern would be something along the lines of:
Send, ^c ; copy the selected data in Excel
WinActivate, ahk_exe iexplore.exe ; activate browser
Sleep 30
;paste first data item
SendInput, {down}{tab}
;paste second data item
SendInput, +{tab}{down}
;paste third data item
SendInput, {down}
;paste fourth data item
SendInput, {down}
;paste fifth data item
Also, is there a way to activate a window (excel spreadsheet) based on what the name starts with? Basically, I have several reports (where the data comes from) that, for example, start with XXX then have an identifying reference number (e.g. XYZ 31259875, XYZ 57896547). Only one of these reports would be open at a time.
Please excuse my ignorance and thank you in advance!
Perhaps use:
ComObjGet()
What you can do is get the Excel COM object, then it's just like vba:
MyWorkbook := ComObjGet("Somepath here")
row = 0
column := 1 ;Column A as an example
Loop, 50 ;Just an example for the loop
{
row:= row + 1
Cell:= MyWorkbook.Sheets(1).Cells(row, column).text
WinActivate, ahk_exe iexplore.exe
...your other events
}`
Also I'd strongly suggest using IE's COM object too, that way you can use the element's ID or something similar to get control. Example:
;Get Pointer to IE
For ie in ComObjCreate("Shell.Application").Windows
{
if (ie.LocationName="SomeWindowNameHere") and InStr(ie.FullName, "iexplore.exe")
{
break
}
}
And finally...
ie.getelementbyid("SomeElementID").value:=Cell

AutoHotKey: how to check if the copy/paste buffer has certain phrase or keyword and act upon result

After about two years from my initial question, I keep finding myself needing a screen scraping from a browser window.
The way I do this is :
Maximize the browser window with
send !{space}
sleep 500
send x
then, place the cursor close to top left corner with
mousemove, 30, 30 ; or similar
sleep 250 ; let computer catch up with mouse movement
mouseclick, L
send ^a
sleep 250
send ^c
at this point. I have the screen-scraped content in my paste buffer. All I am interested in is the text portions anyway. I can launch a notepad and paste the contents of the buffer into a text file and try searching for the the strings I am looking for, but I have gut feeling that, I can skip this temporary file creation step. Just don't know how.
And I would like to perform, few more ahk commands if the string is found in the buffer or keep waiting looping if it is not there yet.
Thanks for your contributions
Assuming you look for the string Hello World in your clipboard
an example code would be
send ^c
StringCaseSense, Off
Haystack = %Clipboard%
Needle = Hello World
IfInString, Haystack, %Needle%
{
MsgBox, The string was found.
return
}
else
{
;Do something else here
}
You an use the built in variable 'Clipboard' to read and write the clipboard contents.

How to obtain textual contents from a window

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/