diff --git a/windowDetector/main.m b/windowDetector/windowDetector.h similarity index 50% rename from windowDetector/main.m rename to windowDetector/windowDetector.h index 7980e6f..d3122ba 100644 --- a/windowDetector/main.m +++ b/windowDetector/windowDetector.h @@ -1,5 +1,5 @@ // -// main.m +// windowDetector.h // windowDetector // // Created by Jon Lara Trigo on 28/04/2020. @@ -8,10 +8,10 @@ #import -int main(int argc, const char * argv[]) { - @autoreleasepool { - // insert code here... - NSLog(@"Hello, World!"); - } - return 0; -} +NS_ASSUME_NONNULL_BEGIN + +@interface windowDetector : NSObject + +@end + +NS_ASSUME_NONNULL_END diff --git a/windowDetector/windowDetector.m b/windowDetector/windowDetector.m new file mode 100644 index 0000000..491c3f4 --- /dev/null +++ b/windowDetector/windowDetector.m @@ -0,0 +1,58 @@ +// +// windowDetector.m +// windowDetector +// +// Created by Jon Lara Trigo on 28/04/2020. +// Copyright © 2020 Jon Lara Trigo. All rights reserved. +// + +#import "windowDetector.h" +#import +#import + +#define NSLog(FORMAT, ...) fprintf(stderr, "%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]); + +@implementation windowDetector + +- (id) init + { + self = [super init]; + if (!self) + return self; + [[[NSWorkspace sharedWorkspace] notificationCenter] + addObserver:self + selector:@selector(getInfo:) + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; + + [[[NSWorkspace sharedWorkspace] notificationCenter] + addObserver:self + selector:@selector(getInfo:) + name:NSWorkspaceDidTerminateApplicationNotification object:nil]; + + return self; + } + +- (void) getInfo:(NSNotification *)d + { + NSRunningApplication *app = d.userInfo[NSWorkspaceApplicationKey]; + + NSDate * now = [NSDate date]; + NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init]; + [outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; + NSString *dateString = [outputFormatter stringFromDate:now]; + + NSLog(@"{\"name\": \"%@\", \"time\": \"%@\", \"bundleIdentifier\": \"%@\",\"launchDate\": \"%@\",\"PID\": \"%d\", \"isTerminated\": %hhd}", [app localizedName], dateString, [app bundleIdentifier], [app launchDate],[app processIdentifier], [app isTerminated]); + } + +int main(int argc, const char * argv[]) { + @autoreleasepool + { + NSRunLoop *runLoop = NSRunLoop.currentRunLoop; + windowDetector *w = [[windowDetector alloc] init]; + while ([runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:5]]); + }; + return 0; + +} +@end