X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/b28cde9af5f0de2466cb62fa8e43c60fc58bf948..e740c3a87c5e9bcf4ef1751b37545fc616592e6c:/iTunesRemote.m diff --git a/iTunesRemote.m b/iTunesRemote.m index 27d346e..14e1ba3 100755 --- a/iTunesRemote.m +++ b/iTunesRemote.m @@ -25,6 +25,7 @@ - (BOOL)begin { iTunesPSN = [self iTunesPSN]; + asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype); //Register for application termination in NSWorkspace @@ -35,37 +36,40 @@ - (BOOL)halt { iTunesPSN.highLongOfPSN = kNoProcess; + CloseComponent(asComponent); //Unregister for application termination in NSWorkspace return YES; } -- (NSArray *)sources +- (NSArray *)playlists { - //This is probably unneeded - return nil; -} - -- (int)currentSourceIndex -{ - //This is probably unneeded - return nil; + int i; + int numPresets = [[self runScriptAndReturnResult:@"get number of playlists"] intValue]; + NSMutableArray *presets = [[NSMutableArray alloc] init]; + + for (i = 0; i < numPresets; i++) { + [presets addObject:[self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of playlist %i", i]]]; + } + + return [NSArray arrayWithArray:presets]; } -- (NSArray *)playlistsForCurrentSource +- (int)numberOfSongsInPlaylistAtIndex:(int)index { - //This is probably unneeded - return nil; + NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get number of tracks in playlist %i", index]]; + return [result intValue]; } -- (NSString *)sourceTypeOfCurrentPlaylist +- (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; } @@ -79,9 +83,10 @@ return result; } -- (NSString *)songTitleAtIndex +- (NSString *)songTitleAtIndex:(int)index { - return nil; + NSString *result = [self runScriptAndReturnResult:[NSString stringWithFormat:@"get name of track %i of current playlist", index]]; + return result; } - (int)currentSongIndex @@ -124,7 +129,7 @@ - (NSString *)currentSongLength { - return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pDur" + return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:[self iTunesPSN]]; } @@ -145,7 +150,25 @@ - (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]; +} + +- (int)currentEQPresetIndex +{ + int result; + result = [[ITAppleEventCenter sharedCenter] + sendTwoTierAEWithRequestedKeyForNumber:@"pidx" + fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" + appPSN:[self iTunesPSN]]; + return result; } - (BOOL)play @@ -176,36 +199,25 @@ return YES; } -- (BOOL)goToNextPlaylist -{ - //This is probably unneeded - return NO; -} - -- (BOOL)goToPreviousPlaylist -{ - //This is probably unneeded - return NO; -} - -- (BOOL)switchToSourceAtIndex:(int)index -{ - //This is probably unneeded - return NO; -} - - (BOOL)switchToPlaylistAtIndex:(int)index { + [self runScriptAndReturnResult:[NSString stringWithFormat: + @"play 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]]; + [self runScriptAndReturnResult:@"set EQ enabled to 1"]; return NO; } @@ -230,4 +242,35 @@ 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