-
Notifications
You must be signed in to change notification settings - Fork 13
/
AppBlocker.py
executable file
·101 lines (77 loc) · 3.11 KB
/
AppBlocker.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import Foundation
import signal
import re
import os
import sys
import shutil
from AppKit import *
from PyObjCTools import AppHelper
# List of all blocked bundle identifiers. Can use regexes.
blockedBundleIdentifiers = ['com.apple.InstallAssistant.Sierra']
# Whether the blocked application should be deleted if launched
deleteBlockedApplication = False
# Whether the user should be alerted that the launched applicaion was blocked
alertUser = True
# Message displayed to the user when application is blocked
alertMessage = "The application \"{appname}\" has been blocked by IT"
alertInformativeText = "Contact your administrator for more information"
# Use a custom Icon for the alert. If none is defined here, the Python rocketship will be shown.
alertIconPath = "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/Actions.icns"
# Define callback for notification
class AppLaunch(NSObject):
def appLaunched_(self, notification):
# Store the userInfo dict from the notification
userInfo = notification.userInfo
# Get the laucnhed applications bundle identifier
bundleIdentifier = userInfo()['NSApplicationBundleIdentifier']
# Check if launched app's bundle identifier matches any 'blockedBundleIdentifiers'
if re.match(blockedBundleIdentifiersCombined, bundleIdentifier):
# Get path of launched app
path = userInfo()['NSApplicationPath']
# Get PID of launchd app
pid = userInfo()['NSApplicationProcessIdentifier']
# Quit launched app
os.kill(pid, signal.SIGKILL)
# Alert user
if alertUser:
alert(alertMessage.format(appname=userInfo()['NSApplicationName']), alertInformativeText, ["OK"])
if deleteBlockedApplication:
try:
shutil.rmtree(path)
except OSError, e:
print ("Error: %s - %s." % (e.filename,e.strerror))
# Define alert class
class Alert(object):
def __init__(self, messageText):
super(Alert, self).__init__()
self.messageText = messageText
self.informativeText = ""
self.buttons = []
def displayAlert(self):
alert = NSAlert.alloc().init()
alert.setMessageText_(self.messageText)
alert.setInformativeText_(self.informativeText)
alert.setAlertStyle_(NSInformationalAlertStyle)
for button in self.buttons:
alert.addButtonWithTitle_(button)
if os.path.exists(alertIconPath):
icon = NSImage.alloc().initWithContentsOfFile_(alertIconPath)
alert.setIcon_(icon)
# Don't show the Python rocketship in the dock
NSApp.setActivationPolicy_(1)
NSApp.activateIgnoringOtherApps_(True)
alert.runModal()
# Define an alert
def alert(message="Default Message", info_text="", buttons=["OK"]):
ap = Alert(message)
ap.informativeText = info_text
ap.buttons = buttons
ap.displayAlert()
# Combine all bundle identifiers and regexes to one
blockedBundleIdentifiersCombined = "(" + ")|(".join(blockedBundleIdentifiers) + ")"
# Register for 'NSWorkspaceDidLaunchApplicationNotification' notifications
nc = Foundation.NSWorkspace.sharedWorkspace().notificationCenter()
AppLaunch = AppLaunch.new()
nc.addObserver_selector_name_object_(AppLaunch, 'appLaunched:', 'NSWorkspaceWillLaunchApplicationNotification',None)
# Launch "app"
AppHelper.runConsoleEventLoop()