Combining Two Commands to Switch Between If One Doesn't Work Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
asopala
Posts: 21
Joined: 23 May 2021, 13:52

Combining Two Commands to Switch Between If One Doesn't Work

Post by asopala » 18 Aug 2022, 12:13

Hi,

I'm trying to set up a macro that toggles on/off a button in Pro Tools (the Grid show/hide button) using Findtext. Only issue is it works clicking once, but not the second time because the color of the background changed. I have two separate commands:

Code: Select all

t1:=A_TickCount, X:=Y:=""

Text:="|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004"

	 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))
	 {
	   CoordMode, Mouse
	   X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
	   Click, %X%, %Y%
	 }

Code: Select all

 t1:=A_TickCount, X:=Y:=""

Text:="|<>*105$20.zzzw7rqRzxjlMP0qqwBBb3PQ4q7zzzU"

	 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))
	 {
	   CoordMode, Mouse
	   X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
	   Click, %X%, %Y%
	 }
I ultimately want it so that if one doesn't work, it defaults to the other code, effectively allowing me to toggle this button on and off. Is there a way to do that in autohotkey? I have pictures of the button below.
Attachments
Grid Off.PNG
Grid Off.PNG (6.47 KiB) Viewed 2411 times
Grid On.PNG
Grid On.PNG (5.47 KiB) Viewed 2411 times

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by mikeyww » 18 Aug 2022, 13:03

Hello,

Perhaps an Else block?

asopala
Posts: 21
Joined: 23 May 2021, 13:52

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by asopala » 18 Aug 2022, 14:05

mikeyww wrote:
18 Aug 2022, 13:03
Hello,

Perhaps an Else block?
That seemed to do it. Haven't used that one before. Though when I tried adding a WinGetPos and a MouseMove in the script, I got the error message ELSE with no matching IF. Not sure what's causing the conflict.

Code: Select all

if WinGetPos, wX, wY, wW, wH, AudioSuite
		SetMouseDelay, 20

			t1:=A_TickCount, X:=Y:=""

			Text:="|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004"

			 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))
			 {
			   CoordMode, Mouse
			   X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
			   Click, %X%, %Y%
			 }

			MouseMove, %Orig_X%, %Orig_Y%

			Sleep, 500

	else WinGetPos, wX, wY, wW, wH, AudioSuite
		SetMouseDelay, 20

			t1:=A_TickCount, X:=Y:=""

			Text:="|<>*105$20.zzzw7rqRzxjlMP0qqwBBb3PQ4q7zzzU"

			 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))
			 {
			   CoordMode, Mouse
			   X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
			   Click, %X%, %Y%
			 }
			MouseMove, %Orig_X%, %Orig_Y%

			Sleep, 500

	return

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by BoBo » 18 Aug 2022, 16:04

Probably like this...

Code: Select all

if (<whatever condition>) {
	WinGetPos, wX, wY, wW, wH, AudioSuite
	SetMouseDelay, 20

	t1:=A_TickCount, X:=Y:=""
	Text:="|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004"

	if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))	{
		CoordMode, Mouse
		X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
		Click, %X%, %Y%
		}

		MouseMove, %Orig_X%, %Orig_Y%
		Sleep, 500
	} else {
		WinGetPos, wX, wY, wW, wH, AudioSuite
		SetMouseDelay, 20

		t1:=A_TickCount, X:=Y:=""
		Text:="|<>*105$20.zzzw7rqRzxjlMP0qqwBBb3PQ4q7zzzU"

		 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text)) {
			CoordMode, Mouse
			X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
			Click, %X%, %Y%
			}
		MouseMove, %Orig_X%, %Orig_Y%
		Sleep, 500
		}
	return

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by mikeyww » 18 Aug 2022, 16:29

Also: an AHK command cannot be used as an expression.

asopala
Posts: 21
Joined: 23 May 2021, 13:52

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by asopala » 18 Aug 2022, 16:31

BoBo wrote:
18 Aug 2022, 16:04
Probably like this...

Code: Select all

if (<whatever condition>) {
	WinGetPos, wX, wY, wW, wH, AudioSuite
	SetMouseDelay, 20

	t1:=A_TickCount, X:=Y:=""
	Text:="|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004"

	if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))	{
		CoordMode, Mouse
		X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
		Click, %X%, %Y%
		}

		MouseMove, %Orig_X%, %Orig_Y%
		Sleep, 500
	} else {
		WinGetPos, wX, wY, wW, wH, AudioSuite
		SetMouseDelay, 20

		t1:=A_TickCount, X:=Y:=""
		Text:="|<>*105$20.zzzw7rqRzxjlMP0qqwBBb3PQ4q7zzzU"

		 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text)) {
			CoordMode, Mouse
			X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
			Click, %X%, %Y%
			}
		MouseMove, %Orig_X%, %Orig_Y%
		Sleep, 500
		}
	return
Didn't seem to work. It clicks on the button when grid is on, but does not move me back to the original position; it also doesn't work when the grid is off. Am I doing something wrong with WinGetPos? I just want it to get the X and Y coordinates, I don't think I need width and height.

asopala
Posts: 21
Joined: 23 May 2021, 13:52

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by asopala » 18 Aug 2022, 16:39

mikeyww wrote:
18 Aug 2022, 16:29
Also: an AHK command cannot be used as an expression.
Then I guess I'm back to square one. With the current command I have in there, it clicks when the grid is on and off, but double clicks have been occurring when the grid is on. And that's not addressing the whole thing of wanting the mouse position to go back to its original position. Here's the code I have:

