#import "notify.h"
NSString* getBundleIdentifier(NSString* appName) {
NSString* findString = [NSString stringWithFormat:@"get id of application \"%@\"", appName];
NSAppleScript* findScript = [[NSAppleScript alloc] initWithSource:findString];
NSAppleEventDescriptor* resultDescriptor = [findScript executeAndReturnError:nil];
return [resultDescriptor stringValue];
}
BOOL setApplication(NSString* newbundleIdentifier) {
@autoreleasepool {
if (!installNSBundleHook()) {
return NO;
}
if (LSCopyApplicationURLsForBundleIdentifier((CFStringRef)newbundleIdentifier, NULL) != NULL) {
[fakeBundleIdentifier release]; fakeBundleIdentifier = newbundleIdentifier;
[newbundleIdentifier retain];
return YES;
}
return NO;
}
}
NSDictionary* sendNotification(NSString* title, NSString* subtitle, NSString* message, NSDictionary* options) {
@autoreleasepool {
NSUserNotificationCenter* notificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
NotificationCenterDelegate* ncDelegate = [[NotificationCenterDelegate alloc] init];
notificationCenter.delegate = ncDelegate;
ncDelegate.keepRunning = NO;
NSUserNotification* userNotification = [[NSUserNotification alloc] init];
BOOL isScheduled = NO;
userNotification.title = title;
if (![subtitle isEqualToString:@""]) {
userNotification.subtitle = subtitle;
}
userNotification.informativeText = message;
if (options[@"sound"] && ![options[@"sound"] isEqualToString:@""]) {
if ([options[@"sound"] isEqualToString:@"NSUserNotificationDefaultSoundName"]) {
userNotification.soundName = NSUserNotificationDefaultSoundName;
} else {
userNotification.soundName = options[@"sound"];
}
}
if (options[@"deliveryDate"] && ![options[@"deliveryDate"] isEqualToString:@""]) {
ncDelegate.keepRunning = YES;
double deliveryDate = [options[@"deliveryDate"] doubleValue];
NSDate* scheduleTime = [NSDate dateWithTimeIntervalSince1970:deliveryDate];
userNotification.deliveryDate = scheduleTime;
NSLog(@"Delivery date option passed as %@ converted to %f resulting in %@", options[@"deliveryDate"], deliveryDate, scheduleTime);
isScheduled = YES;
}
if (options[@"mainButtonLabel"] && ![options[@"mainButtonLabel"] isEqualToString:@""]) {
ncDelegate.keepRunning = YES;
userNotification.actionButtonTitle = options[@"mainButtonLabel"];
userNotification.hasActionButton = 1;
} else {
userNotification.hasActionButton = 0;
}
if (options[@"actions"] && ![options[@"actions"] isEqualToString:@""]) {
ncDelegate.keepRunning = YES;
[userNotification setValue:@YES forKey:@"_showsButtons"];
NSArray* myActions = [options[@"actions"] componentsSeparatedByString:@","];
if (myActions.count > 1) {
[userNotification setValue:@YES forKey:@"_alwaysShowAlternateActionMenu"];
[userNotification setValue:myActions forKey:@"_alternateActionButtonTitles"];
}
}
if (options[@"closeButtonLabel"] && ![options[@"closeButtonLabel"] isEqualToString:@""]) {
ncDelegate.keepRunning = YES;
[userNotification setValue:@YES forKey:@"_showsButtons"];
userNotification.otherButtonTitle = options[@"closeButtonLabel"];
}
if (options[@"response"] && ![options[@"response"] isEqualToString:@""]) {
ncDelegate.keepRunning = YES;
userNotification.hasReplyButton = 1;
userNotification.responsePlaceholder = options[@"mainButtonLabel"];
}
if (options[@"click"] && [options[@"click"] isEqualToString:@"yes"]) {
ncDelegate.keepRunning = YES;
ncDelegate.waitForClick = YES;
}
if (options[@"appIcon"] && ![options[@"appIcon"] isEqualToString:@""]) {
NSImage* icon = getImageFromURL(options[@"appIcon"]);
[userNotification setValue:icon forKey:@"_identityImage"];
[userNotification setValue:@(false) forKey:@"_identityImageHasBorder"];
}
if (options[@"contentImage"] && ![options[@"contentImage"] isEqualToString:@""]) {
userNotification.contentImage = getImageFromURL(options[@"contentImage"]);
}
if (options[@"asynchronous"] && [options[@"asynchronous"] isEqualToString:@"yes"]) {
ncDelegate.keepRunning = NO;
}
if (isScheduled) {
[notificationCenter scheduleNotification:userNotification];
} else {
[notificationCenter deliverNotification:userNotification];
}
[NSThread sleepForTimeInterval:0.1f];
while (ncDelegate.keepRunning) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
if (ncDelegate.actionData != NULL) {
return ncDelegate.actionData;
} else {
return [[NSDictionary alloc] init];
}
}
}