PIDfromAnyID()

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

PIDfromAnyID()

10 May 2014, 15:43

Code: Select all

PIDfromAnyID( anyID="" ) { ; SKAN, 10-May-2014. http://ahkscript.org/boards/viewtopic.php?p=17974#p17974
  Process, Exist, %anyID%
  IfGreater, ErrorLevel, 0, Return ErrorLevel
  DetectHiddenWindows, % SubStr( ( ADHW := A_DetectHiddenWindows ) . "On", -1 )
  SetTitleMatchMode,   % SubStr( ( ATMM := A_TitleMatchMode )   . "RegEx", -4 ) 
  WinGet, PID, PID, ahk_id %anyID%
  IfEqual, PID,, WinGet, PID, PID, %anyID% 
  DetectHiddenWindows, %ADHW%
  SetTitleMatchMode, %ATMM%
Return PID ? PID : 0    
}
Usage:

Code: Select all

MsgBox % PIDfromAnyID()                                       ; Own PID          
MsgBox % PIDfromAnyID( "winlogon.exe" )                       ; process name
MsgBox % PIDfromAnyID( 4 )                                    ; validating raw PID

MsgBox % PIDfromAnyID( "MyScript.ahk" )                       ; PID of a running script
MsgBox % PIDfromAnyID( "MyScript.ahk ahk_class AutoHotkey" )  ; precise than above
MsgBox % PIDfromAnyID( "Torrent" )                            ; part of window title

MsgBox % PIDfromAnyID( A_ScripthWnd )                         ; Own PID

MouseGetPos,,, OutputVarWin 
MsgBox % PIDfromAnyID( OutputVarWin )                         ; PID of Window under mouse 
My Scripts and Functions: V1  V2
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: PIDfromAnyID()

10 May 2014, 17:11

thanks, tried and it is not just good...it is FANTASTIC! :ugeek:
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: PIDfromAnyID()

10 May 2014, 18:33

interesting!
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: PIDfromAnyID()

10 May 2014, 19:20

SKAN wrote:

Code: Select all

  SetTitleMatchMode,   % SubStr( ( ATMM := A_TitleMatchMode )   . "RegEx", -4 ) 
heh.

User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: PIDfromAnyID()

12 May 2014, 16:58

guest3456 wrote:
SKAN wrote:

Code: Select all

  SetTitleMatchMode,   % SubStr( ( ATMM := A_TitleMatchMode )   . "RegEx", -4 ) 
heh.
Yea, that is the most interesting thing about the script. I assume he is wanting to keep the saving of the old setting and the setting of the new on one line.

How about this for brevity:

Code: Select all

DetectHiddenWindows, % (ADHW := A_DetectHiddenWindows) ? "On" :
SetTitleMatchMode,   % (ATMM := A_TitleMatchMode) ? "RegEx" :
I might start doing it this way myself. I often save settings in a function to restore later.
Does not work though if the current setting is 0 but DetectHiddenWindows and SetTitleMatchMode do not have a 0 setting.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: PIDfromAnyID()

13 May 2014, 14:39

FanaticGuru wrote:How about this for brevity:

Code: Select all

DetectHiddenWindows, % (ADHW := A_DetectHiddenWindows) ? "On" :
SetTitleMatchMode,   % (ATMM := A_TitleMatchMode) ? "RegEx" :
I like it.. Thanks :)
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: PIDfromAnyID()

15 Jun 2014, 21:24

To keep the code valid, you should add something (any value) after the colon :
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: PIDfromAnyID()

16 Jun 2014, 13:13

lexikos wrote:To keep the code valid, you should add something (any value) after the colon :
It might not be valid but I do it often with no apparent problem. If you put nothing after the : it seems to return a null if the ternary is false.

Code: Select all

x := 13
x := 0 ? 1 :
if (x = "")
	MsgBox True
Now if you try to put another expression after the : by using a , you will have problems.
x := 0 ? 1 : , y := 7 will not work. Things fail and the contents of neither x or y are changed.
x := 0 ? 1 : {}, y := 7 will work and x gets changed to null and y gets changed to 7.

But if you want things to be more proper and bullet-proof you can do this:
DetectHiddenWindows, % (ADHW := A_DetectHiddenWindows) ? "On" : "On"
That way the ternary returns "On" whether it is true or false (if you are doing some A_ that can have a false value) and the ternary is valid and can contain another expression after it by the use of a comma.
DetectHiddenWindows, % (ADHW := A_DetectHiddenWindows) ? "On" : "On", y := 7

Having a proper ternary structure is also safer for future updates that might change how AHK handles things when it finds nothing after the colon.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: PIDfromAnyID()

16 Jun 2014, 13:28

FanaticGuru wrote:x := 0 ? 1 : {}, y := 7 will work and x gets changed to null and y gets changed to 7.

Code: Select all

x := 0 ? 1 : {}, y := 7
MsgBox, % IsObject(x)
Why not x := 0 ? 1 : ""?
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: PIDfromAnyID()

16 Jun 2014, 15:38

kon wrote:
FanaticGuru wrote:x := 0 ? 1 : {}, y := 7 will work and x gets changed to null and y gets changed to 7.

Code: Select all

x := 0 ? 1 : {}, y := 7
MsgBox, % IsObject(x)
Why not x := 0 ? 1 : ""?
True, I didn't intent {} to be interpreted as an Object but as a empty block. Anything can be there just not "nothing", I was just trying to put what I thought was the closest thing to nothing which in my mind is an empty block like below where an empty block is put after a While command.

Code: Select all

 While func(5)
{
}
MsgBox FINISHED

func(x)
{
	Random, R, 1, x
	if (R = 1)
		return false
	else
		MsgBox % R
	return true
}
Basically a case where you want to repeat a function till the function returns false.

I assume this is still being interpreted as a empty block but maybe it is not.

Code: Select all

While func(5)
{}
The basic point being that you can have a defined nothing after the colon but not just a nothing nothing; if you want to have another expression on the same line and be proper. If you don't want to have another expression on the same line and don't care about being proper then you can have truly nothing after the colon. x := 0 ? 1 : works just fine in that case.

I hope nothing is confusing! Is that confusing?

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 254 guests