Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Duplicate function



  • Please log in to reply
13 replies to this topic
Coder_Chick
  • Members
  • 95 posts
  • Last active: Jul 08 2015 07:02 PM
  • Joined: 02 Jun 2014

Sorry I'm new to coding. Is there a way to have a section of code and then reuse that same code without defining new function.

 

The following code will activate the Chrome brower. However, I want to activate the browser is multiple sections of my script, but it won't let me paste this code in two places in my scripts because it says: Duplicate function definition. 

    { ;this will activate chrome
    WinActivate, % "ahk_pid " GetOldestPIDFromProcessName("chrome.exe")

GetOldestPIDFromProcessName(strProcessName) {

    for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {           
        nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
        nOldestPIDCreationDate := oProc.CreationDate    
    }
    return nOldestPID
}
}


Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

Take the function and place it at the bottom of your script. Then, simply call it when you need it.

GetOldestPIDFromProcessName("chrome.exe")
...
GetOldestPIDFromProcessName("chrome.exe")
...

GetOldestPIDFromProcessName(strProcessName) {

for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {
nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
nOldestPIDCreationDate := oProc.CreationDate 
}
return nOldestPID
}
You're getting an error because you can not have two functions with the same name and parameter list.

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


Coder_Chick
  • Members
  • 95 posts
  • Last active: Jul 08 2015 07:02 PM
  • Joined: 02 Jun 2014

I'm sorry I'm still not understanding. Can you please take my full code, add it once, put something in between (a msgbox), and add again as you are suggesting? I'm just not seeing it. Thank you so much for the help. Im new to this.



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

Alright, try this for instance:

 

MsgBox Hi, let's launch Chrome
GoSub Launch_Chrome
MsgBox How about a second time?
GoSub Launch_Chrome
MsgBox A third....?
GoSub Launch_Chrome
return
 
Launch_Chrome:
Run, chrome.exe
return

 

A function can work very similar to a GoSub routine. Instead of having

 

Launch_Chrome:
Run, chrome.exe
return

 

You have

 

GetOldestPIDFromProcessName(strProcessName) {

for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {
nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
nOldestPIDCreationDate := oProc.CreationDate 
}
return nOldestPID
}
}

 

