Script is providing inconsistent results Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kspanks04
Posts: 3
Joined: 17 Jul 2018, 15:32

Script is providing inconsistent results

17 Jul 2018, 15:50

Hi everyone. I am working on a script to help me enter my time and activities for work. The system is obnoxious in that you have to enter one activity per page load. The form defaults with today's date, so if you don't enter your activities each day you have to change the date in the past for each day and activity you need to enter.

I typically am 2-5 months behind on this task so I spend a lot of time doing this routine: loading the page, changing the date, submitting, changing the date to what it just was, submitting, changing the date to what it just was plus 1 day, submitting, repeat...

To speed things up my code begins with a manually copied a date (i.e. Jan-01-2018). I enter my activity and the page reloads with today's date. If I need to enter another activity for Jan-01, I activate the script and it figures out if I want to use Jan-01-2018, or if I want to advance to Jan-02-2018, based on whether or not I have the "01" highlighted. If its highlighted it pastes "02" in place of it and then copies the whole date for the next page load. The problem I'm having is that sometimes when I highlight "01" it crtl-A, ctrl-V's "01", instead of changing only the "01" to "02".

Examples in action (both times the clipboard started with "Jan-01-2018"

How its supposed to work: https://i.imgur.com/8N1iJYU.gifv

How it sometimes works: https://i.imgur.com/xBIye28.gifv

and the code:

Code: Select all

^j::
ClipSaved := ClipboardAll       ;save clipboard
clipboard := ""  ; empty clipboard
Send, ^c    ; copy the selected file
   if (Clipboard = "") {
   Clipboard := ClipSaved   ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
   ClipSaved =   ; Free the memory in case the clipboard was very large.
   Send, ^a^v
   } else {
        RegExMatch(clipboard,"\d+$",match)		;finds number last in clipboard string, and stores it to the value "match"
        match++  ;increases the value of "match" by 1
        clipboard:=RegExReplace(clipboard,"\d+$",match)  ;replaces the last number in clipboard string with the new value of "match"
        send ^v
        send, ^a^c
   }
Return




User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Script is providing inconsistent results

17 Jul 2018, 17:29

- I would avoid trying to click at the nth character, because its position may be affected by the widths of the other characters, which can change.
- Also, odd things can happen with clicking e.g. if you click at a certain speed, you may select a word or the entire string.
- I would put a date into memory, and use hotkeys to increment/decrement by one day, and not use the clipboard, but send text instead.
- Here's an example script:

Code: Select all

;warning: if used in the wrong place this script could overwrite important text in the program you are using
#IfWinActive, My Test File - Notepad
q::
w::
;uncomment the 2 lines below to start at a specific date:
;if (vDate = "")
;	vDate := 20180401 ;start at APR-01-2018
vNum := InStr(A_ThisHotkey, "q") ? 1 : -1 ;increment/decrement based on the hotkey
EnvAdd, vDate, % vNum, D
FormatTime, vDate2, % vDate, MMM-dd-yyyy
SendInput, % "^a{Text}" vDate2 ;note: {Text} requires AHK v1.1.27+
return
#IfWinActive
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
kspanks04
Posts: 3
Joined: 17 Jul 2018, 15:32

Re: Script is providing inconsistent results

18 Jul 2018, 09:18

Thank you, that is a much better solution! How would I add a third option to increment by 0 (keep the date the same)? This is throwing an exception:

Code: Select all

;warning: if used in the wrong place this script could overwrite important text in the program you are using
#q::
#w::
#e::
;uncomment the 2 lines below to start at a specific date:
if (vDate = "")
	vDate := 20180212 ;start at APR-01-2018
vNum := InStr(A_ThisHotkey, "q") ? 1 : -1 : 0 ;increment/decrement based on the hotkey
EnvAdd, vDate, % vNum, D
FormatTime, vDate2, % vDate, MMM-dd-yyyy
SendInput, % "^a{Text}" vDate2 ;note: {Text} requires AHK v1.1.27+
return
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Script is providing inconsistent results  Topic is solved

18 Jul 2018, 09:37

An example that handles 3 possibilities:

Code: Select all

q:: ;ternary operator 3-possibility example
w::
e::
vNum := InStr(A_ThisHotkey, "q") ? 1
: InStr(A_ThisHotkey, "w") ? -1
: 0
MsgBox, % vNum
return

;==================================================

;q:: ;ternary operator 3-possibility example (if/else alternative)
;w::
;e::
if InStr(A_ThisHotkey, "q")
	vNum := 1
else if InStr(A_ThisHotkey, "w")
	vNum := -1
else
	vNum := 0
MsgBox, % vNum
return
Links:
Variables and Expressions - Definition & Usage | AutoHotkey
https://autohotkey.com/docs/Variables.htm#ternary
Sort - Syntax & Usage | AutoHotkey
https://autohotkey.com/docs/commands/Sort.htm#Examples
Switch/Case statement - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 86#p187486
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Rohwedder and 249 guests