Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved the bundleIDsToBlock for configuration support. #2

Merged
merged 1 commit into from
Oct 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions bigsurblocker/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,82 +16,86 @@ @implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
self.alertTriggered = NO;

NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSNotificationCenter *nc = [workspace notificationCenter];

NSOperationQueue *notificationQueue = [NSOperationQueue new];

// Subscribe for notifications when apps are launched
[nc addObserverForName:NSWorkspaceDidLaunchApplicationNotification
object:nil
queue:notificationQueue
usingBlock:^(NSNotification * _Nonnull note) {

// Get information about the launched app
NSDictionary *userInfo = [note userInfo];
NSRunningApplication *runningApp = [userInfo objectForKey:NSWorkspaceApplicationKey];
NSString *bundleID = runningApp.bundleIdentifier;

NSArray *bundleIDsToBlock = @[

// Load our user defaults suite. This will allow the alert text
// to be specified with a configuration profile
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.hjuutilainen.bigsurblocker"];

NSArray *bundleIDsToBlock = [userDefaults arrayForKey:@"bundleIDsToBlock"];

if (!bundleIDsToBlock) {
bundleIDsToBlock = @[
@"com.apple.InstallAssistant.BigSur",
@"com.apple.InstallAssistant.Seed.macOS1016Seed1",
];

}
if ([bundleIDsToBlock containsObject:bundleID]) {
NSLog(@"Detected macOS installer app launch");

// Get the localized app name
NSString *appName = runningApp.localizedName;

// Terminate the app
NSLog(@"Terminating \"%@\", \"%@\"", appName, bundleID);

// We could be polite but...
[runningApp forceTerminate];

// Check if we are already displaying an alert
if (self.alertTriggered) {
NSLog(@"Skipping alert. Previous alert still running");
} else {
self.alertTriggered = YES;

// GUI must be run from main thread
dispatch_async(dispatch_get_main_queue(), ^{

// Load our user defaults suite. This will allow the alert text
// to be specified with a configuration profile
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.hjuutilainen.bigsurblocker"];



NSString *messageText = [userDefaults stringForKey:@"AlertTitle"];
if (!messageText) {
messageText = NSLocalizedString(@"The application \"%@\" has been blocked", @"");
}

NSString *informativeText = [userDefaults stringForKey:@"AlertText"];
if (!informativeText) {
informativeText = NSLocalizedString(@"Contact your administrator for more information", @"");
}

// Configure the alert
NSAlert *alert = [[NSAlert alloc] init];

[alert setMessageText:[NSString stringWithFormat:messageText, appName]];
[alert setInformativeText:informativeText];

[alert addButtonWithTitle:NSLocalizedString(@"OK", @"")];
[alert setAlertStyle:NSAlertStyleWarning];
[alert setIcon:[NSImage imageNamed:NSImageNameCaution]];

// Show the alert above all other apps and windows
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
[[alert window] setLevel:NSStatusWindowLevel];

// Show the alert
//
// Note that the [alert runModal] will not return until the user dismisses the popup window
[alert runModal];

// User dismissed the alert, change status to allow new ones to be displayed
self.alertTriggered = NO;
});
Expand Down