Skip to content

Commit

Permalink
Add -remove groupID option which removes a notfiication. Closes jul…
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Jul 30, 2012
1 parent 7f04dc6 commit 9039a4a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
13 changes: 12 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ application mbundle.

#### Options

The `-message` option is the only one that is required.
At a minimum, you have to specify either the `-message` option or the `-remove`
option.

-------------------------------------------------------------------------------

Expand All @@ -53,6 +54,9 @@ The title of the notification. This defaults to ‘Terminal’.
Specifies the ‘group’ a notification belongs to. For any ‘group’ only _one_
notification will ever be shown, replacing previously posted notifications.

A notification can be explicitely removed with the `-remove` option, describe
below.

Examples are:

* The sender’s name to scope the notifications by tool.
Expand All @@ -61,6 +65,13 @@ Examples are:

-------------------------------------------------------------------------------

`-remove ID` **[required]**

Removes a notification that was previously sent with the specified ‘group’ ID,
if one exists.

-------------------------------------------------------------------------------

`-activate ID`

Specifies which application should be activated when the user clicks the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "-group 12345"
argument = "-group abcde"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-remove abcde"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
Expand All @@ -68,19 +72,19 @@
</CommandLineArgument>
<CommandLineArgument
argument = "-execute &apos;say &quot;OMG&quot;&apos;"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-execute &apos;ls -l&apos;"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-title Kicker"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-message &apos;Execute: rake spec&apos;"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "-activate com.apple.Safari"
Expand Down
62 changes: 36 additions & 26 deletions Terminal Notifier/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ - (void)printHelpBanner;
const char *appVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] UTF8String];
printf("%s (%s) is a command-line tool to send OS X User Notifications.\n" \
"\n" \
"Usage: %s -message VALUE [options]\n" \
"Usage: %s -[message|remove] [VALUE|ID] [options]\n" \
"\n" \
" Required:\n" \
" Either of these is required:\n" \
"\n" \
" -message VALUE The notification message.\n" \
" -remove ID Removes a notification with the specified ‘group’ ID.\n" \
"\n" \
" Optional:\n" \
"\n" \
Expand All @@ -50,52 +51,61 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *message = defaults[@"message"];
if (message == nil) {
NSString *remove = defaults[@"remove"];
if (message == nil && remove == nil) {
[self printHelpBanner];
exit(1);
}

NSMutableDictionary *options = [NSMutableDictionary dictionary];
if (defaults[@"activate"]) options[@"bundleID"] = defaults[@"activate"];
if (defaults[@"group"]) options[@"groupID"] = defaults[@"group"];
if (defaults[@"execute"]) options[@"command"] = defaults[@"execute"];
if (defaults[@"open"]) options[@"open"] = defaults[@"open"];
if (remove) {
[self removeNotificationWithGroupID:remove];
if (message == nil) exit(0);
}

if (message) {
NSMutableDictionary *options = [NSMutableDictionary dictionary];
if (defaults[@"activate"]) options[@"bundleID"] = defaults[@"activate"];
if (defaults[@"group"]) options[@"groupID"] = defaults[@"group"];
if (defaults[@"execute"]) options[@"command"] = defaults[@"execute"];
if (defaults[@"open"]) options[@"open"] = defaults[@"open"];

[self deliverNotificationWithTitle:defaults[@"title"] ?: @"Terminal"
message:message
options:options];
[self deliverNotificationWithTitle:defaults[@"title"] ?: @"Terminal"
message:message
options:options];
}
}
}

- (void)deliverNotificationWithTitle:(NSString *)title
message:(NSString *)message
options:(NSDictionary *)options;
{
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
NSUserNotification *userNotification = nil;

// First remove earlier notification with the same group ID.
if (options[@"groupID"]) {
for (userNotification in center.deliveredNotifications) {
if ([userNotification.userInfo[@"groupID"] isEqualToString:options[@"groupID"]]) {
NSString *deliveredAt = [userNotification.actualDeliveryDate description];
printf("* Removing previous notification, which was delivered on: %s\n", [deliveredAt UTF8String]);
[center removeDeliveredNotification:userNotification];
break;
}
}
}
if (options[@"groupID"]) [self removeNotificationWithGroupID:options[@"groupID"]];

// Now create and deliver the new notification
userNotification = [NSUserNotification new];
NSUserNotification *userNotification = [NSUserNotification new];
userNotification.title = title;
userNotification.informativeText = message;
userNotification.userInfo = options;

NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
center.delegate = self;
[center scheduleNotification:userNotification];
}

- (void)removeNotificationWithGroupID:(NSString *)groupID;
{
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
for (NSUserNotification *userNotification in center.deliveredNotifications) {
if ([userNotification.userInfo[@"groupID"] isEqualToString:groupID]) {
NSString *deliveredAt = [userNotification.actualDeliveryDate description];
printf("* Removing previously sent notification, which was sent on: %s\n", [deliveredAt UTF8String]);
[center removeDeliveredNotification:userNotification];
break;
}
}
}

- (void)userActivatedNotification:(NSUserNotification *)userNotification;
{
[[NSUserNotificationCenter defaultUserNotificationCenter] removeDeliveredNotification:userNotification];
Expand Down

0 comments on commit 9039a4a

Please sign in to comment.