X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/8c2086efaf1ee7419fc91275fdc7426de3fbe7fd..796f8360ac05b39085aef9f202f635a04ba6834e:/iTunesRemote.m diff --git a/iTunesRemote.m b/iTunesRemote.m index 8745044..5bab47e 100755 --- a/iTunesRemote.m +++ b/iTunesRemote.m @@ -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 @@ -25,23 +25,44 @@ - (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; } +- (BOOL)isAppRunning +{ + 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"]; @@ -66,8 +87,7 @@ int i; int numPresets = [[self runScriptAndReturnResult:@"get number of playlists"] 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 playlist %i", i]]]; } @@ -173,7 +193,7 @@ 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]]]; } @@ -276,6 +296,29 @@ 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;