Jump to content

Getting title from processname?


Recommended Posts

Hello,

I've used Autoit for a while now and i must admit that im impressed.

However when i use WinGetTitle() it can interfere with other processes that contains the same Title.

Let's say i've got a program1.exe running with title "Test" and program2.exe running with the same title.

I want to get title from just one of them because they contain diffrent titletext.

Is there a way to do that?

Link to comment
Share on other sites

Perhaps this helps:

#include <array.au3>
$iPid = Run("notepad.exe")
Sleep(100) ; allow notepad to create window
$aArray = _WinGetInfoByProcess($iPid, 1)
_ArrayDisplay($aArray, "by Pid, only visible windows")
$aArray = _WinGetInfoByProcess("notepad.exe", -1)
_ArrayDisplay($aArray, "by Name, all windows")
$aArray = _WinGetInfoByProcess("notepad.exe", 0)
_ArrayDisplay($aArray, "by Name, only invisible windows")
$aArray = _WinGetInfoByProcess("notepad.exe", 1)
_ArrayDisplay($aArray, "by Name, only visible windows")
$hHandle = _WinGetInfoByProcess("notepad.exe") ; default is $nShow = 2
ControlSend($hHandle, "", "", "{alt}{right 2}{down 2}{enter}")
Sleep(300 * ControlSend("[active]", "", "", "{tab 2}{BS}72{enter}")) ; allow window to change
ControlSend("[active]", "", "", "{enter}     Hello World.")

Func _WinGetInfoByProcess($vProcess, $nShow = 2)
    ;
    ;===============================================================================
    ;
    ; Function Name:    _WinGetInfoByProcess
    ; Description::     Get Window Handle of Process
    ; Parameter(s):     $vProcess = Processname(e.g. "Notepad.exe") or Processnumber(e.g. 4711 )
    ;                   $nShow = -1 "All (Visble or not)"
    ;                   $nShow = 0 "Not Visible Only"
    ;                   $nShow = 1 "Visible Only"
    ;                   $nShow = 2 "Return handle of newest  window " (Default)
    ; Requirement(s):   none
    ; Return Value(s):
    ;                   @extended = returns number of entries in the array
    ;                   @error = 0 AND $nShow = 2 (default)  returns handle of newest window
    ;                   @error = 0 returns array with processinfos
    ;                              n = 1 to @extended
    ;                              Array[n][0] shows windows title.
    ;                              Array[n][1] shows windows handle.
    ;                              Array[n][2] shows windows Pid.
    ;                   @error = 1 Process not found.
    ;                   @error = 2 Window not found.
    ; Author(s):        inspired by Smoke_N's script _WinGetHandleByPID()
    ;                   but to handle more than one process with the same name
    ;                   and to return handle of newest window ($nShow = 2).
    ; tested versions:  Autoit 3.3.0.0 
    ;
    ;===============================================================================
    ;
    If Not ProcessExists($vProcess) Then Return SetError(1, 0, 0) ; no matching process
    Local $iWinList, $aWinList = WinList()
    Local $iResult, $aResult[UBound($aWinList)][3]
    Local $iProcessList, $aProcessList = ProcessList($vProcess)
    If $aProcessList[0][0] = 0 Then Local $aProcessList[2][2] = [[1, 0],["", $vProcess]]
    For $iWinList = 1 To $aWinList[0][0]
        For $iProcessList = 1 To $aProcessList[0][0]
            If WinGetProcess($aWinList[$iWinList][1]) = $aProcessList[$iProcessList][1] Then
                If $nShow > -1 And Not $nShow = (2 = BitAND(WinGetState($aWinList[$iWinList][1]), 2)) Then ContinueLoop
                $iResult += 1
                $aResult[$iResult][0] = $aWinList[$iWinList][0]
                $aResult[$iResult][1] = $aWinList[$iWinList][1]
                $aResult[$iResult][2] = $aProcessList[$iProcessList][1]
            EndIf
        Next
    Next
    If $iResult = 0 Then Return SetError(2, 0, 0) ; no window found
    ReDim $aResult[$iResult + 1][3]
    $aResult[0][0] = $iResult
    If $nShow = 2 Then Return SetError(0, $iResult, $aResult[1][1])
    Return SetError(0, $iResult, $aResult)
