Finished the plugin using my runScriptAndReturnResult method.
authorKent Sutherland <ksuther@ithinksw.com>
Fri, 14 Feb 2003 04:21:05 +0000 (04:21 +0000)
committerKent Sutherland <ksuther@ithinksw.com>
Fri, 14 Feb 2003 04:21:05 +0000 (04:21 +0000)
ITMTRemote.h
ITMTRemote.m
MenuTunes.m
iTunesRemote.h
iTunesRemote.m

index e4d3019..be50925 100755 (executable)
@@ -77,6 +77,7 @@
  */
 - (BOOL)halt;
 
+- (int)numberOfPlaylists;
 - (int)numberOfSongsInPlaylistAtIndex:(int)index;
 - (NSString *)classOfPlaylistAtIndex:(int)index;
 - (int)currentPlaylistIndex;
index bdc2dda..780ce4c 100755 (executable)
     return NO;
 }
 
+- (int)numberOfPlaylists
+{
+    return 0;
+}
+
 - (int)numberOfSongsInPlaylistAtIndex:(int)index
 {
     return 0;
index 884af16..2a584ee 100755 (executable)
@@ -45,6 +45,8 @@ Things to do:
 - (void)applicationDidFinishLaunching:(NSNotification *)note
 {
     currentRemote = [self loadRemote];
+    [currentRemote begin];
+    
     asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
 
     [self registerDefaultsIfNeeded];
@@ -767,7 +769,6 @@ andEventID:(AEEventID)eventID
 
 - (void)playPause:(id)sender
 {
-    NSString *rawr;
     NSString *state = [self runScriptAndReturnResult:@"return player state"];
     if ([state isEqualToString:@"playing"]) {
         [self sendAEWithEventClass:'hook' andEventID:'Paus'];
index 1599e2d..79821c6 100755 (executable)
@@ -8,13 +8,15 @@
 
 
 #import <Cocoa/Cocoa.h>
+#import <Carbon/Carbon.h>
 #import <ITMTRemote/ITMTRemote.h>
 #import <ITFoundation/ITFoundation.h>
 
-
 @interface iTunesRemote : ITMTRemote <ITMTRemote>
 {
+    ComponentInstance asComponent;
     ProcessSerialNumber iTunesPSN;
 }
 - (ProcessSerialNumber)iTunesPSN;
+- (NSString *)runScriptAndReturnResult:(NSString *)script;
 @end
index 5978135..b688dac 100755 (executable)
@@ -25,6 +25,7 @@
 - (BOOL)begin
 {
     iTunesPSN = [self iTunesPSN];
+    asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
     
     //Register for application termination in NSWorkspace
     
 - (BOOL)halt
 {
     iTunesPSN.highLongOfPSN = kNoProcess;
+    CloseComponent(asComponent);
     
     //Unregister for application termination in NSWorkspace
     return YES;
 }
 
+- (int)numberOfPlaylists
+{
+    NSString *result = [self runScriptAndReturnResult:@"get number of playlists"];
+    return [result intValue];
+}
+
 - (int)numberOfSongsInPlaylistAtIndex:(int)index
 {
-    return [[ITAppleEventCenter sharedCenter]
-                    sendAEWithSendStringForNumber:@"cPla"
-                    eventClass:@"core" eventID:@"cnte"
-                    appPSN:[self iTunesPSN]];
+    NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get number of tracks in playlist %i", index]];
+    return [result intValue];
 }
 
 - (NSString *)classOfPlaylistAtIndex:(int)index
 {
     //Not working yet. It returns the 4 character code instead of a name.
-    NSString *result;
+    /*NSString *result;
     result = [[ITAppleEventCenter sharedCenter]
                 sendTwoTierAEWithRequestedKey:@"pcls"
                 fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd"
-                appPSN:[self iTunesPSN]];
+                appPSN:[self iTunesPSN]];*/
+    NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get class of playlist %i", index]];
     return result;
 }
 
@@ -71,7 +78,8 @@
 
 - (NSString *)songTitleAtIndex:(int)index
 {
-    return nil; 
+    NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"return name of track %i", index]];
+    return result;
 }
 
 - (int)currentSongIndex
 
 - (NSArray *)eqPresets;
 {
-    return nil;
+    int i;
+    int numPresets = [[self runScriptAndReturnResult:@"get number of EQ presets"] intValue];
+    NSMutableArray *presets = [[NSMutableArray alloc] init];
+    
+    for (i = 0; i < numPresets; i++) {
+        [presets addObject:[self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of EQ preset %i", i]]];
+    }
+    
+    return [NSArray arrayWithArray:presets];
 }
 
 - (BOOL)play
 
 - (BOOL)goToNextSong
 {
-    [self numberOfSongsInPlaylistAtIndex:1];
+    NSLog(@"%@", [self classOfPlaylistAtIndex:3]);
     //[[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next"
     //        appPSN:[self iTunesPSN]];
     return YES;
 
 - (BOOL)switchToPlaylistAtIndex:(int)index
 {
+    [self runScriptAndReturnResult:[NSString stringWithFormat:
+        @"set current playlist to playlist %i", index]];
     return NO;
 }
 
 - (BOOL)switchToSongAtIndex:(int)index
 {
+    [self runScriptAndReturnResult:[NSString stringWithFormat:
+        @"play track %i of current playlist", index]];
     return NO;
 }
 
 - (BOOL)switchToEQAtIndex:(int)index
 {
+    [self runScriptAndReturnResult:[NSString stringWithFormat:
+        @"set current EQ preset to EQ preset %i", index]];
     return NO;
 }
 
     return number;
 }
 
+- (NSString *)runScriptAndReturnResult:(NSString *)script
+{
+    AEDesc scriptDesc, resultDesc;
+    Size length;
+    NSString *result;
+    Ptr buffer;
+    
+    script = [NSString stringWithFormat:@"tell application \"iTunes\"\n%@\nend tell", script];
+    
+    AECreateDesc(typeChar, [script cString], [script cStringLength], 
+&scriptDesc);
+    
+    OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
+    
+    length = AEGetDescDataSize(&resultDesc);
+    buffer = malloc(length);
+    
+    AEGetDescData(&resultDesc, buffer, length);
+    AEDisposeDesc(&scriptDesc);
+    AEDisposeDesc(&resultDesc);
+    result = [NSString stringWithCString:buffer length:length];
+    if ( (! [result isEqualToString:@""])      &&
+         ([result characterAtIndex:0] == '\"') &&
+         ([result characterAtIndex:[result length] - 1] == '\"') ) {
+        result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
+    }
+    free(buffer);
+    buffer = nil;
+    return result;
+}
+
 @end