How to disable function when an app is active.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ozgurerdogan
Posts: 47
Joined: 27 Apr 2017, 02:32
Contact:

How to disable function when an app is active.

Post by ozgurerdogan » 30 Jun 2022, 08:21

I want to disable following function, if mstsc.exe is running. Is it possible?

Code: Select all

#Persistent
OnClipboardChange("clipChanged")

clipChanged(type) {
If WinActive("ahk_class CabinetWClass") || FileExist(plain := Clipboard) || type != TEXT := True
Return
If ("" = text := Trim(RegExReplace(plain, ""), " `t`r`n"))
Return
OnClipboardChange("clipChanged", False), Clipboard := "", Clipboard := text
ClipWait, 0
If ErrorLevel
MsgBox, 48, Error, An error occurred while waiting for the clipboard.
OnClipboardChange("clipChanged")
}
return
[Mod edit: [code][/code] tags added.]

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

Re: How to disable function when an app is active.

Post by mikeyww » 30 Jun 2022, 08:41

Active & running are two different things.

Code: Select all

proc = mstsc.exe
; proc = notepad.exe
Loop {
 Process, WaitClose, %proc%
 OnClipboardChange("clipChanged")
 SoundBeep, 1500
 Process, Wait, %proc%
 SoundBeep, 1000
 OnClipboardChange("clipChanged", False)
}

clipChanged(type) {
 ; Code here
 MsgBox, Test
}

ozgurerdogan
Posts: 47
Joined: 27 Apr 2017, 02:32
Contact:

Re: How to disable function when an app is active.

Post by ozgurerdogan » 30 Jun 2022, 08:47

When I add my code to ; Code here section. I am getting error. Can you please write again with my code ?

ozgurerdogan
Posts: 47
Joined: 27 Apr 2017, 02:32
Contact:

Re: How to disable function when an app is active.

Post by ozgurerdogan » 30 Jun 2022, 08:49

Actually if "D:\Belgelerim\Apps\mRemote\mRemoteNG.exe" is running... not mstsc.exe

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

Re: How to disable function when an app is active.

Post by mikeyww » 30 Jun 2022, 08:50

You can use whatever process name you like here (full path not used). See :arrow: Process. Example

It sounds like you have a different question. What is the error? What happens when you run the script? What should happen instead?

ozgurerdogan
Posts: 47
Joined: 27 Apr 2017, 02:32
Contact:

Re: How to disable function when an app is active.

Post by ozgurerdogan » 30 Jun 2022, 08:57

When I run an app named mRemoteNG.exe, I want following block not functioning / disabled.

Code: Select all

#Persistent
OnClipboardChange("clipChanged")

clipChanged(type) {
If WinActive("ahk_class CabinetWClass") || FileExist(plain := Clipboard) || type != TEXT := True
Return
If ("" = text := Trim(RegExReplace(plain, ""), " `t`r`n"))
Return
OnClipboardChange("clipChanged", False), Clipboard := "", Clipboard := text
ClipWait, 0
If ErrorLevel
MsgBox, 48, Error, An error occurred while waiting for the clipboard.
OnClipboardChange("clipChanged")
}
return

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

Re: How to disable function when an app is active.

Post by mikeyww » 30 Jun 2022, 09:09

My script shows how to do that using a loop in the auto-execute section.

ozgurerdogan
Posts: 47
Joined: 27 Apr 2017, 02:32
Contact:

Re: How to disable function when an app is active.

Post by ozgurerdogan » 30 Jun 2022, 09:15

But my function is only usable WHEN I copy a text to clipboard. So it is not auto running.
When I open or run mremote.exe AND if I copy some text to clipboard, it must not be trimmed with my function. You see.

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

Re: How to disable function when an app is active.

Post by mikeyww » 30 Jun 2022, 09:57

Your function doesn't really do anything, because variables are local to Functions by default. You can :arrow: Return the value if you wish.

David4
Posts: 27
Joined: 12 Jul 2016, 14:28

Re: How to disable function when an app is active.

Post by David4 » 30 Jun 2022, 13:20

ozgurerdogan wrote:
30 Jun 2022, 08:57
When I run an app named mRemoteNG.exe, I want following block not functioning / disabled.
Shouldn't it be enough, when you insert these lines at the beginning of your function:

Code: Select all

process,exist,mRemoteNG.exe
if errorlevel
  return

ozgurerdogan
Posts: 47
Joined: 27 Apr 2017, 02:32
Contact:

Re: How to disable function when an app is active.

Post by ozgurerdogan » 01 Jul 2022, 03:25

Ok so this is final code. But it still trims text in clipboard when a portable app named mremote.exe is running.

