Series of actions in one script, successively - autohotkey

AutoHotKey.
Leraning how to make a series of actions, with strings, files, variables etc. From 1 to 101 of them. Files in one folder or folders in one folder, strings from high, variables in one script. By more simple and classical methods.

If you want to do multiple similar actions it's almost always a good idea to use some form of loop.
Arrays can also help a lot.
Here's a file/folder loop:
Loop Files, C:\*.exe, R ; get all .exe files that are on C:\ and subfolders of it
{
MsgBox, Full path of the current file: %A_LoopFileFullPath%
If (A_LoopFileName = "virus.exe") {
MsgBox, A file called virus.exe was found.
}
}
Here's a normal loop:
Loop, 101 ;run the following code 101 times
{
MsgBox, This is iteration number %A_Index%
If (A_Index = 10)
MsgBox, Nice! You made it through 10 iterations!
}
...
Loop, 101 ;run the following code 101 times
{
If (A_Index >= 10 && A_Index < 20)
MsgBox, This is iteration number %A_Index%. (10-20)
If (A_Index >= 37 && A_Index < 71)
{
MsgBox, This is iteration number %A_Index%. (37-71)
}
}

Related

Autohotkey 3 clicks = volume mute

In autohotkey im trying to make it so that when I press the left mouse button 3 times with a delay of +/- 10 ms it becomes a volume mute
LButton::
if (?)
{
Send, Volume_Mute
}
else
{
Send, LButton
}
Return
Use A_TickCount to read current time in milliseconds and then calculate the delay between clicks. See Date and Time
ms := A_TickCount
N := 3 ; number of clicks
T := 500 ; max delay between clicks, ms
clicks := 0
~lbutton::
msx := A_TickCount ; get current time
d := msx - ms ; get time past
ms := msx ; remember current time
if (d < T)
clicks += 1
else
clicks := 1
if (clicks >= N)
{
; tooltip %N%-click detected
send {Volume_Mute}
clicks := 0
}
return
Each Autohotkey Script (example.Ahk) that you will run in a loop (running in the background), these loops will repeating in a count off frequence ?...ms (milliseconds)
If you want to use a delay from +- 10ms you will need to change the Timer. (Default = +-250ms)
With the Autohotkey Command (SetTimer) you can change that.
(ps- +-10 ms is very fast i recommend to use a lower Time frequence)
In the Line (SetTimer, CountClicks, 100) you can change(optimize) the number 100. (so that it works fine on your system.)
Note: you can remove the line (msgbox) this is only to show visual how many times you did click.
Try this code:
#NoEnv
#SingleInstance force
;#NoTrayIcon
a1 := -1
b1 := 0
esc::exitapp ;You can click the (esc) key to stop the script.
;if you use ~ it will also use the default function Left-Button-Click.
;and if you Click the Left Mouse Button 3x times, it will Execute Ahk Code Part 3
~LButton::
if(a1 = -1)
{
a1 := 4
#Persistent
SetTimer, CountClicks, 100
}
else
{
a1 := 3
}
return
CountClicks:
if(a1 = 3)
{
b1 := b1 + 1
}
if(a1 = 0)
{
msgbox you did Click <LButton> Key > %b1%x times
if (b1=1)
{
;if Click 1x - Then Execute Ahk Code Part 1
;Here you can put any code for Part 1
}
if (b1=2)
{
;if Click 2x - Then Execute Ahk Code Part 2
;Here you can put any code for Part 2
}
if (b1=3)
{
;if Click 3x - Then Execute Ahk Code Part 3
;Here you can put any code for Part 3
Send {Volume_Mute} ;Send, Volume_Mute
}
if (b1=4)
{
;if Click 4x - Then Execute Ahk Code Part 4
;Here you can put any code for Part 4
}
b1 := 0
SetTimer, CountClicks , off
reload ; restart script
}
a1 := a1 - 1
return
I did test it out on a Windows 10 System and it works.

Autohotkey : skip comment lines in Loop / FileReadLine

i read a config.ini with a location of some .exe to run, but i want to skip a comment line from my config.ini (cause i want to explain how to use the file), if someone can help me thanks
#NoEnv
#SingleInstance force
Loop
{
FileReadLine, exe, config.ini, %A_Index%
if ErrorLevel
break
Run %exe%
Sleep , 300
}
return
ExitApp
config.ini
// Put here location of your programs << line to skip
// Thanks << line to skip
C:\WINDOWS\notepad.exe
C:\WINDOWS\****.exe
...
Loop
{
FileReadLine, exe, config.ini, %A_Index%
if ErrorLevel
break
else if SubStr(exe,1,2)=="//"
continue
Run %exe%
Sleep , 300
}
Using SubStr() check if first two chars in line are // if so use continue to skip rest of loop and start next iteration.

How do I condense multiple similar hotkeys into one in AutoHotkey?

