function or similar for repeated code Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
chocnmint
Posts: 6
Joined: 11 Mar 2018, 16:06

function or similar for repeated code

11 Mar 2018, 17:43

I'm having some problems trying to save some lines, with many hotkeys that check if one program is running, and if so do some stuff or tell me it's not running.

F1::
if WinExist, program X
{
WinActivate
DO SOME STUFF #1
return
}
Else
{
MsgBox, program X is not running
return
}


F2::
if WinExist, program X (same as before)
{
WinActivate
DO SOME STUFF #2 (different from #1)
return
}
Else
{
MsgBox, program X is not running
return
}

.
.
.
.
.
and so on with many keys for the same program.
I'd like to find a way to make something like a function that handles the "if" script, so I can save a lot of lines of code.
The way I thought was to make a label just before "do some stuff" and use it inside the function, but it cannot be repeated the same label for the different hotkeys.
Thanks in advance.
Last edited by chocnmint on 11 Mar 2018, 18:31, edited 2 times in total.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: function or similar for repeated code

11 Mar 2018, 18:10

Code: Select all

F2::func("random program","print","random program is not running")
F3::func("another winTitle","copy","49")
F4::func("yet another winTitle","sound")

func(winTitle,function,msg := "Error!") {
   If WinExist(winTitle) {
      WinActivate,% winTitle
      %function%()
      Return
      } Else {
      MsgBox % msg
      }
   Return
   }
   
print() {
   MsgBox % "I'm printing"
   Return
   }
copy() {
   MsgBox % "I'm copying"
   Return
   }

sound() {
   SoundBeep
   Return
   }
Not tested.
Added additional feature: "execute different task" (the 2nd parameter)
chocnmint
Posts: 6
Joined: 11 Mar 2018, 16:06

Re: function or similar for repeated code

11 Mar 2018, 18:19

BoBo wrote:

Code: Select all

F2::func("random program","random program is not running")
F3::func("another winTitle","49")
F4::func("yet another winTitle")

func(winTitle,msg := "Error!") {
   If WinExist(winTitle) {
      WinActivate,% winTitle
      DO SOME STUFF
      Return
      } Else {
      MsgBox % msg
      }
   Return
   }
Not tested.
Lots of thanks, i see your point, but I think i havent explained myself well enough.
All my hotkeys try to activate and do some stuff in the same program (say notepad), but the stuff they do is different, for example F1 writes a string, F2 copies something, F3 writes, and prints in pdf, etc. What i try is not to repeat the same lines about looking for the program to be running or telling me its not running in case its not.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: function or similar for repeated code

11 Mar 2018, 18:30

This is the basis of the #IfWinActive directive.

Code: Select all

#IfWinActive, ahk_class Notepad
q::
MsgBox, % "Notepad: " A_ThisHotkey
return

w::
MsgBox, % "Notepad: " A_ThisHotkey
return
#IfWinActive ;this line isn't necessary, but I use it for clarity

#IfWinActive, ahk_class WordPadClass
q::
MsgBox, % "WordPad: " A_ThisHotkey
return

w::
MsgBox, % "WordPad: " A_ThisHotkey
return
#IfWinActive ;this line isn't necessary, but I use it for clarity

q::
MsgBox, % "not Notepad/WordPad: " A_ThisHotkey
return

w::
MsgBox, % "not Notepad/WordPad: " A_ThisHotkey
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
chocnmint
Posts: 6
Joined: 11 Mar 2018, 16:06

Re: function or similar for repeated code  Topic is solved

12 Mar 2018, 04:59

I would like to share the direction I'm currently heading:

In my script I have the keys of my keybard ordered by their position in the keyboard. I start from Escape, next F1, and so on from left to right and up to bottom.

Some keys that have to activate the same program (see example avobe), are next to each other, but others don't, so I wouldn't like to separate them in the script (clarity purposes). This issue will happen if i use something like this:

Code: Select all

#IfWinActive, ahk_class Notepad
q::
MsgBox, % "Notepad: " A_ThisHotkey
return

m::
MsgBox, % "Notepad: " A_ThisHotkey
return
#IfWinActive
Note that "q" and "m" are far away in the keyboard, and I want them to be that way in the script.

I've found this to work for me I guess: a subroutine with a temp variable.

Code: Select all

#SingleInstance, force

return

subroutine:
;subroutine: conditional to notepad existance
if WinExist("ahk_class Notepad")
{
	WinActivate
	Temp = 1
	return
}
else
{
	Msgbox, Needed program not running
	Temp = 0
	return
}
	

q::
gosub, subroutine
if (Temp=1) 
{
send, You pressed Q
return
}
return

m::
gosub, subroutine
if (Temp=1) 
{
send, You pressed M
return
}
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 275 guests