I have created an application using pyobjc to monitor the current application I'm using or current URL if Safari/Chrome are being used. I get the frontmost application using:
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
which runs in a loop that retrieves info every second. Nevertheless, this solution seems inefficient, given the fact that I can lose info for over 30 seconds.
I am wondering if there is a solution using events, i.e., have an observer that listens when there is a change in the current frontmost application.
I have found this answer Mac OS X - How to monitor a window change event?, but there are two issues: 1. I only need info about the application and not about the specific window, and 2. I have no idea how to translate these messages into Python code.
An AppleScript solution is suitable, given that I could call it from Python using osascript.
Thanks!
Related
I have two scripts:
one continuosly comunicates with the gps module and refresh the gps time variable.
the second one ask me when I want to view the gps time and prints it on the console.
How can I retrieve the gps_time variable of the first script (while it running in background) from the second script?
You can do some socket programming.
Maybe you can get some idea from here:
Interprocess communication in Python
Official docs:
https://docs.python.org/3.10/library/socket.html
I am creating a test automation which uses an application without any interfaces. However, The application calls a batch script when it changes modes, and I am therefore am able to catch the mode transitions.
What I want to do is to get the batch script to give an input to my python script (I have a state machine running in python) during runtime. Such that I can monitor the state of the application with python instead of the batch file.
I am using a similar state machine to the one of Karn Saheb:
https://dev.to/karn/building-a-simple-state-machine-in-python
However, instead of changing states statically like:
device.on_event('event')
I want the python script to do something similar to:
while(True):
device.on_event(input()) # where the input is passed from the batch script:
REM state.bat
set CurrentState=%1
"magic code to pass CurrentState to python input()" %CurrentState%
I see that a solution would be to start the python script from the batch file every time it is called with the "event" and then save the current event in another file upon termination of the python script... But I want to avoid such handling and rather evaluate this during runtime.
Thank you in advance!
A reasonably portable way of doing this without ugly polling on temporary files is to use a socket: have the main process listen and have the batch file(s) start a small program that connects to the server and writes a message.
There are security considerations here: you can start by listening only to the loopback interface, with further authentication if the local machine should not be trusted.
If you have more than one of these processes, or if you need to handle the child dying before it issues its next report, you’ll have to use threads or something like select to unify the news from different input channels (e.g., waiting on the child to exit vs. waiting on news from the next batch file).
Background info:
I have built a program main.py, that is built to perform work at a stateless level (meaning if something fails, it will get requeued/database etc separately)...
Problem:
I am trying to figure out how to monitor this main.py program from my linux instance.
Main.py runs and threads out multiple workers, however I want to monitor and ensure the system is still healthy to 'perform work'...
How can you do this from a single script level and determine that it is no longer working?
My Ideas:
Create a status notification to syslog stating it is healthy... and trigger on no longer receives? (if that is possible)
Linux maybe provides a way to monitor that system is healthy?
last one is creating a TCP/Socket level query mechanism to check for status...
I hope i'm describing the problem right... I'm trying to ensure program is not only running ,but processing and can process[?]
So first off, overall what I'm trying to accomplish is for a base machine (as in a VPS) to run automated task through Firefox using Python.
Now the object or goal is to have Firefox run the given tasks in the browser itself, though then connect to a VPS (through the browser) using a VNC connection, and control or issue tasks as well to that VPS (this is the part I'm having trouble with); with also as little memory required as possible for maximum efficiency.
To give an example, if you've used Digital Ocean, you can view your VPS's specific screen or terminal within the current browser.
To be clear, the VPS OS I'm using to run the base process is Linux, though the VPS that the program is connecting to (through the browser) is using a Windows OS. Something such as this let's say (note I didn't screenshot this):
My problem lies with that after running through all of the scripted tasks using Selenium in Python (with Firefox), once I open up the VPS in the browser, I can't figure out how to access it properly or issue jobs to be completed.
I've thought about maybe using (x,y) coordinates for mouse clicks, though I can't say this would exactly work (I tested it with iMacros, though not yet Selenium).
So in a nutshell, I'm running base tasks in Firefox to start, and then connecting to a VPS, and finally issuing more tasks to be completed from Firefox to that VPS that's using a Windows OS environment.
Suggestions on how to make this process simpler, more efficient, or further its reliability?
There is a class in java called Robot class which can handle almost all keyboard operation
There is a similer thing present in python gtk.gdk.Display.
Refer below:-
Is there a Python equivalent to Java's AWT Robot class?
Take a screenshot via a python script. [Linux]
OR
Python ctypes keybd_event simulate ctrl+alt+delete
Demo java code:-
try{
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_DELETE);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_DELETE);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
Hope it will help you :)
I'd like to create a daemon (base on script or some lower level language) that calculates statistics on all opened applications according to their initiating process. The problem is that the initiating process does not always equivalent to the actual parent process.
For instance, When I press an hyperlink from Microsoft Word that should open executable file like file:///Applications/Chess.app/
In the case above, I've observed that the ppid of 'Chess' is in fact 'launchd', just the same as if I was running it from launchpad.
Perhaps there's a mach_port (or any other) api to figure out who really initiated the application ?
You can't. Mac OS X does not keep track of this information in the way you're looking for -- opening an application from another application does not establish a relationship of any sort between those applications.