EndFunc   ;==>_WinGetInfoByProcess

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Hello,

I've used Autoit for a while now and i must admit that im impressed.

However when i use WinGetTitle() it can interfere with other processes that contains the same Title.

Let's say i've got a program1.exe running with title "Test" and program2.exe running with the same title.

I want to get title from just one of them because they contain diffrent titletext.

Is there a way to do that?

I don't know what you mean by having the same Title, but having different titletext? Perhaps you meant different window text?

Anyway, you should get the window's handle and work with that, as it remains unique for the life of the window, even when title/text are changed. INSTANCE can work, as suggested by JackDinn, but the instance is transient based on desktop Z-order, not the order in which the GUIs were created.

$hWin = WinGetHandle("[CLASS=My Class;TITLE=My Title]", "Window text here")

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hmm, the above methods dont work for me.

Ill put an example:

Window1: Title "Player - Guns'N Roses - Paradise City"

Window2: Title "Player Official Website - Mozilla Firefox"

When i use stringsplit on the title it will trow me an error. Because there is two titles.

$title = WinGetTitle ("Spotify")
        $title = StringTrimLeft($title, 10)
        $title = StringSplit($title, chr(150))
        $title[1] = StringTrimRight($title[1], 1)
        $title[2] = StringTrimLeft($title[2], 1)
        $title[1] = urlencode($title[1])
        $title[2] = urlencode($title[2])
Link to comment
Share on other sites

Hmm, the above methods dont work for me.

Ill put an example:

Window1: Title "Player - Guns'N Roses - Paradise City"

Window2: Title "Player Official Website - Mozilla Firefox"

When i use stringsplit on the title it will trow me an error. Because there is two titles.

$title = WinGetTitle ("Spotify")
        $title = StringTrimLeft($title, 10)
        $title = StringSplit($title, chr(150))
        $title[1] = StringTrimRight($title[1], 1)
        $title[2] = StringTrimLeft($title[2], 1)
        $title[1] = urlencode($title[1])
        $title[2] = urlencode($title[2])
Huh? :party:

Then make your WinGetTitle() more specific with additional parameters so it only finds the unique match you want.

On re-reading the title of the OP, are you just looking for a window to associate with a PID? Did you get anything out of forumer100's post? You could also check it with WinList() and WinGetProcess() to find the one owned by the PID you want.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

INSTANCE can work, as suggested by JackDinn, but the instance is transient based on desktop Z-order, not the order in which the GUIs were created.

Oh, always assumed that, good to know :)...
Link to comment
Share on other sites

well if your still having problems maybe this might help

$x = 1
While WinExists("[TITLE:Spotify; CLASS:Notepad; INSTANCE:" & $x & "]")
    $tit = WinGetTitle("[TITLE:Spotify; CLASS:Notepad; INSTANCE:" & $x & "]")
    $title = StringRegExp($tit, "Spotify - (.+)",3)
    ConsoleWrite(' : $title = ' & $title[0]  & @crlf)
    $x += 1
WEnd

dont forget to change the CLASS:Notepad to what you need, use the AI info tool to find out.

it will just look for all windows whos name is starting with "Spotify" then it removes the "Spotify - " from the beginning and gives you the rest of the name. im sure you can deal wit it from there :)

Edited by JackDinn

Thx all,Jack Dinn.

 

JD's Auto Internet Speed Tester

JD's Clip Catch (With Screen Shot Helper)

Projects :- AutoIt - My projects

My software never has bugs. It just develops random features. :-D

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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