Jump to content

Get last active process


 Share

Recommended Posts

Hello!

My proces list is full of Notepad.exe, all of these open windows have this same ProcessName and ProcessClass. 

I editing example Notepad.exe (3) After edit i go to AutoIt and run script. 

Script need to know what was last processID of editing Notepad.exe.

Script cant be runned while i edit my notepad.

There is any way to return proces list by last usage or something similar?

@Melba23

Regards!

Ascer

 

Link to comment
Share on other sites

  • Moderators

Ascer,

Assuming that the Notepad window is still active then you can use this:

$iPID = WinGetProcess("[ACTIVE]")

to get the PID.

If the window is no longer active then you will have to get a bit more complicated and store the PID of the last active Notepad window which you get using the code above.

And just for interest, why do you need the PID?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks for request @Melba23!

Notepad.exe list was only example. My proces list cannot store information about last $pid or $hwnd in main window.

What i properly do:

Open proces.exe  -> Go to Deskop -> Open Folder called Data -> run AutoIt compiled script.

How to get $pid or $hwnd of proces.exe while i have more than one opened?

The class and Title is this same.

Edited by Ascer
Link to comment
Share on other sites

  • Moderators

Ascer,

Not too hard to do - this works for me with 3 Notepads open:

#include <Array.au3>

HotKeySet("{ESC}", "_Exit")

; Class of multiple windows
$sClass = "Notepad"

; Get list of these windows
$aNotepadList = WinList("[CLASS:" & $sClass & "]")
; And display it just for interest
_ArrayDisplay($aNotepadList, "", Default, 8)

; Declare variables
Global $hLastActiveNotepad = 0, $iLastNotepadPID

While 1
    ; Get handle of active GUI
    $hHandle = WinGetHandle("[ACTIVE]")
    ; Check if it is a Notepad
    For $i = 1 To $aNotepadList[0][0]
        If $aNotepadList[$i][1] = $hHandle Then
            ; if it is not the same instance as the last active Notepad
            If $hHandle <> $hLastActiveNotepad Then
                ; Store new handle
                $hLastActiveNotepad = $hHandle
                ; Determine PID
                $iLastNotepadPID = WinGetProcess("[ACTIVE]")
                ConsoleWrite($iLastNotepadPID & @CRLF)
            EndIf
            ExitLoop
        EndIf
    Next
WEnd

Func _Exit()
    Exit
EndFunc

But I am still intrigued as to why you need the PID.....

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

6 minutes ago, Melba23 said:

Ascer,

Not too hard to do - this works for me with 3 Notepads open:

#include <Array.au3>

HotKeySet("{ESC}", "_Exit")

; Class of multiple windows
$sClass = "Notepad"

; Get list of these windows
$aNotepadList = WinList("[CLASS:" & $sClass & "]")
; And display it just for interest
_ArrayDisplay($aNotepadList, "", Default, 8)

; Declare variables
Global $hLastActiveNotepad = 0, $iLastNotepadPID

While 1
    ; Get handle of active GUI
    $hHandle = WinGetHandle("[ACTIVE]")
    ; Check if it is a Notepad
    For $i = 1 To $aNotepadList[0][0]
        If $aNotepadList[$i][1] = $hHandle Then
            ; if it is not the same instance as the last active Notepad
            If $hHandle <> $hLastActiveNotepad Then
                ; Store new handle
                $hLastActiveNotepad = $hHandle
                ; Determine PID
                $iLastNotepadPID = WinGetProcess("[ACTIVE]")
                ConsoleWrite($iLastNotepadPID & @CRLF)
            EndIf
            ExitLoop
        EndIf
    Next
WEnd

Func _Exit()
    Exit
EndFunc

But I am still intrigued as to why you need the PID.....

M23

Thanks for time spent on writing code but it's useless for me.

Script need to know $pid or $handle just at start. I cant run script, do my things and script find last active $pid.

So there is any function to return $process list by last usage/active? 

Link to comment
Share on other sites

  • Moderators

Ascer,

Firstly, when you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unnecessarily.

Secondly, the code might well be useless for you - but you had not specified your precise requirements earlier in the thread, so how I am I supposed to guess precisely what you need unless you tell me?

Finally, you have the basis of what you require in that script - so unless you offer a lot more information on what exactly you are trying to do and just what these multiple processes actually are, I am out of here.

M23

 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Ascer,

I already pointed you to the Forum rules when I locked your first thread - and specifically mentioned the "no gaming" rule - and yet here you are again asking about game interaction. So the thread gets locked again - we take our rules seriously and enforce them rigidly.

And you have used up the final element of flex we were prepared to show you. Either follow the rules in future or leave the community - and we will force your departure if necessary. Your choice - please choose wisely.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...