Stripped out older stuff. It's better now :)
[MenuTunes.git] / iTunesRemote.m
index b688dac..5bab47e 100755 (executable)
@@ -14,7 +14,7 @@
 
 - (NSString *)information;
 {
-    return @"Default MenuTunes plugin to control iTunes.";
+    return @"Default MenuTunes plugin to control iTunes. Written by iThink Software.";
 }
 
 - (NSImage *)icon
 - (BOOL)begin
 {
     iTunesPSN = [self iTunesPSN];
+    
+    //We won't need this once we're pure AEs
     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;
 }
 
 - (BOOL)halt
 {
     iTunesPSN.highLongOfPSN = kNoProcess;
+    
+    //We won't need this once we're pure AEs
     CloseComponent(asComponent);
     
     //Unregister for application termination in NSWorkspace
+    [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
+    
     return YES;
 }
 
-- (int)numberOfPlaylists
+- (BOOL)isAppRunning
 {
-    NSString *result = [self runScriptAndReturnResult:@"get number of playlists"];
-    return [result intValue];
+    NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
+    int i;
+    
+    for (i = 0; i < [apps count]; i++) {
+        if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
+                isEqualToString:@"iTunes"]) {
+            return YES;
+        }
+    }
+    return NO;
+}
+
+- (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;
+    int numPresets = [[self runScriptAndReturnResult:@"get number of playlists"] intValue];
+    NSMutableArray *presets = [[NSMutableArray alloc] init];
+    for (i = 1; i <= numPresets; i++) {
+        [presets addObject:[self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of playlist %i", i]]];
+    }
+    
+    return [NSArray arrayWithArray:presets];
 }
 
 - (int)numberOfSongsInPlaylistAtIndex:(int)index
 
 - (NSString *)songTitleAtIndex:(int)index
 {
-    NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i", index]];
+    NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of track %i of current playlist", index]];
     return result;
 }
 
 
 - (NSString *)currentSongLength
 {
-    return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pDur"
+    return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim"
                 fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd"
                 appPSN:[self iTunesPSN]];
 }
     int numPresets = [[self runScriptAndReturnResult:@"get number of EQ presets"] intValue];
     NSMutableArray *presets = [[NSMutableArray alloc] init];
     
-    for (i = 0; i < numPresets; i++) {
+    for (i = 1; i <= numPresets; i++) {
         [presets addObject:[self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of EQ preset %i", i]]];
     }
     
     return [NSArray arrayWithArray:presets];
 }
 
+- (int)currentEQPresetIndex
+{
+    int result;
+    result = [[ITAppleEventCenter sharedCenter]
+                sendTwoTierAEWithRequestedKeyForNumber:@"pidx"
+                fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"
+                appPSN:[self iTunesPSN]];
+    return result;
+}
+
 - (BOOL)play
 {
     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play"
 
 - (BOOL)goToNextSong
 {
-    NSLog(@"%@", [self classOfPlaylistAtIndex:3]);
-    //[[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next"
-    //        appPSN:[self iTunesPSN]];
+    [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next"
+            appPSN:[self iTunesPSN]];
     return YES;
 }
 
     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:
-        @"set current playlist to playlist %i", index]];
+        @"play playlist %i", index]];
     return NO;
 }
 
 {
     [self runScriptAndReturnResult:[NSString stringWithFormat:
         @"set current EQ preset to EQ preset %i", index]];
+    [self runScriptAndReturnResult:@"set EQ enabled to 1"];
     return NO;
 }
 
     return number;
 }
 
+- (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];
+    }
+}
+
+//This is just temporary
 - (NSString *)runScriptAndReturnResult:(NSString *)script
 {
     AEDesc scriptDesc, resultDesc;