Code: Select all

		t1:=A_TickCount, X:=Y:=""

		Text:="|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004"

			 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))
			 {
			   CoordMode, Mouse
			   X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
			   Click, %X%, %Y%
			 }
	else

		t1:=A_TickCount, X:=Y:=""

		Text:="|<>*105$20.zzzw7rqRzxjlMP0qqwBBb3PQ4q7zzzU"

			 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))
			 {
			   CoordMode, Mouse
			   X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
			   Click, %X%, %Y%
			 }

	return

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by mikeyww » 18 Aug 2022, 18:49

I would read about blocks, and also see how BoBo formatted the Else block.

https://www.autohotkey.com/docs/commands/Block.htm

I find that your specific use of indentation is confusing and makes the script harder to read.

Keep in mind that while indentation can look good and serve a purpose for the reader, it has no effect for AHK itself.

asopala
Posts: 21
Joined: 23 May 2021, 13:52

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by asopala » 18 Aug 2022, 19:25

mikeyww wrote:
18 Aug 2022, 18:49
I would read about blocks, and also see how BoBo formatted the Else block.

https://www.autohotkey.com/docs/commands/Block.htm

I find that your specific use of indentation is confusing and makes the script harder to read.

Keep in mind that while indentation can look good and serve a purpose for the reader, it has no effect for AHK itself.
Ok, I looked into it. Good to know about the indentations. Looks like the only thing I need besides having the cursor go back to its original position is knowing if there's a way to make an if statement for "if the following code works" or something like that. Here's what I got.

Code: Select all

if ;Need something here to say if the following function works

{ t1:=A_TickCount, X:=Y:=""

Text:="|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004"

if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))
	{
	CoordMode, Mouse
	X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
	Click, %X%, %Y%
	}
}

else ;If the previous doesn't work, execute this function

{ t1:=A_TickCount, X:=Y:=""

	Text:="|<>*105$20.zzzw7rqRzxjlMP0qqwBBb3PQ4q7zzzU"

	if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text))
	{
	CoordMode, Mouse
	X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
	Click, %X%, %Y%
	}
}

return

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by mikeyww » 18 Aug 2022, 22:05

I suppose it depends on what you mean by "function", and what you mean by "works". https://www.autohotkey.com/docs/Concepts.htm#functions

ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by ananthuthilakan » 19 Aug 2022, 01:38

Code: Select all

Text:=["|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004","|<>*105$20.zzzw7rqRzxjlMP0qqwBBb3PQ4q7zzzU"]
T::  ;--> your hotkey to ON/OFF the button
t1:=A_TickCount, X:=Y:=""
loop, 2
{
 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text[A_index])) ;---> if not working increase the fault tolerance from 0 to 0.1 or create this thing ; "|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004" again
	 {
	   CoordMode, Mouse
	   X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id
	   Click, %X%, %Y%
	   break
	 }

}
return

wouldn't this do the trick
Edit :
Tested with the images you provided and working as intended :rainbow: :rainbow:

ananthuthilakan
Posts: 188
Joined: 08 Jul 2019, 05:37
Contact:

Re: Combining Two Commands to Switch Between If One Doesn't Work  Topic is solved

Post by ananthuthilakan » 19 Aug 2022, 02:12

Code: Select all

coordMode, mouse, screen
Text:=["|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004","|<>*105$20.zzzw7rqRzxjlMP0qqwBBb3PQ4q7zzzU"]
T::  ;--> your hotkey to ON/OFF the button
MouseGetPos, orig_x, orig_y
t1:=A_TickCount, X:=Y:=""  ;--> t1 variable  := A_tickCount is also not needed i guess for your use case
loop, 2
{
 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text[A_index]))
	 {
	   ;~ CoordMode, Mouse
	   X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id  
	   Click, %X%, %Y%
	   break
	 }

}
MouseMove, %orig_x% , %orig_y%
return
With move mouse back to original location :cookie:
Animation.gif
Animation.gif (76.42 KiB) Viewed 982 times

asopala
Posts: 21
Joined: 23 May 2021, 13:52

Re: Combining Two Commands to Switch Between If One Doesn't Work

Post by asopala » 23 Aug 2022, 14:45

ananthuthilakan wrote:
19 Aug 2022, 02:12

Code: Select all

coordMode, mouse, screen
Text:=["|<>*103$21.0001s44EU0W0owHYYW0Y4MYYVsYw0004","|<>*105$20.zzzw7rqRzxjlMP0qqwBBb3PQ4q7zzzU"]
T::  ;--> your hotkey to ON/OFF the button
MouseGetPos, orig_x, orig_y
t1:=A_TickCount, X:=Y:=""  ;--> t1 variable  := A_tickCount is also not needed i guess for your use case
loop, 2
{
 if (ok:=FindText(914-150000, 106-150000, 914+150000, 106+150000, 0, 0, Text[A_index]))
	 {
	   ;~ CoordMode, Mouse
	   X:=ok.1.x, Y:=ok.1.y, Comment:=ok.1.id  
	   Click, %X%, %Y%
	   break
	 }

}
MouseMove, %orig_x% , %orig_y%
return
With move mouse back to original location :cookie:
Animation.gif
Holy crap, you did it. Thanks so much!

Post Reply

Return to “Ask for Help (v1)”