There must be a better way of doing this :) Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

There must be a better way of doing this :)

22 May 2017, 05:08

Hi,

I have this code where I want to disable certain gui buttons if certain script(s) is / are not running.
It works like intended, but I have a feeling there must be a better way of doing it.
I feel like repeating the same code for every button is a bit inefficient.

Code: Select all

SetTimer, CheckScript, 5000
SetTimer, CheckScript2, 5000

CheckScript:

IfWinNotExist, SRT Cloud afmelden.ahk
{
	GuiControl, disable, Script5
}
Else 
{
	GuiControl, enable, Script5
}
CheckScript2:
IfWinNotExist, TSI Cloud afmelden.ahk
{
	GuiControl, disable, Script6
}
Else 
{
	GuiControl, enable, Script6
}
return
(I know the curly brackets are not required, but that's the way I like to code :) ).
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: There must be a better way of doing this :)

22 May 2017, 05:23

try this

Code: Select all

scripts := ["Script1name.akk","Script2name.ahk","Script3name.ahk","Script4name.ahk","SRT Cloud afmelden.ahk","TSI Cloud afmelden.ahk"]
SetTimer, CheckScript, 5000

CheckScript:
for i, name in scripts
{
   IfWinNotExist, %name%
      GuiControl, disable, Script%i%
   Else 
      GuiControl, enable, Script%i%
}
return
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: There must be a better way of doing this :)

22 May 2017, 06:46

Thanks Blackholyman,

It works, but I don't really like that you have to add these 4 fake scripts in the code.
What if I had a gui button that doesn't contain a number ?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: There must be a better way of doing this :)  Topic is solved

22 May 2017, 07:18

Code: Select all

Loop 
{
    CheckScript("SRT Cloud afmelden.ahk",5,10)          ; window title, script number, seconds to wait (optional)
    CheckScript("TSI Cloud afmelden.ahk",6,3)
    }

CheckScript(wintitle,sNo,s=5){                          ; if 3rd paramter is missing 5 seconds to wait (default)
    toggle := (WinExist(wintitle)=0) ? "enable":"disable"   ; if window title not exist (=0) is true, enable else disable
    GuiControl, %toggle%, Script%sNo%
    Sleep % s*1000
    Return
    }

!x::ExitApp
    
Not tested (I'm away from my desk). Good luck :)
Last edited by BoBo on 22 May 2017, 07:46, edited 1 time in total.
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: There must be a better way of doing this :)

22 May 2017, 07:41

Nice approach, I see what you're doing there It is throwing an error though.

The error is:

Warning: This variable has not been assigned a value.

Specifically: enable (a global variable)
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: There must be a better way of doing this :)

22 May 2017, 07:47

WalkerOfTheDay wrote:Nice approach, I see what you're doing there It is throwing an error though.

The error is:

Warning: This variable has not been assigned a value.

Specifically: enable (a global variable)
that should do the trick ... "enable" : "disable"
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: There must be a better way of doing this :)

22 May 2017, 08:40

Yes ! :clap:

It's working, it only was doing the opposite of what I wanted :lol:

So I changed:

Code: Select all

toggle := (WinExist(wintitle)=0) ? "enable":"disable"   ; if window title not exist (=0) is true, enable else disable
To:

Code: Select all

toggle := (!WinExist(wintitle)=0) ? "enable":"disable"   ; if window title not exist (=0) is true, enable else disable
Thank you so much. I love this community !
User avatar
WalkerOfTheDay
Posts: 710
Joined: 24 Mar 2016, 03:01

Re: There must be a better way of doing this :)

22 May 2017, 08:51

Spoke to soon, the loop prevent the rest op the script from executing. So when I press any of the
buttons, nothing happens.

I think I need to somehow replace the loop with a timer.
I'll try to find the sollution myself. If I fail I'll get back to this thread :)

I did it :dance:

Code: Select all

CheckScript(wintitle,sNo,s=5) ; if 3rd parameter is missing 5 seconds to wait (default)
{                              
	toggle := (!WinExist(wintitle)=0) ? "enable":"disable"   ; if window title not exist (=0) is true, enable else disable
	GuiControl, %toggle%, Script%sNo%
	Sleep % s*1000
	Return
 }


SetTimer, ReloadButton, 1000

ReloadButton:
    	CheckScript("SRT Cloud afmelden.ahk",5,1)          ; window title, script number, seconds to wait (optional)
   	CheckScript("TSI Cloud afmelden.ahk",6,1)
	return 

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, iamMG and 169 guests