Re: How to tell if iTunes is running.
Re: How to tell if iTunes is running.
- Subject: Re: How to tell if iTunes is running.
- From: has <email@hidden>
- Date: Sat, 24 May 2008 23:40:34 +0100
Steve Christensen wrote:
Would something like this work better? It should deal with
localization or if the user renames iTunes for some reason.
iTunesIsOpen = NO;
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
NSString* iTunesPath = [workspace
absolutePathForAppBundleWithIdentifier:@"com.apple.iTunes"];
NSArray* lApplications = [workspace launchedApplications];
int lAppsCount = [lApplications count];
int a;
for (a = 0; a < lAppsCount; a++)
{
NSDictionary* applicationD = [lApplications objectAtIndex:a];
if ([[applicationD objectForKey:@"NSApplicationPath"]
isEqualToString:iTunesPath])
{
iTunesIsOpen = YES;
break;
}
}
Yeah, bundle IDs are a good habit. Here's a slightly cleaner, case-
insensitive version of the above:
BOOL IsRunning(NSString *bundleID) {
NSEnumerator *appsList = [[[NSWorkspace sharedWorkspace]
launchedApplications] objectEnumerator];
NSDictionary *appInfo;
bundleID = [bundleID lowercaseString];
while (appInfo = [appsList nextObject]) {
if ([[[appInfo objectForKey: @"NSApplicationBundleIdentifier"]
lowercaseString] isEqual: bundleID])
return YES;
}
return NO;
}
Or just use one of the ObjC-AppleEvent bridges to do everything. For
example, using objc-appscript:
#import <Foundation/Foundation.h>
#import "ITGlue/ITGlue.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ITApplication *itunes = [[ITApplication alloc] initWithBundleID:
@"com.apple.itunes"];
NSLog(@"Is running: %i", [itunes isRunning]);
[[itunes run] send];
NSLog(@"Is running: %i", [itunes isRunning]);
[[itunes quit] send];
while([itunes isRunning]) {
printf("Waiting while iTunes quits...\n");
sleep(1);
}
NSLog(@"Is running: %i", [itunes isRunning]);
[itunes release];
[pool release];
return 0;
}
HTH
has
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden