Browse Source

App to monitor the frontmost application

Zachary Kotlarek 13 years ago
parent
commit
603bdbb663
3 changed files with 85 additions and 0 deletions
  1. 7 0
      frontApp/Makefile
  2. 24 0
      frontApp/com.uberzach.frontApp.plist
  3. 54 0
      frontApp/frontApp.m

+ 7 - 0
frontApp/Makefile

@@ -0,0 +1,7 @@
+default: frontApp
+
+frontApp: frontApp.m
+	gcc -o frontApp -framework Cocoa frontApp.m
+
+clean:
+	rm -f frontApp

+ 24 - 0
frontApp/com.uberzach.frontApp.plist

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>LimitLoadToSessionType</key>
+	<string>Aqua</string>
+	<key>LimitLoadToHosts</key>
+	<array>
+		<string>beddy.uberzach.com</string>
+		<string>loki.uberzach.com</string>
+		<string>heady.uberzach.com</string>
+	</array>
+	<key>RunAtLoad</key>
+	<true/>
+	<key>Label</key>
+	<string>com.uberzach.frontApp</string>
+	<key>KeepAlive</key>
+	<true/>
+	<key>ProgramArguments</key>
+	<array>
+		<string>/Users/tv/bin/video/frontApp/frontApp</string>
+	</array>
+</dict>
+</plist>

+ 54 - 0
frontApp/frontApp.m

@@ -0,0 +1,54 @@
+#import <Cocoa/Cocoa.h>
+
+@interface MDAppController : NSObject <NSApplicationDelegate>
+@property (retain) NSString *outfile;
+@end
+
+@implementation MDAppController 
+- (id)init {
+    if ((self = [super init])) {
+        [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
+                      selector:@selector(activeAppDidChange:)
+               name:NSWorkspaceDidActivateApplicationNotification object:nil];
+    }
+    return self;
+}
+- (void)dealloc {
+    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
+    [super dealloc];
+}
+- (void)activeAppDidChange:(NSNotification *)notification {
+    NSRunningApplication* runningApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
+	NSString *str = [NSString stringWithFormat:@"%@\n", runningApp.bundleIdentifier];
+	if (![str writeToFile:[self outfile] atomically:YES encoding:NSStringEncodingConversionAllowLossy error:nil]) {
+		NSLog(@"Unable to write to path: %@", [self outfile]);
+	}
+}
+@end
+
+int main(int argc, const char * argv[]) {
+    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+    [NSApplication sharedApplication];
+
+    MDAppController *appController = [[MDAppController alloc] init];
+    [NSApp setDelegate:appController];
+
+	// Construct a default path or use the provided one
+	if (argc > 1) {
+		NSString *str = [NSString stringWithUTF8String: argv[1]];
+		[appController setOutfile:str];
+	} else {
+		NSString *tmpDir = NSTemporaryDirectory();
+		[appController setOutfile:[NSString stringWithFormat:@"%@plexMonitor/frontApp", tmpDir]];
+	}
+
+	// Force an update at init
+	[appController activeAppDidChange:nil];
+
+	// Wait forever
+    [NSApp run];
+
+	// Never reached
+    [pool release];
+	return 0;
+}