And instead of GoSub, Launch_Chrome, you have GetOldestPIDFromProcessName("chrome.exe"). (Well, your code seems it would use it like so: WinActivate, % "ahk_pid " GetOldestPIDFromProcessName("chrome.exe").



Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

You didn't post your full code, as far as I can see. Just take the function and place it at the bottom of the script. That will allow the script to see the function exists so you can call it like you'd call any other function.

The function is:

GetOldestPIDFromProcessName(strProcessName) {

for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {
nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
nOldestPIDCreationDate := oProc.CreationDate 
}
return nOldestPID
}
Place that after all your other code, once.

I also suggest you read here to understand how functions work. You may also read here for a more detailed tutorial on functions. Obviously, that's not AHK, it's C++, but AHK is an interpreter written from C++, so they share the same structure, you just don't have to worry about data types and return-value types.

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


Coder_Chick
  • Members
  • 95 posts
  • Last active: Jul 08 2015 07:02 PM
  • Joined: 02 Jun 2014

Can you test using your chrome brower? It isn't working.

;chrome is minimized or not active

GetOldestPIDFromProcessName(strProcessName) {

for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {
nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
nOldestPIDCreationDate := oProc.CreationDate 
}
return nOldestPID
}

;chrome is minimized or not active

msgbox It should open when you click OK

WinActivate, % "ahk_pid " GetOldestPIDFromProcessName("chrome.exe")

;yay it opens, so i minimize it again, then press OK in the msgbox below

msgbox It should open again

;it doesnt open like it should


Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

msgbox It should open when you click OK

WinActivate, % "ahk_pid " GetOldestPIDFromProcessName("chrome.exe")

msgbox It should open again

WinActivate, % "ahk_pid " GetOldestPIDFromProcessName("chrome.exe")

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


Coder_Chick
  • Members
  • 95 posts
  • Last active: Jul 08 2015 07:02 PM
  • Joined: 02 Jun 2014

Ahh yes, but even with the fix, it doesnt work a second time

;chrome is minimized or not active

GetOldestPIDFromProcessName(strProcessName) {

for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {
nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
nOldestPIDCreationDate := oProc.CreationDate 
}
return nOldestPID
}

;chrome is minimized or not active

msgbox It should open when you click OK

WinActivate, % "ahk_pid " GetOldestPIDFromProcessName("chrome.exe")

;yay it opens, so i minimize it again, then press OK in the msgbox below

msgbox It should open again

WinActivate, % "ahk_pid " GetOldestPIDFromProcessName("chrome.exe")

;it doesnt open like it should


Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

If you pay close attention, you'll notice that it does activate, but it does not bring it forward. That's because of the msgbox. When the msgbox closes, it automatically falls back to what was behind it, that being Chrome (you can see this by looking at the icon on the taskbar). If you have it minimized, run the script, hit ok, re-minimize chrome, click/activate another window that isn't the msgbox, then hit ok again, it will work. To get around that, you can save the PID, check if it's minimized and restore the window if it is, then activate it.
 


msgbox It should open when you click OK
cpid:=GetOldestPIDFromProcessName("chrome.exe")
WinGet, windowState, MinMax, % "ahk_pid " cpid
if(windowState=-1)
    WinRestore, % "ahk_pid " cpid
WinActivate, % "ahk_pid " cpid
 
msgbox It should open again
cpid:=GetOldestPIDFromProcessName("chrome.exe")
WinGet, windowState, MinMax, % "ahk_pid " cpid
if(windowState=-1)
    WinRestore, % "ahk_pid " cpid
WinActivate, % "ahk_pid " cpid

Similarly, you can make it a function for condensed code (whole code).


msgbox It should open when you click OK
ActivateWindow("chrome.exe")
 
msgbox It should open again
ActivateWindow("chrome.exe")
 
GetOldestPIDFromProcessName(strProcessName) {
    for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {
        nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
        nOldestPIDCreationDate := oProc.CreationDate 
    }
    return nOldestPID
}
 
ActivateWindow(strProcessName){
    cpid:=GetOldestPIDFromProcessName(strProcessName)
    WinGet, windowState, MinMax, % "ahk_pid " cpid
    if(windowState=-1)
        WinRestore, % "ahk_pid " cpid
    WinActivate, % "ahk_pid " cpid
}

 

* I fixed the formatting, because it was bothering me.


OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


GeekDude
  • Spam Officer
  • 391 posts
  • Last active: Oct 05 2015 08:13 PM
  • Joined: 23 Nov 2009
✓  Best Answer

Very similar to what I had suggested she use on the IRC channel before she left without saying anything.

 

pid := GetOldestPIDFromProcessName("chrome.exe")

msgbox It should open when you click OK

WinActivate("ahk_pid " pid)

msgbox It should open again

WinActivate("ahk_pid " pid)
return

WinActivate(Title)
{
	WinGet, Out, MinMax, %Title%
	if (Out == -1)
		WinRestore, %Title%
	WinActivate, %Title%
}

GetOldestPIDFromProcessName(strProcessName) {
	for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {
		nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
		nOldestPIDCreationDate := oProc.CreationDate 
	}
	return nOldestPID
}


Coder_Chick
  • Members
  • 95 posts
  • Last active: Jul 08 2015 07:02 PM
  • Joined: 02 Jun 2014

Works great. However, one issue.

 

Test this. When it opens the first time, leave it open, then click the OK button, since the window is already open it obviously doesn't need to open, but it isn't activating it correctly. 

 

If I run this, and then do the shortcut to print Ctrl+P, the print box opens. Which is great.

GetOldestPIDFromProcessName(strProcessName) {
    for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {
        nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
        nOldestPIDCreationDate := oProc.CreationDate 
    }
    return nOldestPID
}
 
ActivateWindow(strProcessName){
    cpid:=GetOldestPIDFromProcessName(strProcessName)
    WinGet, windowState, MinMax, % "ahk_pid " cpid
    if(windowState=-1)
        WinRestore, % "ahk_pid " cpid
    WinActivate, % "ahk_pid " cpid
}


msgbox It should open when you click OK
ActivateWindow("chrome.exe")

exitapp

If I do the following code, and do not minimize the window after the first dialog box, when I press ok in the second msgbox, it does not let me use the Ctrl+P.

GetOldestPIDFromProcessName(strProcessName) {
    for oProc in ComObjGet("winmgmts:").ExecQuery("Select ProcessID,CreationDate from Win32_Process WHERE Name = '" strProcessName "'") {
        nOldestPID := (nOldestPIDCreationDate > oProc.CreationDate) ? oProc.ProcessID : nOldestPID ? nOldestPID : oProc.ProcessID
        nOldestPIDCreationDate := oProc.CreationDate 
    }
    return nOldestPID
}
 
ActivateWindow(strProcessName){
    cpid:=GetOldestPIDFromProcessName(strProcessName)
    WinGet, windowState, MinMax, % "ahk_pid " cpid
    if(windowState=-1)
        WinRestore, % "ahk_pid " cpid
    WinActivate, % "ahk_pid " cpid
}


msgbox It should open when you click OK
ActivateWindow("chrome.exe")

msgbox It should open again
ActivateWindow("chrome.exe")
 



Coder_Chick
  • Members
  • 95 posts
  • Last active: Jul 08 2015 07:02 PM
  • Joined: 02 Jun 2014

@Geekdude.... My apologies. I'm having to close my Chrome browser to do some of the testing. I greatly appreciate all the help.



Coder_Chick
  • Members
  • 95 posts
  • Last active: Jul 08 2015 07:02 PM
  • Joined: 02 Jun 2014

@Geekdude... Your example actually seemed to resolve the issue.



Coder_Chick
  • Members
  • 95 posts
  • Last active: Jul 08 2015 07:02 PM
  • Joined: 02 Jun 2014

Thanks again, all. I appreciate the help.