App did launch/quit notifications in the plugin now
[MenuTunes.git] / iTunesRemote.m
index 14e1ba3..3381cb3 100755 (executable)
@@ -28,6 +28,8 @@
     asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
     
     //Register for application termination in NSWorkspace
+    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
+    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
     
     NSLog(@"iTunes Plugin loaded");
     return YES;
     CloseComponent(asComponent);
     
     //Unregister for application termination in NSWorkspace
+    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
+    
     return YES;
 }
 
+- (void)applicationLaunched:(NSNotification *)note
+{
+    NSDictionary *info = [note userInfo];
+    
+    if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
+        iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
+        iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
+        
+        [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
+    }
+}
+
+- (void)applicationTerminated:(NSNotification *)note
+{
+    NSDictionary *info = [note userInfo];
+    
+    if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
+        iTunesPSN.highLongOfPSN = kNoProcess;
+        [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
+    }
+}
+
+- (PlayerState)playerState
+{
+    NSString *result = [self runScriptAndReturnResult:@"get player state"];
+    
+    if ([result isEqualToString:@"playing"]) {
+        return playing;
+    } else if ([result isEqualToString:@"paused"]) {
+        return paused;
+    } else if ([result isEqualToString:@"stopped"]) {
+        return stopped;
+    } else if ([result isEqualToString:@"rewinding"]) {
+        return rewinding;
+    } else if ([result isEqualToString:@"fast forwarding"]) {
+        return forwarding;
+    }
+    
+    return stopped;
+}
+
 - (NSArray *)playlists
 {
     int i;
     return YES;
 }
 
+- (BOOL)fastForward
+{
+    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast"
+            appPSN:[self iTunesPSN]];
+    return YES;
+}
+
+- (BOOL)rewind
+{
+    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd"
+            appPSN:[self iTunesPSN]];
+    return YES;
+}
+
+
 - (BOOL)switchToPlaylistAtIndex:(int)index
 {
     [self runScriptAndReturnResult:[NSString stringWithFormat: