loop not working when Count is a variable [solved]

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
1scotch469
Posts: 2
Joined: 27 Mar 2017, 06:14

loop not working when Count is a variable [solved]

27 Mar 2017, 07:00

My loop works, unless Count is a variable or a Clipboard value.

According to https://autohotkey.com/docs/commands/Loop.htm
I should be able to run a loop where Count is a variable.

Likewise, according to https://autohotkey.com/docs/misc/Clipboard.htm and https://autohotkey.com/board/topic/8630 ... -variable/
I should be able to set a variable using contents of the clipboard in order to use that variable (those clipboard contents) elsewhere in a script.

However, I cannot get it to work.

I am new to AHK. My script is to manipulate an OpenOffice Calc project.

I have run my script countless times trying to debug the problem. I am at a loss.

This script excerpt works

Code: Select all

; /******************************/ ; get row number and set RepeatCount

send, {Home}
sleep, 20
send, {end}
sleep, 20
send, {=}{r}{o}{w}{(}{)}{enter}
sleep, 20
send, {up}
Clipboard =						  ; Must start off blank for detection to work.
RepeatCount =                     ; 
send, {Lcontrol down}{x}{lcontrol up}				; Cut text into Clipboard
ClipWait, 2
sleep, 5500
clipboard = %clipboard%   ; Convert any formatted text to plain text.
sleep, 500

; /******************************/ test it with notepad (to make sure that the data is being copied to the clipboard)
send, #r
sleep, 300
send, notepad{enter}
WinActivate, Untitled - Notepad ahk_class SALFRAME									 
; WinWaitActive, Untitled - Notepad ahk_class SALFRAME
sleep, 500
send, ^v
sleep, 500
send, !fx
sleep, 100
send, {enter}
sleep, 500
send, output{enter}
sleep, 500

; /******************************/  a quick break to check the notepad output
MsgBox, 4,, First section complete,<br>Would you like to continue? (press Yes or No)<br>This Message will timeout in 120 seconds, 120
IfMsgBox Timeout
    MsgBox You didn't press YES or NO within the 120-second period.
else IfMsgBox No
    ExitApp
; /******************************/

WinActivate, bak-AltecLansing.ods - OpenOffice Calc ahk_class SALFRAME									 
; WinWaitActive, bak-AltecLansing.ods - OpenOffice Calc ahk_class SALFRAME					
sleep, 1500


; RepeatCount := Clipboard	   	 ; Fetch the text into variable

