Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Incrementing A Number In Loop?
#1
I've created a macros that successfully targets an already-opened app, then pauses for one minute (60 seconds) before what I'm hoping is a loop to occur:
 
Code:
Copy      Help
int w1=act(win("Untitled - Notepad" "Notepad"))
lef 167 72 w1 1 ;;editable text
'"word0001"           ;; "word0001"
wait 60

Can anyone enlighten me on how to not only loop the script , but also make "0001" increase by one on each loop until say "9999"? Much appreciated.
#2
Macro Macro33
Code:
Copy      Help
int w=win("Untitled - Notepad" "Notepad")
act id(15 w) ;;editable text 'Text Editor'
str sBlock
for _i 1 10000
,_s.format("%04i" _i)
,key F"word{_s}[]";;[] adds new line; remove if you don't want

above slow and dangerous unless you block key and mouse input which could change focus - see BlockInput2, though this can also be tricky and sometimes dangerous to use.

This better:
 
Code:
Copy      Help
str sColl
for _i 1 10000
,_s.format("%04i" _i)
,sColl.addline(F"word{_s}")    
int w=win("Untitled - Notepad" "Notepad")
sColl.setwintext(id(15 w)) ;;editable text 'Text Editor'

tip: learn QM faster and more easily by using the 'wizards' in the toolbar, especially useful are the ones that put arrows on (though all eventually become lifesavers! thanks Gintaras!!!!)

   
#3
Hope the this helps. This is probably easier understand, but longer code.
The function (SetEndThreadHotkey), will allow you to terminate the thread with any Function key. default "F12"

Function Increment_Num_Notepad
Code:
Copy      Help
;// Increment_Num_Notepad \\
;;;;;2022-04-28  Thursday  -  07.26.26
int i=1
str s s1 sw
str wd = "word"
SetEndThreadHotkey "F12"    ;;  «--‹‹    ≡KILL SWITCH≡  ---     Change to any "Fxx" Function Key.

;;;;; Loop
if i < 9
,s1 = "000"
if i > 9
,s1 = "00"
if i > 99
,s1 = "0"
if i > 9999
,goto Bottom

s=i ;; <--- Convert incremented number to string, for output in notepad.
sw.from(wd s1 s) ;; <--- Combine: "word" + "0xx" + "x" = Eg.   word+000+1

int w1=act(win("Untitled - Notepad" "Notepad")) ;; <--- This will activate Notepad evry loop.
;;;-->   lef 167 72 w1 1 ;;editable text ;; <--- Remarked so won't steal the mouse.  
outp (sw); 'Y ;; <--- Output string in Notepad and Enter to new line.
wait 60    ;; <--- Wait a Minute.
i + 1         ;; <--- Increment number x1.
goto Loop ;; <--- Start Loop again.

;;;;; Bottom
;End Function

Function SetEndThreadHotkey
Code:
Copy      Help
;/

function $hotkey

;Sets a hotkey to end current thread (running macro or function).

;hotkey - a single key without modifiers (Ctrl etc), specified as QM key code like with <help>key</help> and QM keyboard triggers.

;EXAMPLE
;SetEndThreadHotkey "F8"
;;macro
;rep
,;out 1
,;wait 1


int mod vk; if(!TO_HotkeyFromQmKeys(hotkey mod vk)) end "invalid hotkey string"
if(mod) end "modifier keys not supported"

QMTHREAD qt; GetQmThreadInfo(0 qt)
mac "sub.Thread" "" qt.threadhandle vk


#sub Thread
function ht vk

int- __ht82657(ht) __vk82657(vk)

int hh=SetWindowsHookEx(WH_KEYBOARD_LL &sub.Hook_WH_KEYBOARD_LL _hinst 0)
opt waitmsg 1
wait 0 H ht
UnhookWindowsHookEx hh


#sub Hook_WH_KEYBOARD_LL
function# nCode message KBDLLHOOKSTRUCT&k
if(nCode<0) goto gNext
if(k.flags&(LLKHF_UP|LLKHF_INJECTED)) goto gNext

int- __ht82657 __vk82657
if k.vkCode=__vk82657
,EndThread "" __ht82657
,ret 1

;gNext
ret CallNextHookEx(0 nCode message &k)


Forum Jump:


Users browsing this thread: 1 Guest(s)