0

所以我试图根据iTunes是否正在运行来做一个if。我需要这个,因为我的应用程序获取了曲目名称。这个曲目名称已经完成并且可以工作,但我不希望 iTunes 一直运行......

所以我决定尝试一个带有 setLaunchPath 实例的 NSTask 来检查 iTunes.app 是否正在运行。下面的代码是不言自明的,但由于某种原因,如果 iTunes 打开,它会一直打到我的 else。我每 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");
}

}
4

1 回答 1

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.");
    }
} 


}
于 2012-05-02T01:26:25.960 回答