I'm trying to create hotkeys to simulate input buffering in an online game I'm playing which doesn't natively support input buffering, meaning mashing a spell key so that it goes off after the previous spell is finished casting is the best method to manually do.
With the help of a hotkey I can loop the key press with minimal delay by holding down my key so that it sends the instant I'm done casting the previous spell.
However creating multiple hotkeys for each button I have bound on my keyboard to a spell seems tedious and I'm not sure how to condense these hotkeys into one using an array of defined keys (ie. 1 through 6, and F1 through F6)
An example snippet of my code so far with only 3 keys taken into account:
$5::
{
Loop
{
Loop, 5
{
Send, 5
Sleep, 1
}
GetKeyState, state, 5
if state = U
break
}
return
}
$2::
{
Loop
{
Loop, 5
{
Send, 2
Sleep, 1
}
GetKeyState, state, 2
if state = U
break
}
return
}
$F2::
{
Loop
{
Loop, 5
{
Send, {F2}
Sleep, 1
}
GetKeyState, state, F2
if state = U
break
}
return
}
I'm trying to condense it to something like this, if possible:
hotkeys := [5, 2, F2]
hotkeyCount := hotkeys.MaxIndex()
curKey := 1
Loop, hotkeyCount
{
hotkeyIndex := hotkeys[curKey]
$%hotkeyIndex%::
{
Loop
{
Loop, 5
{
Send, {%hotkeyIndex%}
Sleep, 1
}
GetKeyState, state, %hotkeyIndex%
if state = U
break
}
return
}
curKey := curKey + 1
}
Create a FIFO stack that will safe the preset actions and call them when they are ready.
Array functions contains the functions: Function_a, Function_b, Function_c, that are triggered with their respective hotkeys, a, b, c.
The hotkeys don't call the functions directly, but add their numerical index the to the stack stack.
The timer check, retrieves the numerical index from the stack, then the function from the array functions at that index is called. When the function returns, the next index is retrieved if there is any. Only one functions is running at a time.
SetBatchLines, -1
global stack := Object()
global stack_head = 0
global stack_tail = 0
global functions := [Func("Function_a"),Func("Function_b"),Func("Function_c")]
SetTimer, check , 25
return
check:
if( stack_head > stack_tail )
{
i := stack[stack_tail]
functions[i]()
stack_tail++
}
return
Function_a()
{
tooltip, Function_a running...
Sleep, 1000
tooltip,
return
}
Function_b()
{
tooltip, Function_b running...
Sleep, 1000
tooltip,
return
}
Function_c()
{
tooltip, Function_c running...
Sleep, 1000
return
}
a::
stack[stack_head] := 1
stack_head++
return
s::
stack[stack_head] := 2
stack_head++
return
d::
stack[stack_head] := 3
stack_head++
return
This enables concurrent running of the functions, that can do whatever you want, while at the same time hotkeys can add actions (functions indexes) to the stack, which will be called in order they were added one at a time.
I have edited you example to make it functional:
$a::
$s::
$d::
$1::
key := A_ThisHotkey
key :=Trim(key,"$")
Loop
{
Loop, 5
{
SendInput, %key%
Sleep, 1
}
state := GetKeyState(key , "P" )
if state = 0
{
break
}
}
return

SendEvent ^{ins} isn't copying content to the clipboard

