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.
Related
I am creating a TUI application using Python and curses. And I want to embed/integrate the shell, like you have in VS Code, etc. How can I do this?
I don't just want to capture input and execute it using bash, I want the real shell. E.g. in VS Code you see the real shell prompt (as defined in bashrc) and also you can run a whole application (e.g. vim) from inside the VS code terminal.
I have looked at pty module: not sure if it can help here. I want to basically have a library function like invoke_shell() to which I just pass a reference to a curses window (inside of which I want to show the shell). I do not need to interact with the shell in any other way, except to: (1) close it, (2) capture a keystroke for my application to move focus away from the shell window to another curses window in my application (and move focus back to it when I need it). Is there any such library?
Any help is greatly appreciated! Thanks in advance.
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.
Say I have a folder called "Family Photos" and I want to automatically run a python program if that folder is selected. How would I go about doing that? Would I just put the code in the folder and it runs automatically?
Edit: I'm on Windows 10
You can use tkinter in python.
Tkinter is Python's de-facto standard GUI (Graphical User Interface) package. It is a thin object-oriented layer on top of Tcl/Tk.Tkinter is not the only GuiProgramming toolkit for Python. It is however the most commonly used one
You can use other GuiProgramming toolkits as well. Follow this_link to know about other gui frameworks.
For how part in Tkinter .Follow this_link
Some unix/linux systems are open source so you could modify the OS behavior to do that, i don't believe windows offer this feature, you probably should create an APP for that
The best solution would be a python scripts that run it self each hour and check if this folder is modified and do something if its, and it will run each time you turn on the computer once
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().
I apologize for such a basic question. I've done some research online and still cannot figure out for the life of me how to turn a python folder into something like an actual app I can open in OS X. I am using Mac OS X, Terminal and Coderunner for my Python project.
Here are a few options:
Platypus is not Python-specific. It lets you wrap a simple GUI around a command line tool.
py2app is Python-specific and a good choice if you have a GUI, or need to run in the background.
PyInstaller is similar to py2app but cross-platform; I've never used it, so I don't know how well it works.
The right choice depends on what your program does; who is the expected audience — do you need to redistribute it, if so how, and so forth. If you want to make the application entirely self-contained — not dependent on anything else beyond the OS — then things get more complicated (though certainly not insoluble; there are several commercial Mac desktop apps written in Python.)
Typically you would make the script executable by putting
#!/usr/bin/env python
as the first line, and then in a terminal window typing
chmod u+x myscript.py
You might also want to put it in a special scripts folder and then add that to your PATH by editing .bash_profile. (I am putting a lot of buzz-words here to help you find the tutorials explaining how these things work.)
You can wrap your script into an Automator object if you want to try running it that way, but in the long run you will be better off getting comfortable working in a terminal window.
It would also help to know what your app does: Process files, generate a GUI, do a calculation, etc...