forked from erikberglund/AppBlocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNEWOBSERVER.py
52 lines (40 loc) · 1.78 KB
/
NEWOBSERVER.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
import objc
from Foundation import NSObject, NSKeyValueObservingOptionNew, NSKeyValueChangeNewKey
"""
Getting help from:
https://nshipster.com/key-value-observing/
https://svn.red-bean.com/pyobjc/branches/pyobjc-1.4-branch/Examples/00ReadMe.html
https://stackoverflow.com/questions/8317338/how-to-use-kvo-to-detect-when-an-application-gets-active
https://stackoverflow.com/questions/52710089/macos-detect-all-application-launches-including-background-apps
"""
import Foundation
import AppKit
from PyObjCTools import AppHelper
import signal
import os
def takeaction(runningapp):
if runningapp.bundleIdentifier() == 'com.twitch.desktop':
os.kill(runningapp.processIdentifier(), signal.SIGKILL)
print("TWITCH IS NOT ALLOWED!")
elif runningapp.bundleIdentifier() == 'com.apple.DirectoryUtility':
print "adrna"
os.kill(runningapp.processIdentifier(), signal.SIGKILL)
elif runningapp.bundleIdentifier() == 'com.jamfsoftware.selfservice.mac':
print 1
os.kill(runningapp.processIdentifier(), signal.SIGKILL)
class Observer(NSObject):
def __init__(self):
#self.currentbid = 0
pass
def observeValueForKeyPath_ofObject_change_context_(self, path, object, changeDescription, context):
if NSKeyValueChangeNewKey == 'new':
takeaction(object.runningApplications()[-1])
#print changeDescription[NSKeyValueChangeNewKey]
def willChangeValueForKey_(self, key):
print "Changed" +key
observer = Observer.new()
workspace = Foundation.NSWorkspace.sharedWorkspace() # For runnning apps
#3nc = workspace.notificationCenter()
workspace.addObserver_forKeyPath_options_context_(observer, 'runningApplications', NSKeyValueObservingOptionNew, 0)
AppHelper.runConsoleEventLoop().run()