Proper format for multiple IF statements Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Proper format for multiple IF statements

13 Nov 2020, 20:18

Hello,

Can someone look at the script below and help me understand what I've missed or done wrong?

I'm working in PowerPoint and I want to move a selected object by entering the horizontal and vertical positions in the Format Shape box.

Prior to running the script, I must input three variables:

xPos :=
yPos:=
Ref :=

The X and Y variables are arbitrary and the Ref variable is one of four letters (T, L, R, B) representing Top, Left, Right, or Bottom respectively.

The eight reference variables are fixed.

I suspect the problem is the way I've written the four IF portions. I'm use to Excel where if one IF criteria is satisfied, the script stops; and if not satisfied, it automatically goes to the next IF, etc. But does that work the same way in AutoHotkey?

Thanks

Code: Select all

F2::					
;Sleep lengths:					
    short := 999					
    medium := 999					
    long := 2299					
;Input Variables
	xPos := 19.51				
	yPos := 2.19				
	Ref := R				
; Reference Variables
	xRefT :=  -3.78		; xTop		
	yRefT :=  -0.62		; yTop		
					
	xRefL := -0.46		; xLeft		
	yRefL :=  -4.40		; yLeft		
					
	xRefR :=  -7.10		; xRight		
	yRefR :=  -4.40		; yRight		
					
	xRefB :=  -3.78		; xBottom		
	yRefB :=  -8.18		; yBottom		
					
CoordMode, Mouse, Screen					
Click, Right					
	Sleep, short				
Send z						; Format Shape box	
	Sleep, short				
CoordMode, Mouse, Relative					
Click 95, 635				; Position	
	Sleep, short				
If Ref = T					
	Send !h				
		xVar := xPos + xRefT			
		Send %xVar%			
	Send !v				
		yVar := yPos + yRefT			
		Send %yVar%			
If Ref = L					
	Send !h				
		xVar := xPos + xRefL			
		Send %xVar%			
	Send !v				
		yVar := yPos + yRefL			
		Send %yVar%			
If Ref = R					
	Send !h				
		xVar := xPos + xRefR			
		Send %xVar%			
	Send !v				
		yVar := yPos + yRefR			
		Send %yVar%			
If Ref = B					
	Send !h				
		xVar := xPos + xRefB			
		Send %xVar%			
	Send !v				
		yVar := yPos + yRefB			
		Send %yVar%			
Send {Enter}					
	Sleep, short				
Return
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: Proper format for multiple IF statements  Topic is solved

13 Nov 2020, 20:24

Read about If.
If the If statement's expression evaluates to true (which is any result other than an empty string or the number 0), the line or block underneath it is executed. Otherwise, if there is a corresponding Else statement, execution jumps to the line or block underneath it. If an If owns more than one line, those lines must be enclosed in braces (to create a block). However, if only one line belongs to an If, the braces are optional. See the examples at the bottom of this page.
Braces, rather than indentation, identify blocks in AHK. The documentation page has examples that will help you to correct your script.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Proper format for multiple IF statements

13 Nov 2020, 21:18

Thank you for the comments and the link.

I made changes to the script by placing the IF criteria in parentheses and enclosing the blocks in curly brackets. Now there is a response to the script but it always responds as if the reference (Ref) is B — the last option.

For example, below is the updated script with:

Ref := R

But as mentioned, the result is as though Ref := B

Thanks

Code: Select all

F2::					
;Sleep lengths:					
    short := 999					
    medium := 999					
    long := 2299					
;Input Variables
	xPos := 17.84	
	yPos := 16.36
	Ref := R				
; Reference Variables
	xRefT :=  -3.78		; xTop		
	yRefT :=  -0.62		; yTop		
					
	xRefL := -0.46		; xLeft		
	yRefL :=  -4.40		; yLeft		
					
	xRefR :=  -7.10		; xRight		
	yRefR :=  -4.40		; yRight		
					
	xRefB :=  -3.78		; xBottom		
	yRefB :=  -8.18		; yBottom		
					
CoordMode, Mouse, Screen					
Click, Right					
	Sleep, short				
Send z						; Format Shape box	
	Sleep, short				
CoordMode, Mouse, Relative					
Click 95, 635				; Position	
	Sleep, short				
If (Ref = T)
	{
	Send !h				
		xVar := xPos + xRefT			
		Send %xVar%			
	Send !v				
		yVar := yPos + yRefT			
		Send %yVar%
	}
If (Ref = L)					
	{
	Send !h				
		xVar := xPos + xRefL			
		Send %xVar%			
	Send !v				
		yVar := yPos + yRefL			
		Send %yVar%			
	}
If (Ref = R)			
	{
	Send !h				
		xVar := xPos + xRefR			
		Send %xVar%			
	Send !v				
		yVar := yPos + yRefR			
		Send %yVar%			
	}
If (Ref = B)					
	{
	Send !h				
		xVar := xPos + xRefB			
		Send %xVar%			
	Send !v				
		yVar := yPos + yRefB			
		Send %yVar%			
	}
Send {Enter}					
	Sleep, short				
Return
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Proper format for multiple IF statements

13 Nov 2020, 21:22

Note: I just discovered that the object is being moved to each of the four locations and ends with B. Why is that occurring if the Ref variable is only equal to R?
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
gregster
Posts: 9096
Joined: 30 Sep 2013, 06:48

Re: Proper format for multiple IF statements

13 Nov 2020, 21:26

I guess you meant to use strings - then you need to use quotes around them (in expression mode):

Code: Select all

Ref := "R"
;...
if (Ref = "R")
and so on..

Currently, you are using variables instead, which are all empty (T, L, R, B and Ref) - that's why every if triggers.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Proper format for multiple IF statements

13 Nov 2020, 21:43

Ahh. That did the trick.

Many thanks to both of you.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Proper format for multiple IF statements

13 Nov 2020, 21:52

WeThotUWasAToad wrote:
13 Nov 2020, 20:18
Hello,

Can someone look at the script below and help me understand what I've missed or done wrong?
It looks like you are writing AutoHotkey code as if it was Python, and not a C family language.
In C family languages, curly braces are your friend { }. They help you visually and logically group code.

Code: Select all

if (Ref = R)
{
	Send !h				
	xVar := xPos + xRefR			
	Send %xVar%			
	Send !v				
	yVar := yPos + yRefR			
	Send %yVar%	
}
Also, you appear to be indenting lines of code randomly or unnecessarily.
This code...

Code: Select all

Click, Right					
	Sleep, short				
Send z						; Format Shape box	
	Sleep, short
Can simply be...

Code: Select all

Click, Right					
Sleep, short				
Send z						; Format Shape box	
Sleep, short
You would usually indent the code, where it is under a control flow statement (if, else, loop, for, while...) or if contained within curly braces ({ }).
This is important, because it helps you (and others looking at your code) visually and logically understand what is going on with your code.
A lot of problems and confusion is caused by oddly or messily written code.
WeThotUWasAToad
Posts: 312
Joined: 19 Nov 2013, 08:44

Re: Proper format for multiple IF statements

13 Nov 2020, 22:21

Thanks for the heads up.

I usually use indentation quite a bit to help me keep various things straight. So it's mainly for my own purposes. However, I will try to remember your comment so it will not be confusing when pasting code here in the forum.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CoffeeChaton, Descolada, ntepa and 153 guests