I would like to see what is the best library to use in order to do some automations under linux x environment.
I have an application that is running in a window (I use Ubuntu but I need to make sure it would work under another distribution). In this window that can be anywhere in my environment, I want to be able to execute some click at specific x,y coordinates then monitor also some squares to see if I have an image coming and when it does, execute some other clicks at different places. This would allow me to automate some applications I have.
Could you please tell me how to best do that with python 2 or 3?
Use pyautogui module. The docs are here: https://pyautogui.readthedocs.org/en/latest/. Use pyautogui.click().
Related
I'm primarily using Windows, where I run WSL2. So from a python script running in the subsystem, I would like to screenshot whatever is on windows monitor, as simple as such:
v1
import mss
import os
os.environ['DISPLAY'] = ':0'
with mss.mss() as sct:
sct.shot()
This gives only gives "Segmentation fault" error and no image. So I tried to setup vcxsrv in Windows and I'm able to open stuff from my subsystem in Windows through the server, however I cant get it the other way around..
I just want access to the windows screen so I can screenshot it. Any help about how to access the monitor through wsl would be greatly appreciated, I can't find much on google..
The problem with your attempted solution is that the WSL/Linux Python's mss, as you've found, isn't able to capture the Windows desktop. Being the Linux version of MSS, it will only be able to communicate with Linux processes and protocols like X. Starting up VcXsrv might get you part of the way there, in that you might be able to capture X output, but you may need to be running the Python app from inside a terminal that is running in a X window.
Regardless, you've said that your goal is to capture the entire Windows desktop, not just the X output in VcXsrv. To do that, you'll need to use a Windows process of some sort.
But don't worry; using WSL's interop, you can still do that from inside WSL/Linux Python. We just need to call out to a Windows .exe of some sort.
There are literally dozens of third-party apps that you could use in Windows to create a screenshot. But I prefer to use a solution that doesn't require any additional installation.
So I resorted to PowerShell for this, since you can easily call powershell.exe and pass in a script from WSL. There are a number of examples here on Stack Overflow, but I ended up going slightly "lower tech" to try to simplify a bit. The code here is most similar to this solution, so refer to that if you want to expand on this.
From WSL/Linux Python:
import os
os.system("""
powershell.exe \"
Add-Type -AssemblyName System.Windows.Forms
[Windows.Forms.Sendkeys]::SendWait('+{Prtsc}')
\$img = [Windows.Forms.Clipboard]::GetImage()
\$img.Save(\\\"\$env:USERPROFILE\\Pictures\\Screenshots\\screenshot.jpg\\\", [Drawing.Imaging.ImageFormat]::Jpeg)\"
""")
That essentially sends the ShiftPrintScreen key chord to capture the current desktop to the clipboard, then saves the clipboard. It can get slightly hairy with the quoting, since you are essentially wrapping PowerShell inside a /bin/sh inside a Python script.
Note that, even though you are in Linux Python, since it's the Windows PowerShell that we are calling, it takes the Windows path format (C:\Users\Username\Pictures...) rather than the Linux version (/mnt/c/Users/...).
While I didn't have any timing issues with this, you may need to insert small delays. Again, refer to the existing answer for that. This solution is primarily to explain how to do it through WSL's Python using PowerShell.
if you are on a laptop you could hit the windows button and prtsc to take a screenshot and if you are on PC you could use OBS its recording software that can do pretty much anything.
I've created a simple script that executes a "moving mouse and keyboard" sequence. Although currently to get this to work I use a Shell (Idle) to run and that is to slow with boot up time and such.
Is there a way to have this python file on desktop och with a hotkey swiftly run the code? I tried doing it through terminal but it doesn't like my module.
Some info:
This is for both mac and windows.
The module I imported is pyautogui from PyPi and it works in the shell.
Thank you in advance!
Some things to consider when trying to setup a hotkey to successfully execute any kind of command:
Each Operating System has its own ways to setup hotkeys and sometimes this may differ between distributions as well as between desktop managers.
Many of the relevant how-to-descriptions are easily found via regular search machines as Google.
If you would in fact like your script to set-up its own hotkeys you would have to re-write it in such a manner that it can detect the current operating system/distribution/desktop manager by itself and execute the commands relevant to that particular set-up.
I'll explain my question: is it possible to write a Python script which interacts with OS X architecture in a high-level way?
For example, can I gain control on Mac OS X windows resizing from a Python script? Are there modules for that? I'm not finding any.
To push things even further, would I be able to control keyboard shortcuts too? I mean, with Python, could I write a script that opens a terminal window everytime I type cmd + Enter from wherever I am in that moment, as if it was a system shortcut (Awesome WM style, if you know what I'm talking about)?
Hope I've been clear.
The 2nd one you can't do for sure, since the events are grabbed by other processes.
You should look for a osx specific library for doing that and then write a python wrapper around it.
I wrote a program that uses the console. Most of the time, the user must see the console informations. For a specific function from command line, I would like to run the script without the console rises. I just don't want see the window but it can be in the task bar. I know I can use extra modules (gui, win32,..) to do that but I would like to use the standard python librairy.
Is it possible to do that?
The program should run on Windows. (python 2.7)
I specify... I know I can use pythonw.exe too. The question then is how to launch the same script with python.exe sometimes and with pythonw.exe (from command line) for a specific function?
Found this question via google, so to answer the question, how to minimize (not completely hide) the console window on Windows when running a Python script:
# Python 3
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 6 )
GetConsoleWindow() will return the window handle for the current console.
ShowWindow(hWnd, nCmdShow) will set the properties for the specific window. 6 is SW_MINIMIZE. Click on the link for other parameters.
on windows there are two python executables in your installation, one is "python.exe", which is the one you typically use. There is another called "pythonw.exe" which is for gui programs. It works just like python.exe, but does not display a console at all.
My first idea would be to have a .pyw-script for that specific task, that is launched in this case. Then only the original script's console pops up for a short time.
Essentially, what I want to do is have an icon that has different symbols for various programs at the bottom of it (for example, a python file might have a symbol for command prompt, a text editor, and a debugger, all little squares at the bottom of the icon), and when the user double clicks on one of these, that program is used to open it up.
How can I put these symbols there (do I have to make a special icon, or is there an easier way to do it) and how can I see if they double click on these? Also, how can I stop the normal program from running when they double click, so only the program I want will work?
Some background: I'm using Python 2.5 and I'm on a Windows Vista, but if possible I'd like for this to be able to run on XP or 7 as well.
You use some GUI system, like GTK+ or wxPython or something.
Sounds like you want to write an extension to windows explorer. This would be a significant project and I'm not sure Python would be the best language (you'd be accessing the win32 api a lot).