Changing hotkeys in the prefs now writes to the prefs and rebuilds the menu.
[MenuTunes.git] / iTunesRemote.m
index b89ccec..1ab3043 100755 (executable)
 - (BOOL)begin
 {
     iTunesPSN = [self iTunesPSN];
-    
-    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
-    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
-    
     return YES;
 }
 
 - (BOOL)showPrimaryInterface
 {
     // Make this into AppleEvents... shouldn't be too hard, I'm just too tired to do it right now.
-    //tell application "iTunes"
-    // set frontmost to true
+    [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:long(1), '----':obj { form:'prop', want:type('prop'), seld:type('pisf'), from:'null'() }" eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
+    // Still have to convert these to AEs:
     // set visible of browser window 1 to true
+    [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:true($$), ----:obj { form:'prop', want:'prop', seld:'pvis', from:obj { form:'indx', want:'cBrW', seld:1, from:'null'() } }" eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
     // set minimized of browser window 1 to false
-    // set view of browser window 1 to (playlist (index of current playlist))
-    //end tell
+    [[ITAppleEventCenter sharedCenter] sendAEWithSendString:@"data:fals($$), ----:obj { form:'prop', want:'prop', seld:'pMin', from:obj { form:'indx', want:'cBrW', seld:1, from:'null'() } }" eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
+
     return NO;
 }
 
     return [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }",index] eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
 }
 
+- (int)currentAlbumTrackCount
+{
+    return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pTrC" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
+}
+
+- (int)currentSongTrack
+{
+    return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pTrN" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
+}
+
 - (NSString *)currentSongUniqueIdentifier
 {
-    return @"BBQ";
+    return [NSString stringWithFormat:@"%i-%i", [self currentPlaylistIndex], [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pDID" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]];
 }
 
 - (int)currentSongIndex
 - (float)currentSongRating
 {
     return [[ITAppleEventCenter sharedCenter]
-                sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN] / 100;
+                sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN] / 100.0;
 }
 
 - (BOOL)setCurrentSongRating:(float)rating
     return YES;
 }
 
-- (BOOL)equalizerEnabled
+/* - (BOOL)equalizerEnabled
 {
-    return [[ITAppleEventCenter sharedCenter]
-                        sendAEWithRequestedKeyForNumber:@"pEQ " eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
+    int thingy = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:type('prop'), want:type('prop'), seld:type('pEQ '), from:() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
+    NSLog(@"Debug equalizerEnabled: %i", thingy);
+    return thingy;
 }
 
 - (BOOL)setEqualizerEnabled:(BOOL)enabled
 {
-[[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }",enabled] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
-}
+    [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }",enabled] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
+    return YES;
+} */
 
 - (NSArray *)eqPresets
 {
 - (BOOL)setShuffleEnabled:(BOOL)enabled
 {
     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu) ----:obj { form:'prop', want:type('prop'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",enabled] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
+    return YES;
 }
 
 - (ITMTRemotePlayerRepeatMode)repeatMode
 {
-    FourCharCode m00f;
-    int result;
+    FourCharCode m00f = 0;
+    int result = 0;
     m00f = [[ITAppleEventCenter sharedCenter]
                 sendTwoTierAEWithRequestedKeyForNumber:@"pRpt" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
 
 
 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
 {
-    FourCharCode m00f;
+    FourCharCode m00f = 0;
     switch (repeatMode)
           {
           case ITMTRemotePlayerRepeatOff:
           }
 
     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu) ----:obj { form:'prop', want:type('pRpt'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",m00f] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
-
+    return YES;
 }
 
 - (BOOL)play
     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];
-    }
-}
-
 @end