Skip to content
Open
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
38 changes: 38 additions & 0 deletions Terminal Notifier/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ - (id)objectForKeyedSubscript:(id)key;
}
@end

@interface AppDelegate()
@property BOOL isDaemon;
@end

@implementation AppDelegate

Expand Down Expand Up @@ -113,6 +116,29 @@ - (void)printVersion;

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

if ([[[NSProcessInfo processInfo] arguments] indexOfObject:@"-daemon"] != NSNotFound) {
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = self;
self.isDaemon = YES;
return;
}

NSString *bundleIdentifier = NSBundle.mainBundle.bundleIdentifier;
//NSLog(@"gaga: id:%@", bundleIdentifier);
NSArray *apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleIdentifier];
if (apps.count < 2) {
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSWorkspaceOpenConfiguration *config = [NSWorkspaceOpenConfiguration configuration];
config.createsNewApplicationInstance = YES;
config.arguments = @[@"-daemon"];
NSURL *url = NSBundle.mainBundle.bundleURL;
[workspace openApplicationAtURL:url configuration:config completionHandler:^(NSRunningApplication *app, NSError *error){
//NSLog(@"gaga: app:%@, err:%@", app, error);
}];
}

NSUserNotification *userNotification = notification.userInfo[NSApplicationLaunchUserNotificationKey];
if (userNotification) {
[self userActivatedNotification:userNotification];
Expand Down Expand Up @@ -326,6 +352,10 @@ - (void)userActivatedNotification:(NSUserNotification *)userNotification;
if (bundleID) success &= [self activateAppWithBundleID:bundleID];
if (command) success &= [self executeShellCommand:command];
if (open) success &= [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:open]];

if (self.isDaemon) {
return;
}

exit(success ? 0 : 1);
}
Expand Down Expand Up @@ -376,7 +406,15 @@ - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
- (void)userNotificationCenter:(NSUserNotificationCenter *)center
didDeliverNotification:(NSUserNotification *)userNotification;
{
if (self.isDaemon) {
return;
}
exit(0);
}

- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
if (self.isDaemon) {
[self userActivatedNotification:notification];
}
}
@end