Code: Select all

process,exist,mRemoteNG.exe
if errorlevel
return
#Persistent
OnClipboardChange("clipChanged")

clipChanged(type) {
If WinActive("ahk_class CabinetWClass") || FileExist(plain := Clipboard) || type != TEXT := True
Return
If ("" = text := Trim(RegExReplace(plain, ""), " `t`r`n"))
Return
OnClipboardChange("clipChanged", False), Clipboard := "", Clipboard := text
ClipWait, 0
If ErrorLevel
MsgBox, 48, Error, An error occurred while waiting for the clipboard.
OnClipboardChange("clipChanged")
}
return

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

Re: How to disable function when an app is active.

Post by mikeyww » 01 Jul 2022, 05:44

The WaitClose approach might fail here if one process is opened while another is also open. An alternative is to get a list of all processes (script available on forum). If the processes have windows, this gets much easier because a window group can be used.

Code: Select all

/* Process wait for any in list ---------------------------------
This script waits for any process in a list to exist.
By mikeyww on 01 July 2022 • For AutoHotkey 1.1.34.03
https://www.autohotkey.com/boards/viewtopic.php?p=470649#p470649
-----------------------------------------------------------------
*/
procList          := ["mRemote", "mRemoteNG"]
waitPerProcessSec := 0.5 / procList.Count() ; Seconds to wait for each process
Process, Priority,, B
Loop {
 For each, proc in procList
  Process, WaitClose, %proc%.exe            ; Wait for all target processes to end
 OnClipboardChange("clipChanged")           ; Activate function
 SoundBeep, 1500
 running := False
 While !running                             ; Wait for one target process to start
  For each, proc in procList {
   Process, Wait, %proc%.exe, %waitPerProcessSec%
   running |= ErrorLevel
  }
 OnClipboardChange("clipChanged", False)    ; Deactivate function
 SoundBeep, 1000
}

clipChanged(type) {
 OnClipboardChange("clipChanged", False), Clipboard := Trim(Clipboard, " `t`r`n")
 OnClipboardChange("clipChanged")
}

ozgurerdogan
Posts: 47
Joined: 27 Apr 2017, 02:32
Contact:

Re: How to disable function when an app is active.

Post by ozgurerdogan » 01 Jul 2022, 06:30

Well I think it is running as expected. Thank you again :)

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

Re: How to disable function when an app is active.

Post by mikeyww » 01 Jul 2022, 06:49

Good! :)

I tested my comment. I was wrong, and the script should be OK. If a target window is opened & closed while another is open, in some cases, this may cause the script to proceed, but the window status will still then be detected, and the next loop iteration will begin, which is (usually) OK for most situations.

I would generally suggest adding #SingleInstance Force so as to avoid multiple instances of this script.

Another variation is below in case helpful. I was testing with Notepad & Chrome here.

Code: Select all

/* Process wait for any in list ---------------------------------
This script waits for any process in a list to exist.
By mikeyww on 01 July 2022 • For AutoHotkey 1.1.34.03
https://www.autohotkey.com/boards/viewtopic.php?p=470649#p470649
-----------------------------------------------------------------
*/
#SingleInstance Force
procList          := ["mRemote", "mRemoteNG", "notepad", "chrome"]
waitPerProcessSec := 0.5 / procList.Count()       ; Seconds to wait for each process
Process, Priority,, B
Loop {
 Loop, 2
  For each, proc in procList
   Process, WaitClose, %proc%.exe                 ; Wait for all target processes to end
 OnClipboardChange("clipChanged")                 ; Activate function
 SoundBeep, 1500
 running := False
 While !running                                   ; Wait for one target process to start
  For each, proc in procList {
   Process, Wait, %proc%.exe, %waitPerProcessSec%
   running |= ErrorLevel
  }
 OnClipboardChange("clipChanged", False)          ; Deactivate function
 SoundBeep, 1000
}

clipChanged(type) {
 OnClipboardChange("clipChanged", False), Clipboard := Trim(Clipboard, " `t`r`n")
 OnClipboardChange("clipChanged")
}

ozgurerdogan
Posts: 47
Joined: 27 Apr 2017, 02:32
Contact:

Re: How to disable function when an app is active.

Post by ozgurerdogan » 01 Jul 2022, 06:54

You know I am still getting suprised when I find new things those are possible with Ahk. I think most incredible app I have ever seen in my life it this tiny Ahk app.

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

Re: How to disable function when an app is active.

Post by mikeyww » 01 Jul 2022, 06:56

It's been very handy at my end! :thumbup:

Post Reply

Return to “Ask for Help (v1)”