2012-04-30 49 views
0
isRunning

所以我想如果iTunes正在運行或不作出是否根據。我需要這個,因爲我的應用程序獲取曲目名稱。這條賽道的名字已經完成和工程,但我不希望iTunes的始終運行...的iTunes NSTask

所以我決定嘗試用NSTask例如setLaunchPath檢查iTunes.app isRunning。下面的代碼是自我解釋的,但是由於某種原因,如果iTunes打開時它會一直打我。我在每分鐘5秒的時間內用nstimer在我的awakeFromnib中調用這個方法。

-(IBAction)ifRunning:(id)pID; { 
NSTask *task = [[NSTask alloc] init]; 
[task setLaunchPath:@"/Applications/iTunes.app/Contents/MacOS/iTunes"]; 
if ([task isRunning]==TRUE) { 
    NSLog(@"iTunes is Running, hit if"); 
    NSString *track = ([self getCurrentTrack]); 
    //getCurrentTrack another one of my methods 


} 

else if ([task isRunning] == FALSE) { 
    NSLog(@"iTunes is not running, hit else if");\ 
    [trackName setTitle:(@"iTunes is Not Playing")]; 

} 

else { 
    NSLog(@"Hit else"); 
} 

} 

回答

0

我想通了下面的代碼。希望這篇文章很有用!

-(IBAction)ifRunning:(id)pID; { 
NSLog(@"Process check "); 
NSWorkspace *ws = [NSWorkspace sharedWorkspace]; 
NSArray *runningAppDictionaries = [ws launchedApplications]; 
NSDictionary *aDictionary; 

for (aDictionary in runningAppDictionaries) 
{ 
    // NSLog(@"Open App: %@", [aDictionary valueForKey:@"NSApplicationName"]); 

    if ([[aDictionary valueForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) 
    { 
     NSLog(@"iTunes is Is Running"); 
     [self getCurrentTrack:nil]; 
     break; 
    } 

    else { 
     NSLog(@"iTunes is not running."); 
    } 
} 


}