How to disable script when in certain programs?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

How to disable script when in certain programs?

Post by milkygirl90 » 24 Oct 2021, 08:49

With reference to this script here : https://www.autohotkey.com/board/topic/73190-smartbright-custom-screen-brightness-and-eyesaver/

I added another piece of code but got stuck halfway:

Code: Select all

#If WinActive("Google Chrome") ||  WinActive("Word")
oldIntensity := Intensity
 Intensity := 0 ; disable intensity
 Gui, +LastFound
WinSet, Transparent, %Intensity%
sleep 100 ; how to return to oldIntensity after I switch to other programs other than chrome or word?
 Intensity := oldIntensity
 WinSet, Transparent, %Intensity%
return
#IF
Any advice please? Thank you.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to disable script when in certain programs?

Post by swagfag » 24 Oct 2021, 09:32

500 posts and a year into AHK, i think u should at least be aware of the differences between if and #If
if not, my only advice would be "for the love of god please read the damn documentation thoroughly at least once christ"

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: How to disable script when in certain programs?

Post by milkygirl90 » 24 Oct 2021, 17:12

swagfag wrote:
24 Oct 2021, 09:32
500 posts and a year into AHK, i think u should at least be aware of the differences between if and #If
if not, my only advice would be "for the love of god please read the damn documentation thoroughly at least once christ"
you don't have to be a d1ck if you don't want to help. not everyone on the forum here does coding full time like you do, and not everyone here is as smart as you to understand every single word written on the "damn documentation" as you say.

for the others, I revised my code but it doesn't seem to work:

Code: Select all

If WinActive("ahk_exe chrome.exe")
{
oldIntensity := Intensity
 Intensity := 0 ; disable intensity
 Gui, +LastFound
WinSet, Transparent, %Intensity%
 return
}

If !WinActive("ahk_exe chrome.exe")
Gui, +LastFound
WinSet, Transparent, %oldIntensity%
return

User avatar
ibieel
Posts: 216
Joined: 17 Oct 2021, 23:30

Re: How to disable script when in certain programs?

Post by ibieel » 25 Oct 2021, 01:16

I didn't understand what you want...
what does this script do? what should i do? what's the problem with it?

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: How to disable script when in certain programs?

Post by boiler » 25 Oct 2021, 05:30

I’m guessing the problem is that you expect the above code to act whenever the active/inactive status of Chrome changes. There is nothing that you’ve shown that routes the script’s execution to that code so that it would be acted on more than the one time the script is initially run. You would need to have it executed regularly either by using SetTimer, Loop, or by using something like OnWin() where you would register the change in active status as an event to trigger this code. Otherwise, there is nothing in your script that would have the checks being done continuously (or at all for that matter, other than once when the script is run).

By the way, you are missing braces around your second If’s block of code. Also, rather than an If with the opposite condition, you should just use else. That’s what it’s for.

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: How to disable script when in certain programs?

Post by milkygirl90 » 25 Oct 2021, 05:59

ibieel wrote:
25 Oct 2021, 01:16
I didn't understand what you want...
what does this script do? what should i do? what's the problem with it?
Hi, this block of code is inserted into the brightness script (linked in 1ˢᵗ post).

When I alt+tab between windows and if it hits certain apps (E.g. chrome in this case), I'd like to disable the overlay (which means intensity := 0). When I switch to other apps, I just want to revert to my previous intensity, like such:

Code: Select all

WinSet, Transparent, %oldIntensity%

User avatar
milkygirl90
Posts: 565
Joined: 10 Nov 2020, 21:22

Re: How to disable script when in certain programs?

Post by milkygirl90 » 25 Oct 2021, 06:08

boiler wrote:
25 Oct 2021, 05:30
I’m guessing the problem is that you expect the above code to act whenever the active/inactive status of Chrome changes. There is nothing that you’ve shown that routes the script’s execution to that code so that it would be acted on more than the one time the script is initially run. You would need to have it executed regularly either by using SetTimer, Loop, or by using something like OnWin() where you would register the change in active status as an event to trigger this code. Otherwise, there is nothing in your script that would have the checks being done continuously (or at all for that matter, other than once when the script is run).

By the way, you are missing braces around your second If’s block of code. Also, rather than an If with the opposite condition, you should just use else. That’s what it’s for.
Thanks for the suggestion. I have amended and solved it with settimer! Just want to check - is it very "wasteful" to loop every 300ms using setTimer? Is there an alternative that doesn't require a loop?

There's also another tricky situation. I'm trying to disable the brightness just before my screenshot tool does a window capture (ShareX). However, no matter how fast the loop is (1ms), it will still capture the "dimmed" screen. Any workaround for this?

User avatar
boiler
Posts: 16926
Joined: 21 Dec 2014, 02:44

Re: How to disable script when in certain programs?

Post by boiler » 25 Oct 2021, 07:20

milkygirl90 wrote: Just want to check - is it very "wasteful" to loop every 300ms using setTimer? Is there an alternative that doesn't require a loop?
It probably doesn’t use that much CPU time at once every 300ms. You can check out CPU usage with Windows’ Task Manager to confirm that. An alternative is using the OnWin() function that I linked in my post, but I believe I discovered it’s not actually registering window events with Windows; it also would be doing a periodic check just as you are, so it’s likely not much more efficient. One way it would be more efficient is that you would only be executing your WinSet code when there is a change in the window active status rather than every time.

Similarly, you could make your SetTimer approach more efficient by storing the last active status of the window in a variable, and you only execute the code in either if/else branch when there is a change in whether the window is active or not. That way you’re only doing some checks to see if the window is active or not every 300ms, not executing a WinSet command every time.

milkygirl90 wrote: There's also another tricky situation. I'm trying to disable the brightness just before my screenshot tool does a window capture (ShareX). However, no matter how fast the loop is (1ms), it will still capture the "dimmed" screen. Any workaround for this?
You could perhaps make a hotkey out of whatever the current key combination is to invoke ShareX to intercept it and temporarily remove the dimming screen cover before executing the screenshot. It might be more complicated if it involves dragging the mouse, but something along those line can probably still be done.

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: How to disable script when in certain programs?

Post by malcev » 25 Oct 2021, 19:02

no matter how fast the loop is (1ms), it will still capture the "dimmed" screen. Any workaround for this?
Dont use image search for automation.

Post Reply

Return to “Ask for Help (v1)”