Trying to make a script that takes first word from a txt file, writes it, repeat till all words written - autohotkey

I have a text file, it has several lines. 1 line = 1 word
Now I want a script, if I press a specific button it will write the first word (first line). If I press the button again, the next word will be written, it goes like that till all words are written. I have tried to code it but it will only write the first word and will keep writing it.. It won't take next word and I don't know why because actually I have made a variable that increases...
Here is my code:
F11::
Index = 1
Array := Object()
Loop, Read, C:\Users\Emilia\Desktop\list.txt
{
Array.Push(A_LoopReadLine)
}
Send % Array[Index]
Index++

Use FileReadLine with a line number parameter
Keep the index variable outside in the global context so it doesn't get reset to 1 each time
Use SendPlay to send the word at once, not character-by-character.
You can switch the default send mode with SendMode play command.
Send Enter key at the end by adding {Enter} in the send command.
index = 1
F11::
FileReadLine line, C:\Users\Emilia\Desktop\list.txt, index
SendPlay %line%{Enter}
index++
Return

Related

How to get ExitApp to take effect immediately in AHK?

If I run the following code, if I hit the ^+q it does not stop entering the numbers 1-100. Only after it completes does the script exit. Is there a way to get the script to stop even if it is in the middle of sending keystrokes?
^j::
ArrayCount := 100
Loop % ArrayCount
{
Send, %A_index%
}
return
^+q::ExitApp ; Exit script with Escape key
There are 2 issues with your code.
Sendreleases modifier keys when it simulates input, using it in a loop this way is going to interfere with autohotkey's hotkey detection. You can still activate ^+q if you press the 3 buttons simultaneously but it's much easier to use a hotkey without modifiers for example the Escape key. This is also what your comment says you're doing
^+q::ExitApp ; Exit script with Escape key
so as a bonus it will fix the discrepancy between your comment and your code ;).
The second problem is that the loop in which you execute the Send command is going to finish very quickly if you use SendInput and by the time ExitApp is executed all the numbers were already sent(even if you don't yet see the effect). In case of SendEvent there is some other problem which prevents other threads from being executed when you do it in a loop(don't know what causes it, might be a bug).
To solve it you need to add Sleep. At my system doing Sleep 1 works well. You can experiment with different numbers and send modes until you get the desired effect(you can also try 0 and -1.
Full code:
^j::
ArrayCount := 100
Loop % ArrayCount
{
Send %A_index%
Sleep 1 ; experiment with how long to sleep
}
return
Escape::ExitApp ; Exit script with Escape key

Autohotkey: How to correctly write text, enclosed within brackets, to text files?

I need Autohotkey (AHK) to insert text, enclosed within brackets, into my text files. However, my AHK code results in erroneous text input depending on using the code on text files opened in Notepad3 or opened in Emacs. My use of brackets are obviously wrong relative to the AHK syntax.
My minimum working example:
::ttt::
; define variables
myString = abcdf
myFile = C:\Users\myUser\notes\myFile.tex
; write variables into current file
SendInput, <<%myString%>> {enter}
SendInput, [[%myFile%]]
return
The results I need should look like this:
<< abcde>>
[[C:\Users\myUser\notes\myFile.tex]]
Emacs When using this script on a text file opened with Emacs, the result looks like this:
<< abcde>>
[[C:\Users\myUser\notes\myFile.tex]]]
The first line written the way I need it, while the second line has got an extra "]" added in the end.
Notepad3 When using this script on a text file opened with Notepad3, the result looks like this:
<< abcde>>
[[C:\Users\myUser\notes\myFile.tex]]< /abcde>
The first line is written the way I need it, while the second line has got a variant of the first line added in the end, though with an "/" added.
How can I modify my code so the text input will look correct both in Notpad3 and in Emacs?
Is the space in << abcde>> necessary? The definition of your variable doesn't seem to reflect that.
Nonetheless, the issue likely stems from the fact that these keys are being inputted instead of sending the text directly. I find that it's usually best to send strings as text if you wish to avoid other hotkeys from potentially disturbing your output. Try this adjustment:
SendInput {text}<< %myString%>>
SendInput {enter}
SendInput {text}[[%myFile%]]

