Permalink
Browse files

Add `-remove groupID` option which removes a notfiication. Closes #6.

  • Loading branch information...
1 parent 7f04dc6 commit 9039a4ad17f896b9903ea5f7dcb7d57e6898a890 @alloy alloy committed Jul 30, 2012
View
13 README.markdown
@@ -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.
-------------------------------------------------------------------------------
@@ -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.
@@ -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
View
12 Terminal Notifier.xcodeproj/xcshareddata/xcschemes/Terminal Notifier.xcscheme
@@ -59,7 +59,11 @@
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
- argument = "-group 12345"
+ argument = "-group abcde"
+ isEnabled = "NO">
+ </CommandLineArgument>
+ <CommandLineArgument
+ argument = "-remove abcde"
isEnabled = "YES">
</CommandLineArgument>
<CommandLineArgument
@@ -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"
View
62 Terminal Notifier/AppDelegate.m
@@ -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" \
@@ -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];

0 comments on commit 9039a4a

Please sign in to comment.