Cannot find .exe with Pywinauto's find_window(title="program.exe") - python

Does anyone know the trick to pywinauto's find_window function? I am building an application with kivy, and trying to use pywinauto to bring an .exe to the foreground, using the following code:
SetForegroundWindow(find_window(title='program.exe'))
I simply want to identify a currently open .exe, and bring it to the foreground. I have looked here https://pywinauto.github.io/docs/code/pywinauto.findwindows.html and it seems "title=" is what I want.
Does anyone know how to point to the .exe with pywinauto?

I think title is for window title (i.e. "python - Cannot find..." in case of this tab), are you sure it not more like "process='program.exe'" ?
if it needs to be and int then its pid (process id) and you can use this to get process id by title:
import win32gui,win32process
def get_window_pid(title):
hwnd = win32gui.FindWindow(None, title)
threadid,pid = win32process.GetWindowThreadProcessId(hwnd)
return pid
Eventually have at this answer as it contains really nice class for getting windows Python Window Activation, i dont want to copy paste, but use it and then you can do:
w = WindowMgr()
w.find_window_wildcard(".*Hello.*")
w.set_foreground()

find_window is low level function I wouldn’t recommend to use.
The right thing is Application object connected to the target process. It can be used so:
from pywinauto import Application
app = Application(backend=“uia”).connect(path=“program.exe”)
app.WindowTitle.set_focus()
If you have several app instances, there is a Desktop object to walk all the windows in the system:
from pywinauto import Desktop
Desktop(backend=“win32”).window(title=“Window Title”, found_index=0).set_focus()
You referred to the old docs for version 0.5.4, the latest one is 0.6.4 with two backends available and many bug fixes. The Getting Started Guide link on the main page is a good source to learn the main concept.

Related

more info or docs for pyvda, or a better module:

import time
import subprocess
from pyvda import AppView, get_apps_by_z_order, VirtualDesktop, get_virtual_desktops
#clockify
subprocess.Popen("C:/Program Files/Clockify/ClockifyWindows.exe")
current_window = AppView.current()
target_desktop = VirtualDesktop(7)
current_window.move(target_desktop)
VirtualDesktop(7).go()
trying to load specific programs to specific desktops in windows 11; so, my "clockify" app loads on the desktop "clock" and blender loads on the blend desktop so on so on.
cannot find any docs for this mofo, but pieced together what's above.
it would be awesome to:
a) check if a virtual desktop exist, and create it, incase of accidental deletion.
but really what I need is a way to specify the "current window". right now (i'm assuming python sees the that as whatever window is active as the current window); I have to put the sleep timer inbeteen loading programs, which is fine.
b) need a way to specify what window as active, then i can tie that to the appropriate desktop and maybe load them all at once to save time
#still a newb and no formal training. and alot of aconyms confuse me, and obvious assumptions may not be obvious to me.
cheers,
-mda

Python Pywinauto no control_identifiers or children in application

Hey everyone I'm having a hard time automating a specific application. Using pywinauto I want to automate clicking, keypresses, etc. to login and benchmark this application but for some reason I can't find any control identifiers for this application. Am I doing something wrong? I used this same method with task manager and other applications and this works fine.
Important Documentation:
Code Example
Class Doc
Code:
import pywinauto
app = pywinauto.application.Application()
window_handle = pywinauto.findwindows.find_windows(title = u'Name of application')
#print window_handle #makes sure to see if handle exists
window = app.window_(handle = window_handle[0])
print window.Children() #first approach
print app.top_window_()._ctrl_identifiers() #second approach
Output:
>>>[]
>>>{}
Has this happened to anyone before and has found a way around it? Should I just resort to using pywin32 instead? Thank you!
Turns out the application does have control identifiers I just needed to get past the first screen. Using window.TypeKeys("{TAB}{TAB}{ENTER}") I was able to navigate past the home screen with keypresses and into the actual application which had all the identifiers.
Useful Links if anyone encounters this problem.
What is a control Identifier?

