Executing context menu functions on windows desktop programmatically - python

So I have a shortcut to a personal area network device when I right-click the icon the context menu comes up with an option "connect Using -> access point".
Context menu image
I want to be able to do this operation automatically. I know that a program like AutoHotkey could do this very easily but I will be doing this operation multiple times per minute and if it is possible to do this in a command prompt or using python it would make my life easier

I have found a relatively non invasive method using pywinauto its not ideal for my application but it still works so I thought I would share
from pywinauto import Desktop, Application
Application().start('explorer.exe "path"')
app = Application(backend="uia").connect(path="explorer.exe", title="name1")
app2 = Desktop(backend='win32') #for sub actions only
app.folderName.set_focus()
common_files = app.folderName.ItemsView.get_item('fileName')
common_files.right_click_input()
app.ContextMenu.actionName.click_input()
app2.PopupMenu.menu_item('subActionName').click_input() #for sub actions only
app.window(title="name1").close()

Related

How do I programmatically video record a window / GUI application in python?

I would like to video record a GUI application (specifically RViz) programmatically since I am running an experiment multiple times and would like to automate and synchronize those recordings with the run. RViz itself offers no recording service and recommends SimpleScreenRecorder. Are there any libraries that offer this functionality?
System: Linux (Ubuntu)
Preferred language: python
I didn't find any suitable tools so I wrote up my own that should work on all Linux systems: https://github.com/LemonPi/window_recorder
You use it as a context manager, supplying either no window names to prompt you to click a window, or a list of window names to attempt to capture (the first one that's valid gets matched). For example,
from window_recorder.recorder import WindowRecorder
import time
# passing in nothing as the window name will allow you to select the window by clicking
# want to capture an RViz window, which could have name "RViz*" as well
with WindowRecorder(["RViz*", "RViz"], frame_rate=30.0, name_suffix="rviz"):
# do things...
time.sleep(0.1)
start = time.time()
while time.time() - start < 2:
time.sleep(0.1)

How to make Pywinauto in Python click a button in a different language?

I just can't get Pywinauto to work.
Basically I want it to open the system settings ( figured that out already) and then click "Change Settings" but in my language (German) which would be "Einstellungen ändern".
I've tried this:
from pywinauto import Desktop, Application, keyboard
from pywinauto.application import Application
app = Application().start("control system")
#so far it works, after that I've tried two options
#1
app.window_(title_re="System").window_(title="Einstellungen ändern").click()
#2
app.window_(best_match="System" ).window_(best_match="Einstellungen ändern").click()
I've tried both of these options with the AutomationId, which I got from Inspect.exe, instead of "System" or "Einstellungen ändern" and I've tried ClickInput() instead of click().
Any ideas?
There are few issues:
Correct backend is "uia" that must be specified for Application object.
Launcher process spawns a subprocess which requires to reconnect to this child process.
This code works for my English Win10:
from pywinauto.application import Application
app = Application(backend="uia").start("control system")
app = Application(backend="uia").connect(title="System", timeout=20)
app.window(title="System").child_window(title="Change settings").invoke()
# app.window(title="System").child_window(title="Einstellungen ändern").invoke()
.click_input() should work as well. Backend "uia" defines method .click() as an alias of .invoke() for control_type="Button" only, because InvokePattern can have different meaning for various control types.
NOTE: After clicking on "Change settings" the appeared "System properties" window is running inside another process which requires method .connect() again and maybe separate Application instance for your convenience.

SendKeys method of AutoIT, shell script or pywinauto lib is not working

Scenario is, I want to handle IE pop up in robot framework with python. To handle pop up, I used AutoIT SendKeys, shell script, pywinauto TypeKeys.
The code is working properly when running on a local machine but when I run the script through team city on the agent machine, then it fails.
My observation is that in the agent machine the code is not working to handle the windows pop up.
After analysis, I have found that this is because the agent machine is working as a locked system while running script through team city.
Code:
def fhandle_savepopup(self):
shell = win32com.client.Dispatch("WScript.Shell")
seleniumLib = BuiltIn().get_library_instance('Selenium2Library')
shell.AppActivate ("Internet Explorer")
sleep(2)
autolib = BuiltIn().get_library_instance('AutoItLibrary')
shell.SendKeys('{F6}')
sleep(3)
shell.SendKeys('{TAB}')
sleep(3)
shell.SendKeys('{ENTER}')
I used AutoIT and pywinauto lib too. But no luck.
Please help me find a solution for this.
You need to keep your desktop open and non-locked (it can be solved by setting auto-logon up + VNC session like TightVNC). For TightVNC you might also require video card drivers update and set custom resolution (in NVIDIA control panel, for example). Of course this node must be connected to the Team City master node from this open desktop (not as a service).
Another option is to click window buttons silently (without real user input emulation). In pywinauto it can be done using Click() (ClickInput() is more realistic and won't work for locked system):
app.Window_(title="Popup title").Wait('visible', timeout=10)
app.Window_(title="Popup title").OK.Click()
P.S. TypeKeys() also works as a real user input. We have plans to implement silent version (SendChars) using WM_CHAR window message that might work for you. But pywinauto 0.6.0 release is currently in deep re-factoring. So it might be released in April or even May, but with great WPF support.

How to make a clickable control that is always on top, in Windows using Python?

The goal is to have an application that runs on top of all other applications and windows in the desktop, and display a clickable control (say, an image button) that moves around the screen.
The application must run as a service in the background and show thebutton (let's say) each hour, once clicked it disappears until the next hour.
This application has to be written in Python.
It looks like PyQt is one of the better options, but I'm not sure if it does support this sort of functionality and if it is a good alternative for modern Windows applications.
What packages or frameworks are appropriate for this scenario? I have seen Pygl and PyGame but they seem to be limited to a window, is this correct?
You actually don't need to create the program as a service. You can just start the application and not show the window immediately. You can use PyQt or wxPython. I'm more familiar with wxPython, so if you went that route, you could use a wx.Frame style flag such as wx.STAY_ON_TOP to get the functionality you want.
I have created applications that load up in the system tray with just an icon. When you click the icon, it shows the frame. The rest of the time, the frame is hidden. I would try that route before looking at doing a service.

How do I control a non-browser window that is part of Firefox?

I'm on OSX using Python 2.x, Selenium & Firefox
I'm automating testing a javascript webapp with Python & Selenium.
One of the links (Add File) in the application opens up a non-browser firefox window titled "File Upload" which looks like (/is?) a Finder window.
Is there a way that I could locate and control this window from my python script? I know Selenium can't do it, but I wondering if it might be possible with something like 'import applescript' and if so how?
I found atomac which allows me to control mac apps through their accessibility controls (which needed to be enabled on Mavericks for Aptana in System Preferences -> Security & Privacy -> Privacy -> Accessibility). Cool tool, but the documentation is pretty sparse. The examples provided on the page above got me to the point where I could close the window via the cancel button, but I had to review the function definitions in atomac's AXClasses.py to figure out the rest. Here's the solution.
import atomac, time
from atomac.AXKeyCodeConstants import *
# to allow me to make firefox frontmost while testing
time.sleep(5)
# get a reference to the running app
firefox = atomac.getAppRefByLocalizedName('Firefox')
# get the window of the reference
firefoxwindow = firefox.windowsR()[0]
# send key sequence to go to my home folder
firefoxwindow.sendKeyWithModifiers('h',[COMMAND,SHIFT])
# send key sequence to select first file there
firefoxwindow.sendKeyWithModifiers('a',[COMMAND])
# press the now active Open button
openbutton = firefoxwindow.buttons('Open')[0]
openbutton.Press()
It's theoretically possible, but really awkward. I'll give you a bunch of links--not ideal, I know, but you could write a book on this.
You'd need to start by enabling AppleScript control of the GUI. Then you'll want to read up on how to control the GUI from within Applescript. However, you wanted to use Python and not AppleScript, so then you'll need to install PyObjC, which is a Python to Cocoa bridge. You'd need to use the Scripting Bridge framework and figure out (from the extremely thin documentation) how to translate the AppleScript docs to Python.

Categories

Resources