Re: Checking One Array Against Another?
Re: Checking One Array Against Another?
- Subject: Re: Checking One Array Against Another?
- From: Rob Keniger <email@hidden>
- Date: Sat, 29 Nov 2008 15:34:40 +1000
On 29/11/2008, at 1:53 PM, Pierce Freeman wrote:
Hi everyone.
I am attempting to check one array's contents against another's, but
so far
it hasn't been working. In the actual application, I am checking the
current open applications against an "okay" application list, and just
thought of another problem: If the user chooses only has some open
on the
list, it will return that the user has some open that is not on the
list.
Some example code (with different variables) is below:
I wrote a small app a while back that monitors application launches
and kills apps in a blacklist, which sounds similar to what you are
doing. In your case you'd just do the reverse and create an
allowedApplications set and if allowedApplications did not contain a
bundle identifier for the app being launched, you'd kill it.
Simplified code is below (edited in Mail):
@interface AppKiller
{
NSSet* blockedApplications;
}
@end
@implementation AppKiller
- (id) init
{
self = [super init];
if (self != nil) {
blockedApplications=[NSSet
setWithObjects:@"com.apple.iTunes",@"com.apple.Safari",nil];
[self checkApps:nil];
//register for app launched notifications
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(checkApps:)
name:NSWorkspaceDidLaunchApplicationNotification object:nil];
}
return self;
}
-(void) dealloc
{
[[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:
self];
[blockedApplications release];
[super dealloc];
}
//check that launched app is not blocked
-(void) checkApps:(NSNotification*) notification
{
NSArray* launchedApps=[[NSWorkspace sharedWorkspace]
launchedApplications];
for(NSDictionary* appInfo in launchedApps)
{
if([blockedApplications containsObject:[appInfo
objectForKey:@"NSApplicationBundleIdentifier"]])
{
NSLog(@"Killing blocked application (%@) with PID %@",[appInfo
objectForKey:@"NSApplicationName"],[appInfo
objectForKey:@"NSApplicationProcessIdentifier"]);
[self quitApplicationWithBundleID:[appInfo
objectForKey:@"NSApplicationBundleIdentifier"]];
}
}
}
//send Quit Apple Event
-(OSStatus)quitApplicationWithBundleID:(NSString *)bundleID {
OSStatus err;
AppleEvent event, reply;
const char *bundleIDString = [bundleID UTF8String];
err = AEBuildAppleEvent(kCoreEventClass, kAEQuitApplication,
typeApplicationBundleID,
bundleIDString, strlen(bundleIDString),
kAutoGenerateReturnID, kAnyTransactionID,
&event, NULL, "");
if (err == noErr) {
err = AESendMessage(&event, &reply, kAENoReply,
kAEDefaultTimeout);
(void)AEDisposeDesc(&event);
}
return err;
}
@end;
--
Rob Keniger
_______________________________________________
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