Is it possible to get current application running with python script

I am very much concerned about my productivity all the time. I have recently come across this beautiful chrome extension Limitless
But this is only measuring what i'm doing within the chrome application. As I work most of the time with pdfs, videos etc, I want to develop similar application for linux(ubuntu) desktop enviroment.
Basically I want the script to run continuously as long as the workstation is on.
It should be able to know what I'm currently looking at (for eg a pdf file or a lecture video in vlc) and get the name of the respective file, start time, end times etc and finally post to db.
It is better if it could know if the system is idle or at sleep.
I don't have slightest clue at bash scripting. so my questions is could this task be accomplished with python.
What I've tried?
I started with a search in google "get current application python", "current window title python" etc etc and really surprised to see absurd results.
Please give me pointers on this.
I think you are asking for vocabulary. So I give you what I know.
You are using Ubuntu so your Window Manager may be Gnome.
The window manager knows which window has the focus.
So maybe you want to find out which window has the focus and you want to map it to the Process that opened the window.
What you need to focus on is is module for Python or a Python Binding for the window manager. This module is likely to also be able to control the windows.
The window manager is started with startx.
You could try to call a command line tool and catch the results
How do get the process list on command line:
https://stackoverflow.com/questions/53489/how-do-you-list-all-processes-on-the-command-line-in-windows
And how to call a tool with python:
Python subprocess.call and subprocess.Popen stdout
[edit] Repeating the call in Intervals and counting the intervals a process were running gives you a good estimation of running time of a process...
[edit2] As GreenAsJade said, you search a way to find out which windows has the focus.
See How do I detect the currently focused application?

pywebkitgtk: Headless + full DOM interaction possible?

I am currently using the Python Webkit DOM Bindings to interact with a website programmatically and that's working for me.
The only problem is that it insists on opening a GTK window to display the page. Has somebody figured out a way to prevent it from opening a window? I.e. to use it in a headless way?
I'm initializing the view like this:
wv = pywebkitgtk.WebView(1024, 768, url=url)
which implicitly opens the GTK window and then I have an onload event-handler to manipulate the DOM.
I first thought of subclassing WebView, but that's not possible because it is a compiled class.
Any other ideas?
I'm the developer responsible for pythonwebkit, and I have a great deal of expertise covering these areas across several platforms. Realistically, you really, really want a completely "headless" WebKit port. In pythonwebkit that actually shouldn't be too hard to do, as there are only three "entry point" functions (one for window, one for document, and one for XMLHTTPRequest).
Really, somebody should do a proper "completely headless" port of WebKit. There already is an example program which is pretty close in WebKit's source tree, maybe that will get you started.
I've been using PyQT. PyQTWebView runs on Webkit and works great. Check out Ghost.py to get started, or use PyQT's API directly. Runs fully headless, and supports a decently recent build of Webkit.
You could try using Xvfb. I like using the command line and setting my display manually, but if you don't like that you could use this: http://cgoldberg.github.io/xvfbwrapper/
Can you get a handle to the GTK window and then call window.hide()? Otherwise, you might just have to use the full Webkit library.
Create a window and add the webview there, and never show the window..
I have webviews running without showing them, and can call a show_all if I need to show them.
web_view = pywebkitgtk.WebView()
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
sw = gtk.ScrolledWindow(hadjustment=None, vadjustment=None)
sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_NEVER)
sw.add(web_view)
window.add(sw)
#window.show_all()

Listen for newly opened windows with a specific title

Is there a way to bind the execution of some Python code to the opening of a window with a specific title?
I know there are some ways to list all the currently open windows, but it would be nice to not have to constantly poll.
You should look into windows hooks. Specifically, the WH_SHELL hook should allow you to monitor window creation.
PyAA seems like it might be the only module that will let you do this. The tutorial for it is gone but I managed to find an archived copy of it.
Section of tutorial covering Event Watchers
The module

Categories

Resources