Autohotkey Input Times Out Because MatchList Isn't Matched

Here's my AHK script
:*:if ::
SendInput IF{Space}
Input cond, I V T5,, then
msgbox %ErrorLevel%
msgbox %cond%
if (ErrorLevel = "Match")
{
SendInput {Enter}End If{Space}'%cond%
}
Return
When I type If x = 1 then I get an ErrorLevel of 'Timeout' and a cond of 'x = 1 then'
My understanding is that when I type then it's supposed to stop the Input and set the ErrorLevel to Match.
I've tried putting it in quotes, using single letters in the MatchList, and including and end key, but none of works. The few examples I could find of using MathList look just like mine.
Input cond, I V T5 *,,% " then"
Without the asterisk option, an item in MatchList needs to be the next thing typed. So If Then would match then in the MatchList. But If anything then doesn't match then in the MatchList because it's trying to match anything then, not just then.
With the asterisk option, if anything then is matched because the next thing typed after If contained then. That causes problems if you type if heathen then because the then in heathen triggers it. The space before then should fix that, but it doesn't.
AHK help says that the MatchList respects spaces, but it doesn't seem to do that when the space is before the first (or only) item in MatchList. The two ways around that are to include a dummy word at the beginning or to use the % thingy to quote the MathList item and include the space.
See also http://ahkscript.org/boards/viewtopic.php?f=5&t=3979
I strongly recommend using RegEx powered hotstrings for this purpose:
#Include <Hotstrings>
hotstrings("if (.*?) then", "If %$1% then``nEnd If%A_SPACE%")
The definition of "T" in the AutoHotkey Help File states this:
T: Timeout (e.g. T3). The number of seconds to wait before terminating the
Input and setting ErrorLevel to the word Timeout. If the Input times out,
OutputVar will be set to whatever text the user had time to enter.
Basically, this is saying exactly what is to be expected (and what is actually happening) with your script. So, for me when I type "if x = 1 then" my "if" is immediately replaced with the caps lock IF and a space.
After 5 seconds, the first MsgBox appears with "Timeout" as the text (again, expected since ErrorLevel is set to the word "Timeout" when the "T" option is present). Once that MsgBox is dismissed, I receive the second MsgBox which (according to the help file) contains OutputVar which is what I (the user) had time to enter in before the Timeout.

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.

Selecting the last N characters before caret with AutoHotkey

Is there a way to select last N symbols with autohotkey?
I'm making a function which replicates Sublime Text's duplicate function (Ctrl+Shift+D). I want text to be selected before it is duplicated via SendInput ^C{right}^V
Technically, I could make something like:
selectBefore(n){
Loop, %n% {
SendInput +{Left}
}
}
But that has shown poor performance.
Another method would be to play with Shift+Home. For example, Send +{Home}, then count the number of symbols selected, then Send {Left} and Send +{Home} again, and so on until reaching the length of the duplicated string.
I don't see any better alternatives.
Is there a good, basic way to select N symbols before caret?
From what I read about ST2 (thank you for making me aware) is that ^+d either copies the selected text or if nothing is selected, copies the whole line.
Would this work?
TempCB = %ClipBoard% ; Park clipboard (text) content, Other content (format, images, etc.) will be lost.
ClipBoard = ; Clear clipboard
Send, ^c ; Grab selected text
Sleep, 100 ; Wait 0.1 seconds for clipboard (clipboard will not get filled if nothing is selected)
if (Clipboard = "") ; Nothing selected, thus copy whole line
{
Send, {Home}+{End}^c ; Select line and copy to clipbard
}
MoveBack := StrLen(ClipBoard)
MoveFwd := MoveBack
MoveBack++ ; Move one step back further back due to earlier step {right}
Send, {Right}{Left}^v{Right}{left %Moveback%}+{Right %MoveFwd%} ; Go to end of selected text (in MS notepad this is will jump over the first next char., thus a jump back as well), add a space and paste.
ClipBoard = %TempCB% ; Restore (text part) of previous clipboard content.
Return
I tested this in MS Notepad, other editors might behave differently (especially around jumping towards the end of the selected text).
The script now copies and pastes the selected text and highlights the newly pasted text.