Isn't there a PRIMARY selection clipboard in windows - python

First of all, its a windows question. Let me introduce you to the linux counterpart.
In Linux, when I select a text anywhere in X Windows System, its gets copied to PRIMARY clipboard.
Selecting a clipboard & explicitly copying it using Ctrl+C causes it to be copied to secondary keyboard.
In linux, I made a dictionary that automatically searches for word that is selected in current window. For that, I just copied the value of primary clipboard.
What's the equivalent of PRIMARY selection in windows? I want to retrieve the current selection in the current window using python.

There is no equivalent in Windows. There is just the single global clipboard which contains a single item, albeit in potentially multiple formats.
I want to retrieve the current selection in the current window.
That's not trivial in Windows. You can do it using, for example, UI Automation, for apps that support UI Automation. Most modern apps do support that but more obscure ones will not readily yield the information you are after.

Related

Issues with 'send_keys(Keys.CONTROL + "A"), only works on Windows or Linux

I am working on an Automation Framework project (Pytest and Selenium), I and have several flows where I need edit data in forms, so the approach that we have been using is that we select the existing text (from the textbox or drop-down) using Send Keys and then just "clicking" CONTROL + A, and after that we use Send Keys as well to click the Backspace to delete the text and add the new one.
This works perfectly on Windows and Linux, but obviously it does not work on Mac computers, we thought about it when we implemented the solution, but we didn't have issues because everyone was using Windows computers (also the server in the CI).
I've been looking for solutions and it seems that there are lot of people with the same issue, but most the solutions do not work for everyone. I tried the following:
Double clicking to select the text --> didn't work
Do not delete the text, just add new text to the existing one --> only works for textboxes, does not work for drop-downs.
Detect the OS and with that use Command if it detects Mac and Control if it detects Windows or Linux --> but it didn't work.
I think that the main issue is that Ctrl does exist on Mac, but it does not work for selecting text, so it performs the action, but it doesn't do what we expect.
Is there a way to select text that will work on all OS, or is there a cleaner way to do it with Python?
Added the following if statement in the config.py file:
self.control_os = Keys.COMMAND if platform.system() == 'Darwin' else Keys.CONTROL

Reading selected text into a Python function without altering the clipboard, not even altering the multi-clipboard

I want to select some text with the mouse or keyboard, alter it using a Python function (e.g. replace a word with a different word), and then paste the altered text. I want to do this without changing what’s on the clipboard. I am not content to preserve only what is on the first slot of the clipboard; I want to preserve what is on every slot of the clipboard, or at least the first e.g. 50 slots. My question is how to do that?
I’m interested in doing this on Windows with Python.
Background: Windows recently introduced a native multi-clipboard (i.e. a clipboard with multiple slots) which can be accessed by pressing Windows-v (see here), and there also are third-party apps such as Copyq and Ditto which provide a multi-clipboard. When you copy something onto the Windows multi-clipboard, the thing you copied gets put on the first slot and everything else gets shifted down by one slot. A solution that uses the Windows multi-clipboard rather than a third-party application would be preferable but not essential. Pyperclip is a popular tool for reading text into Python functions from the clipboard and passing text back to the clipboard from Python functions. However, Pyperclip seems to only be able to interact with the first slot on the clipboard.
My current approach: 1) store the previous contents of the clipboard in a temporary variable, 2) copy the selected text into the clipboard, 3) pass the copied selected text into a Python function via Pyperclip, 4) alter the text using the Python function, 5) put the altered text back onto the clipboard using Pyperclip, 5) paste the altered text, 6) and finally put the original clipboard content back on the clipboard (using the temporary variable). But this approach is not good enough because it changes what is on the higher slots of the clipboard e.g. the second slot. In particular, it adds things to the clipboard thus shifting everything else down.
Is there a way to programmatically delete the extraneous content from the multi-clipboard once I am done with it? For example, once I pass the selected text from the clipboard into the Python function (where it will be manipulated), I would want to use the Python function to remove the selected text from the clipboard since it is no longer needed there. (I know you can delete text from the clipboard using the GUI, but I want to do so by using a Python function.) Alternatively, if there is a way to read selected text into a Python function (and paste it back out) without ever putting it on any slot of the multi-clipboard that would work too.
Thanks

Find new window dialogs automatically

Let's say that I open up some word file with a macro that opens up a dialog with some buttons.
Is there a way to find those buttons automatically and press them (when having only the PID)?
Currently, I'm using pywinauto to automate the GUI testing. If there's a way to do it with pywinauto that would be great.
Thanks.
To summarize all the comments:
It's possible to enumerate all the windows and their controls using methods .windows() (for top-level windows), immediate .children() and all the .descendants() (the whole subtree as a plain list). You can even filter children and descendants by class_name, control_type, content_only and/or title.
Example:
print(app.windows()[0].descendants(control_type='Edit'))

How to programmatically retrieve the equivalent of the "name" field in microsoft's inspect.exe?

I want to retrieve information from a tooltip in the system tray programmatically.
The image shows the tooltip.
Now, I found that by using Microsoft's inspect.exe, which is "a Windows-based tool that enables you select any UI element and view the element's accessibility data", that it is in theory possible to retrieve this value programmatically.
Hovering over the the pandora icon in the toolbar shows me the following properties
It shows one property "name" that contains the exact data I need. I'm unsure how to retrieve this value programmatically using the win32api. I have a hwnd to the pandora icon already.
Additionally, a different ui spy tool, UiSpy.exe calls this same property "helpText" (different song name :p)
I tried using getWindowText(pandoraSystrayIconHwnd) but that returns a different text. Does anyone know what this "name" value is, and how I can retrieve it using the win32api? It should be possible because inspect.exe is an external program that can access the data somehow
I'm doing this in Python, as the target application is written in Python already.
These spy apps are probably using Active Accessibility and/or UI Automation.
You can try calling AccessibleObjectFromWindow on the toolbar HWND or AccessibleObjectFromPoint if you care about the mouse position and then call IAccessible::get_accName.
Keep in mind that the classname and window hierarchy of the tray icon toolbar is undocumented.
If you only care about Pandora and not other applications then I would strongly suggest that you look for other alternatives first. Perhaps they have a hidden window with the title etc.
If you don't mind hacks then you could take a look at TraySaver, it is open source and knows the internal format of the data stored for each icon in the tray toolbar. Keep in mind that it is pretty old and might not work on newer versions of Windows. If you go down this path (and I don't recommend it) then you have to remember that you need to support both 32-bit and 64-bit Explorer.
Maybe GUI automation library pywinauto could help you. It uses Win32 API or UI Automation under the hood (by your choice). Core concept is described in the Getting Started Guide.
Method .window_text() returns exactly the same as Name property shows in Inspect.exe.
To interact with tray area icons you can use this example on StackOverflow.

How to paste text from the clipboard into a text field

How can I paste a string into a text field, like for example the Windows search bar or a text editor using Python?
I searched a lot for this, but all I can find is hundreds of questions asking how to copy to the clipboard or getting a string from the clipboard. What I want to do is paste from the clipboard into the active Window, as if I were pressing ctrl+v. If possible, I want to avoid the seemingly complicated way of emulating the actual low-level keyboard press.
In Windows clipboard is considered IPC (Inter process communication).
You can read some of the details here.
In python you can use this library I think supporting major OS.
For linux specific are options like xclip but I think it's desktop environment dependent.

Categories