Is there a way to find when the activeApplication changes in OSX through Python and AppKit? I know how to find out launchedApplication and activeApplication ( please refer to my other question here: Finding the Current Active Window in Mac OS X using Python )
I've got an OS X app that does this by polling with an NSTimer. I tried searching for distributed notifications to see if I could find a better way to do it, but I couldn't see anything terribly useful.
I did get notifications when application were launched or quit. which is at least a little helpful. You can see the registration of these where my controller wakes up.
This application has been immensely helpful to me and even polling once a second uses nearly no CPU. If I could make it more event driven, I would, though. :)
I'm not aware of an 'official'/good way to do this, but one hackish way to go about this is to listen for any distributed notifications and see which ones are always fired when the frontmost app changes, so you can listen for that one:
You can set something like this up:
def awakeFromNib(self):
NSDistributedNotificationCenter.defaultCenter().addObserver_selector_name_object_(
self, 'someNotification:', None, None)
def someNotification_(self, notification):
NSLog(notification.name())
After you've found a notification that always fires when apps are switched, you can replace the first 'None' in the addObserver_etc_ call with the name of that notification and check for the frontmost app in your 'someNotification_' method.
In my case I noticed that the 'AppleSelectedInputSourcesChangedNotification' fired everytime I switched apps, so I would listen to that..
Keep in mind that this can break any moment and you'll prolly be checking for a change in the frontmost app more often than needed.
There must be a better way though.. hopefully :)
Related
Ive been looking the net dry to find a solution to this and i hope you can help me.
The main goal is that i have my client which interacts with Bybit API servers, their servers has a strict time window offset that i need to be in the bounds of and to do it so, i choose the method of resyncing my time since that worked, but more options might be available for this so feel free to let me know if you got other suggestions.
What i am looking for, is a way for me to tell the python script to resync my time.
It Could be something like w32tm /resync, however all that ive found out after alot of testing is that any deployed script even adminstrator shell commands can not execute w32tm commands unless a typed password is used, and even trying with a typing emulator to automaticly fake the typing, that didnt work.
So is there another way for me to force a /resync of time?
Im looking forward to hearing your answers and hopefully you can stear me in the right direction.
Best regards.
Mathias.
Easier install NPT software o the machines and it runs a service.
These are accurate.
As suggested you let one machine in the network sync time from Meinberg and the rest of the network gets their time form the NTP Server.
https://www.meinbergglobal.com/english/sw/ntp.htm
Please note: This is not the similar-sounding FAQ that involves a multi-headed display. This question is about two different computers.
If I log onto the console of two different computers, sitting-at.example.com and sshed-into.example.com, and then sitting in front of sitting-at.example.com I ssh (with X11 tunneling) into sshed-into.example.com...
If I do that and then run a little Python script that uses libnotify, the notification pops up on the console of sshed-into.example.com, not sitting-at.example.com. But I need the notification on sitting-at.example.com. It seems to me that would make more sense.
The result is I do not see the notification until I drive to the other location and log back into the console of sshed-into.example.com.
My code has a fallback to use a little GTK popup if libnotify raises an exception, but it doesn't kick in because as far as libnotify is concerned, things are working fine.
I could use the GTK popup all the time, like I used to, but I kinda like libnotify where feasible. For one thing, libnotify doesn't get lost if I click to a different virtual desktop or raise a window at an inopportune moment. The GTK popup does.
Is there a way of getting either a remote notification using libnotify, xor of getting an exception?
I've considered parsing $DISPLAY to see if it "looks local", but it seems like there should be a better way.
Thanks in advance!
PS: The code for the little script is opensource and can be found at http://stromberg.dnsalias.org/~strombrg/notify-when-up2.html
I wound up checking $DISPLAY, since the responses here weren't flowing thick and fast.
The function I used:
def is_local_display(regex=re.compile(r'^:0(\.[0-9]+)?$')):
"""Return True iff $DISPLAY points at a local display."""
if 'DISPLAY' not in os.environ:
return False
match = regex.match(os.environ['DISPLAY'])
return bool(match)
I currently have a python script that does exactly what I need it to do, however every now and then the script will hang and the only way to restart it is by killing the script and relaunching it.
I was wondering if there was a way to put in a few commands that will restart it lets say everytime it hangs or when a specific message appears or even just restart it on a timer eg:every 50 seconds.
I cannot provide the code through here, but I can provide it if we talk in private.
I am willing to pay you a bit of money if your fix does work.
please email me at stackoverflow1#shaw.ca
Thanks!
Edit: I see, ok - then is it possible to provide me with some codes which it will restart on a specific timer?
Edit2: Ok thanks everyone for their comments - I will get in touch with the person who built it to see if they can rewrite it from scratch to include a timer.
Cheers.
Feel free to pay me if you want, although it is by no means necessary.
Here:
import time
import threading
import os
def restart():
time.sleep(50)
os.execv('/full/path/to/this/script', ['second argument', 'third argument'])
def main():
t = threading.Thread(target=restart, args=(), name='reset')
t.start()
# ... The rest of your code.
If you have any buffers open that you care about (such as stdout) you'll want to flush them right before the call to execv up there.
I haven't tested this code, because I don't have a python interpreter handy at the moment, but I'd be surprised if it didn't work. That call to execv replaces the current context, so you don't get an increasingly deep hierarchy of child processes. All I'm doing, in case you're curious and want to know what magic phrase to google, is setting a "timer interrupt handler". For the pedants, no, I recognize this thing isn't directly handling any interrupts.
The numeric argument to sleep is in seconds. I would simply request that you not use my code in malware, unless it is for research purposes. I'm particular that way.
edit: Additionally, a lot of it was taken from here.
I spent the last hours trying to get to know wxPython, because I want to write a GUI program. I found some tutorials on that (not too many), but all of them just explain how to add yet another kind of widget, down to fancy things like LED number outputs and mouse gestures (this one e.g. takes it quite far: Another Tutorial). But everything I could find so far does nothing more than create a static GUI, waiting for the user to do something, then execute some handlers and wait again. It took me a while to even find out that wx.App takes a part in all of that, and that you can subclass it.
I want to write a program, that does things without input! The GUI is supposed to be a client that logs in on a server, and when the server sends something, I want the GUI to show what happened. I could not find a single tutorial even mentioning, that such programs exist. How can I write such a thing? How do they integrate with wxpython?
Do I need to span another thread? Is there a way to hook into the MainLoop and have some code executed periodically, that checks for change and then updates some of those fancy GUI things? And is there any page that teaches you, how to do this?
First of all, you should figure out how to do what you want WITHOUT a GUI. In this case, you'll need to figure out how to login to a server. You'll probably need to use something like paramiko for that. See http://www.lag.net/paramiko/
Once you've got that figured out, then you can add it to your GUI. Probably in a button handler so when the user presses a button, it pops up a dialog asking for a user name and password to pass to paramiko to login to the server.
If the server query takes a long time to execute (like say you're querying a database for a huge set of data), then you'll want to run the query in a separate thread. Why? Because that query will block the GUI's main loop and make your app freeze until it finishes. See the following articles for information on wxPython and threads:
http://wiki.wxpython.org/LongRunningTasks
http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
I wrote up a tutorial on making wxPython talk to a socket server, so you might find that useful: http://www.blog.pythonlibrary.org/2013/06/27/wxpython-how-to-communicate-with-your-gui-via-sockets/
I also have an article on how to make an image viewer, and do CRUD ops to a database on there.
I am creating a script in Python to integrate Pidgin with Unity (Ubuntu 12.04), I've managed to do the counting notifications system using the Unity API, but i dont know what event or signal that is activated when the conversation window gains focus(To clear the message counter)...
I've tried some of the signals available on the documentation of Pidgin (https://developer.pidgin.im/wiki/DbusHowto) but none of them worked, is there any GTK(or anything) event that is triggered when the window chat gets focus?
Conversation UI signals are listed here those might be more helpful for what you are trying to do.
Though I don't know that I understand what you meant by your comment about the counter being wiped instantly when you tried some of the other signals. That sounds like it might be a coding error to me.