How to get Ubuntu Terminal's window handles using Python? - python

For running few tests on two different serial ports i.e. ttyS0 and ttyS1, I need to work on two different terminal windows or tabs, as the commands I have to run should type simultaneously through Python script.
My prior experience was automating some tasks on Windows OS and with the help of win32gui library for Python it was easy to get window handles.I took help from here for this HWND of each window in Python
But, I couldn't find any library that can help me around here in Ubuntu.

This gives you the name of the current tty in Python:
import os,sys
os.ttyname(sys.stdout.fileno())
If you need it in the Unix terminal then a simple tty is your friend. Then you can do e.g. echo hello > /dev/pts/# to write to your neighboring tab or window.

Related

Automate sending CMD command in specific window using python

I'm running a couple of things on my home server, two of which are Minecraft servers. I want to set up an automatic restart of the computer every week, but first want to close the servers in the correct way. This is done by sending the command "stop" in their specific CMD windows.
I've successfully been able to select and open specific CMD windows as shown below:
import pygetwindow as gw
handle = gw.getWindowsWithTitle('C:\Windows\system32\cmd.exe')[0]
handle.activate()
My problem is that I can't seem to be able to send the "stop" command in those specific windows. If I for example run the following commands instead:
import pygetwindow as gw
import os
print(gw.getAllTitles())
handle = gw.getWindowsWithTitle('C:\Windows\system32\cmd.exe')[0]
handle.activate()
os.system("stop")
It doesn't do anything because it opens a new CMD window. There might be other issues as well, but since I haven't been able to solve that issue I haven't encountered them yet.
I would really appreciate some help, cause I'm stuck.
I would use a plugin like AutoRestart https://www.spigotmc.org/resources/autorestart.2538/ for Spigot. If you're on vanilla, I would reccomend upgrading to Spigot (or i like to use Pufferfish/Paper)

How to open a window from the command line within a Python script?

I am working on a python script to analyze astronomy images and I am trying to open a DS9 window within a python script (DS9 is a utility that allows images to be interactively viewed and analyzed). Usually I would open DS9 by going into the Linux terminal and typing:
>ds9 &
and then it would pop up in another window.
I tried to mimic this in my python script by writing the following line:
os.system('ds9 &')
When I would run the script the DS9 window would pop up but the rest of the script would not run until I closed the DS9 window. This gave me errors because the tasks that followed needed a DS9 window to be opened.
I am wondering if there is a way to open a window from within a python scripts and still have the rest of the script continue running.
Perhaps:
os.system('ds9 &')
isn't the right approach?
You can use subprocess module.
subprocess is a newer way to spawn processes rather than using os.spawn*() or os.system().
In your case:
import subprocess
subprocess.Popen(["ds9"])
This should run ds9 in background.
See the documentation here.

How to name / find a specific running python script process on windows?

On windows:
I have two scripts:
Client.py
Console.py
Q1:
Client can be run multiple times but only single instance should be left running. It is being run with pythonW.exe
Q2:
Multiple console.py can be run at the same time. But the last one that closes should kill the client.py
Limitations:
Strongly preferred not to write any files. (i.e. use search by window name, PID etc...)
Strongly preferred not to install any additional modules for python. i.e. use ctypes etc...
For Q1 i tried to do ctypes.windll.user32.SetWindowTextA ("NAME") and then search for it. it works for python.exe but not for pythonW.exe beacause there is no console window then.
Thanks!

Python on windows, shutting down and taking a screenshot

I have the following two quesions:
1 I want to be able to shutdown windows xp from python code. I am able to do that by running the following code in the python console:
import os
os.system("shutdown -s -f")
But, if i put the same code in a .py file and try to execute it,it does not work. I get the help prompt for the shutdown command.Any way to fix this ?
2 Is there any way i can take a screenshot of the current screen using python on windows ?
Thank You
There's some code to shutdown windows in this message from the python-win32 list.
You can take a screen shot using PIL's ImageGrab module.

How to switch to a python subprocess created by IPython (on OS X)?

When I use IPython along with the -wthread option, it spawns a python subprocess, which appears as a Mac OS X application.
My problem is that when I send commands to that application (for example plotting with matplotlib), the window is updated behind all my other windows. I would like to be able to call a python command to switch this python window to the front (I do that manually with ⌘-tab, but I have to find the python application first, and there might be several ones).
Is there a python script to detect which application IPython has spawned, and how to then automatically switch to it in OS X?
(I'm stating the problem in OS X, but the issue should be similar on other systems).
Edit: let me break this down in two problems:
how to know which Mac OS X application python is running in? (probably possible with some IPython witchery)
how to tell Mac OS X to put the focus on that application? (maybe using applescript)
Could be either:
Making a new python script that tracks grandchild processes of another script might be tricky. The IPython documentation has an example to monitor spawned processes by pid; JobControl. JobControl only kills the processes but I imagine adding a command to change window focus would be fairly easy.
From what I've read, the Tk gui does not properly set window focus on macs. If your 'matplotlib' or otherwise uses the Tk gui, this may be the problem. -source-
I am not very familiar with OS X, so either run with those, clarify your situation or let me know if I'm too far off.
Here is my full solution, with an IPython magic function.
Install appscript (see this question about switching apps programmatically in OS X), and put the following code in a script called activate.py in your ~/.ipython folder.
import appscript
import sys
appscript.app(pid=int(sys.argv[1])).activate()
Now, edit your ~/.ipython/ipy_user_conf.py configuration file and define the magic function:
def wxactivate(self, arg):
import wx
pid = wx.GetProcessId()
ip = self.api
import os
here = os.path.dirname(__file__)
import subprocess
subprocess.Popen([os.path.join(here, 'activate.py'), str(pid)])
Now you just have to register this magic IPython function by putting the following somewhere in that same configuration file:
ip.expose_magic('wxactivate', wxactivate)
Now, after you run IPython -wthread, you can call %wxactivate and you will switch to the corresponding Python application!
(note that the reason why one has to run the call to appscript's activate() in another process in not clear to me; it may have to do with some threading problem... any explanation would be appreciatated)

Categories