; Loop, Clipboard {
; Loop, %RepeatCount% {
Loop, 247 {
send, {home}
Sleep, 50
send, {LShift Down}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{LShift Up}
Sleep, 50
send, {LAlt Down}{o}{e}{y}{LAlt Up}
Sleep, 50
Send, {Up}
}

; /******************************/ did it loop?
MsgBox, 4,, Second section complete,<br>Would you like to continue? (press Yes or No)<br>This Message will timeout in 10 seconds, 10
IfMsgBox Timeout
    MsgBox You didn't press YES or NO within the 10-second period.
else IfMsgBox No
    ExitApp
; /******************************/
This script excerpt does not work

Code: Select all

; /******************************/ ; get row number and set RepeatCount

send, {Home}
sleep, 20
send, {end}
sleep, 20
send, {=}{r}{o}{w}{(}{)}{enter}
sleep, 20
send, {up}
Clipboard =						  ; Must start off blank for detection to work.
RepeatCount =                     ; 
send, {Lcontrol down}{x}{lcontrol up}				; Cut text into Clipboard
ClipWait, 2
sleep, 5500
clipboard = %clipboard%   ; Convert any formatted text to plain text.
sleep, 500

; /******************************/ test it with notepad (to make sure that the data is being copied to the clipboard)
send, #r
sleep, 300
send, notepad{enter}
WinActivate, Untitled - Notepad ahk_class SALFRAME									 
; WinWaitActive, Untitled - Notepad ahk_class SALFRAME
sleep, 500
send, ^v
sleep, 500
send, !fx
sleep, 100
send, {enter}
sleep, 500
send, output{enter}
sleep, 500

; /******************************/  a quick break to check the notepad output
MsgBox, 4,, First section complete,<br>Would you like to continue? (press Yes or No)<br>This Message will timeout in 120 seconds, 120
IfMsgBox Timeout
    MsgBox You didn't press YES or NO within the 120-second period.
else IfMsgBox No
    ExitApp
; /******************************/

WinActivate, bak-AltecLansing.ods - OpenOffice Calc ahk_class SALFRAME									 
; WinWaitActive, bak-AltecLansing.ods - OpenOffice Calc ahk_class SALFRAME					
sleep, 1500


RepeatCount := Clipboard	   	 ; Fetch the text into variable

; Loop, Clipboard {
Loop, %RepeatCount% {
; Loop, 247 {
send, {home}
Sleep, 50
send, {LShift Down}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{LShift Up}
Sleep, 50
send, {LAlt Down}{o}{e}{y}{LAlt Up}
Sleep, 50
Send, {Up}
}

; /******************************/ did it loop?
MsgBox, 4,, Second section complete,<br>Would you like to continue? (press Yes or No)<br>This Message will timeout in 10 seconds, 10
IfMsgBox Timeout
    MsgBox You didn't press YES or NO within the 10-second period.
else IfMsgBox No
    ExitApp
; /******************************/
This script excerpt also does not work

Code: Select all

; /******************************/ ; get row number and set RepeatCount

send, {Home}
sleep, 20
send, {end}
sleep, 20
send, {=}{r}{o}{w}{(}{)}{enter}
sleep, 20
send, {up}
Clipboard =						  ; Must start off blank for detection to work.
RepeatCount =                     ; 
send, {Lcontrol down}{x}{lcontrol up}				; Cut text into Clipboard
ClipWait, 2
sleep, 5500
clipboard = %clipboard%   ; Convert any formatted text to plain text.
sleep, 500

; /******************************/ test it with notepad (to make sure that the data is being copied to the clipboard)
send, #r
sleep, 300
send, notepad{enter}
WinActivate, Untitled - Notepad ahk_class SALFRAME									 
; WinWaitActive, Untitled - Notepad ahk_class SALFRAME
sleep, 500
send, ^v
sleep, 500
send, !fx
sleep, 100
send, {enter}
sleep, 500
send, output{enter}
sleep, 500

; /******************************/  a quick break to check the notepad output
MsgBox, 4,, First section complete,<br>Would you like to continue? (press Yes or No)<br>This Message will timeout in 120 seconds, 120
IfMsgBox Timeout
    MsgBox You didn't press YES or NO within the 120-second period.
else IfMsgBox No
    ExitApp
; /******************************/ if we're continuing, then yes, the notepad output is correct

WinActivate, bak-AltecLansing.ods - OpenOffice Calc ahk_class SALFRAME									 
; WinWaitActive, bak-AltecLansing.ods - OpenOffice Calc ahk_class SALFRAME					
sleep, 1500


; RepeatCount := Clipboard	   	 ; Fetch the text into variable

Loop, Clipboard {
; Loop, %RepeatCount% {
; Loop, 247 {
send, {home}
Sleep, 50
send, {LShift Down}{Right}{Right}{Right}{Right}{Right}{Right}{Right}{LShift Up}
Sleep, 50
send, {LAlt Down}{o}{e}{y}{LAlt Up}
Sleep, 50
Send, {Up}
}

; /******************************/ did it loop?
MsgBox, 4,, Second section complete,<br>Would you like to continue? (press Yes or No)<br>This Message will timeout in 10 seconds, 10
IfMsgBox Timeout
    MsgBox You didn't press YES or NO within the 10-second period.
else IfMsgBox No
    ExitApp
; /******************************/
According to the documents I referenced at the beginning of this post, all three of those variations of the code should work and should all produce the same results... As far as I can tell anyway, but what do I know, there is likely some secret not explicitly explicit in the documentation. :/




Any help at all would be greatly appreciated, im running out of hair to pull out.
Last edited by 1scotch469 on 27 Mar 2017, 08:58, edited 2 times in total.
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: loop not working when Count is a variable

27 Mar 2017, 08:03

You can use % to force expressions, which is then evaluated

Code: Select all

Loop % Clipboard {
	;Something
}
or this to get the value of a variable

Code: Select all

Loop %Clipboard% {
	;Something
}
Edit: in ahk there are too types of statements:
  • Expressions: in expressions variables are evaluated and strings needs to be surrounded with ". When assigning a variable use :=. And as mentioned a single % forces the statement to be interpreted as an expression.
  • Not-expressions: in not-expressions variables are surrounded with % else it will be considered text. When assigning a variable use =
Edit 2: Sorry, I didn't notice that you had tried this already.
Last edited by Capn Odin on 27 Mar 2017, 08:17, edited 2 times in total.
Please excuse my spelling I am dyslexic.
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: loop not working when Count is a variable

27 Mar 2017, 08:06

Try testing that the clipboard contains a loop count. See "Integer" in the help index for "If Clipboard is Integer" or "If Clipboard is Number" etc..

If the Clipboard variable does contain an Integer string, then you need to bracket it with % signs as in "Loop,%Clipboard%"
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
1scotch469
Posts: 2
Joined: 27 Mar 2017, 06:14

Re: loop not working when Count is a variable

27 Mar 2017, 08:50

Thank you MilesAhead and Capn Odin

Your answers put me on the right track.

Turns out when copying a cell from OOcalc, included in the copied data is a line break (line return), and that throws an illegal character error. The error never came up until I tried to evaluate as an expression.

Code: Select all

Loop % Clipboard {
	; something
	}
	;OR
	Loop, % %RepeatCount% {
	; something
	]
	
Then I was able to get the error.

To solve it, I ran the clipboard contents through Notepad and stripped off the line break, then recopied to clipboard, and viola it worked. a little clunky, but it works.

Thanks again, ill mark as solved.
Guest

Re: loop not working when Count is a variable [solved]

27 Mar 2017, 09:07

You can trim the `r`n and space characters from the clipboard automatically using Trim() like so

Loop, % Trim(Clipboard,"`r`n ")
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: loop not working when Count is a variable [solved]

27 Mar 2017, 11:48

Another approach is to use "Loop, parse, clipboard, `n, `r" as shown in the examples. It has the benefit that it should work for both single and multi-line strings of data from the clipboard.
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: loop not working when Count is a variable [solved]

28 Mar 2017, 21:57

1scotch469 wrote:Turns out when copying a cell from OOcalc, included in the copied data is a line break (line return),
Excel does that, too.
and that throws an illegal character error.
The illegal character error is because you are performing a double-deref, and as it says, that character is not valid in a variable name. You don't want to interpret the value as a variable name in the first place; you are using the wrong syntax. (The newline character is a problem either way, but it is a separate problem.)

Code: Select all

; WRONG:
Loop, % %RepeatCount%
; Right:
Loop, % RepeatCount
Loop, %RepeatCount%
https://autohotkey.com/docs/FAQ.htm#percent
https://autohotkey.com/docs/Variables.htm#ref

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, jaka1, mikeyww, Rohwedder and 307 guests