!c::
file_name = footnote.ini
restore_original_clipBoard := clipboard
clipboard =
KeyWait, Alt
KeyWait, c ;small c
BlockInput, on
SendEvent, ^{ins} ;^c doesn't work
ClipWait, 2 ; Wait for the clipboard to contain text.
if ErrorLevel
{
MsgBox Failed to save the selection: %clipboard%
exit
}
BlockInput, off
save_selection := clipboard
Problem: Despite a selection being made, Sendevent ^{ins} does not save it to the clipboard. Sometimes I have to repeat my hotkey, alt + c several times before the selection is being copied to the clipboard. The KeyWait should ensure me that only ^{ins} is being processed without any additional keys. What am I doing wrong here?
UPDATE
One of the ways I tried to force copy a selection to the clipboard was by using a while loop. I got it to work through the post: Looping clipboard and errorlevel evaluation not working as expected
PROBLEM
When I make a selection and press alt + c it sometimes gets stuck in the infinite loop that I implemented. But as you can see from that code:
clipboard := ""
while( StrLen(clipboard) < 1 )
{
Send, ^{ins}
Sleep, 50
}
MsgBox % ClipBoard
The infinite loop incorporates within itself a continues resending of ^{ins}. For some reason, my selection is not being recognized as a selection. Whilst it is in that infinite loop, I try to reselect the text. It then recognizes it instantly and copies my selection to the clipboard. But alas! The selection is incomplete because it goes so quick.
This problem is not always like that. Sometimes it recognizes the selection first spot on! So sometimes it copies my selection to my clipboard sometimes not. When it does not, then a resending of a ^{ins} does not seem to work. I do not want to the user to reselect his selection. Is that possible to do?
Send {Ctrl Down}{c}{Ctrl Up}
That presses Ctrl+C, you must do it instantly as one command apposed to pressing Ctrl waiting then pressing C.
Never seen Insert key used for copying text.
Also found this sends Ctrl+C as well.
Send, ^c
To send insert key use
{Insert}
This way works for me:
!vk43:: ; alt+c
clipContent:=ClipboardAll
Clipboard:=""
SendEvent, ^{Ins}
ClipWait, .75
MsgBox, % 262 . (ErrorLevel ? 160:208)
, % ErrorLevel ? "Period expired:":"Result:"
, % ErrorLevel ? "Failed to save the selection.":Clipboard
, % (ErrorLevel ? 0:2) . .5
Clipboard:=clipContent
KeyWait, vk43
Return
!vk43:: ; alt+c
clipContent:=ClipboardAll ; backup clipboard content (if needed)
Clipboard:="" ; no comment :)
Loop
{
SendEvent, ^{Ins}
ClipWait, .75 ; means 750ms, same if write 0.75
; assign value of "ErrorLevel" an variable for further usage
errLvl:=ErrorLevel
; monitoring current action (for debugging purpose only)
TrayTip, % "attempt: #"A_Index
, % """ErrorLevel"" of ""ClipWait"" command is: "errLvl
}
; here you can set the condition of ending the cycle: either...
; ...variable errLvl has not a true value,...
; ...or the number of attempts is equal 5
Until, Not errLvl Or A_Index=5
; value of each field of the command "MsgBox"...
; ...are set depending on the value of errLvl variable...
; ...using a ternary expression
; means if errLvl is a true, "options" field is 262160
MsgBox, % 262 . (errLvl ? 160:208)
; means that "title" has a couple variants
, % errLvl ? "Period expired:":"Result:"
; means that "text" has a couple variants
, % errLvl ? "Failed to save the selection.":Clipboard
; means if errLvl is a true, "timeout" field is 0.5 (500ms)
, % (errLvl ? 0:2) . .5
/* same that and above:
IfEqual, errLvl, % True, MsgBox, 262160
, % "Period expired:"
, % "Failed to save the selection."
, 0.5
Else MsgBox, 262208, % "Result:", % Clipboard, 2.5
*/
TrayTip ; remove "TrayTip" (for debugging purpose only)
; save an positive result (if needed)
IfEqual, errLvl, 0, Sleep, -1, someVar:=Clipboard
; return a temporarily saved data into clipboard (if needed)
Clipboard:=clipContent
KeyWait, % "vk43"
Return
From my experience whenever keystrokes are not recognized reliably it's due to either the system or the targeted program not keeping up with the speed at which those keys are sent.
For SendEvent you could try something like SetKeyDelay, 1000, 1000 and see if this improves things. The other option would be to send explicit down and up keys with intermittent sleep calls as outlined in this answer.

Perl: Devel::Gladiator module and memory management

I have a perl script that needs to run in the background constantly. It consists of several .pm module files and a main .pl file. What the program does is to periodically gather some data, do some computation, and finally update the result recorded in a file.
All the critical data structures are declared in the .pl file with our, and there's no package variable declared in any .pm file.
I used the function arena_table() in the Devel::Gladiator module to produce some information about the arena in the main loop, and found that the SVs of type SCALAR and GLOB are increasing slowly, resulting in a gradual increase in the memory usage.
The output of arena_table (I reformat them, omitting the title. after a long enough period, only the first two number is increasing):
2013-05-17#11:24:34 36235 3924 3661 3642 3376 2401 720 201 27 23 18 13 13 10 2 2 1 1 1 1 1 1 1 1 1 1
After running for some time:
2013-05-17#12:05:10 50702 46169 36910 4151 3995 3924 2401 720 274 201 26 23 18 13 13 10 2 2 1 1 1 1 1 1 1 1 1
The main loop is something like:
our %hash1 = ();
our %hash2 = ();
# and some more package variables ...
# all are hashes
do {
my $nowtime = time();
collect_data($nowtime);
if (calculate() == 1) {
update();
}
sleep 1;
get_mem_objects(); # calls arena_table()
} while (1);
Except get_mem_objects, other functions will operate on the global hashes declared by our. In update, the program will do some log rotation, the code is like:
sub rotate_history() {
my $i = $main::HISTORY{'count'};
if ($i == $main::CONFIG{'times'}{'total'}) {
for ($i--; $i >= 1; $i--) {
$main::HISTORY{'data'}{$i} = dclone($main::HISTORY{'data'}{$i-1});
}
} else {
for (; $i >= 1; $i--) {
$main::HISTORY{'data'}{$i} = dclone($main::HISTORY{'data'}{$i-1});
}
}
$main::HISTORY{'data'}{'0'} = dclone(\%main::COLLECT);
if ($main::HISTORY{'count'} < $main::CONFIG{'times'}{'total'}) {
$main::HISTORY{'count'}++;
}
}
If I comment the calls to this function, in the final report given by Devel::Gladiator, only the SVs of type SCALAR is increasing, the number of GLOBs will finally enter a stable state. I doubt the dclone may cause the problem here.
My questions are,
what exactly does the information given by that module mean? The statements in the perldoc is a little vague for a perl newbie like me.
And, what are the common skills to lower the memory usage of long-running perl scripts?
I know that package variables are stored in the arena, but how about the lexical variables? How are the memory consumed by them managed?