是否可以使用可可隐藏一个特定的应用程序?
我知道您可以使用以下代码隐藏所有其他应用程序
[[NSWorkspace sharedWorkspace] performSelectorOnMainThread:@selector(hideOtherApplications) withObject:NULL waitUntilDone:NO];
但是是否可以只隐藏一个特定的应用程序,例如 Safari?
如果您的目标是 Mac OS 10.6+,则可以使用新NSRunningApplication
类:
- (BOOL) hideAppWithBundleID:(NSString *)bundleID
{
NSArray *apps = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleID];
if ([apps count] == 0)
return NO;
return [(NSRunningApplication *)[apps objectAtIndex:0] hide];
}
你可以用applescript做到这一点:
tell application "System Events" to set visible of process "Safari" to false
或通过调用从可可中调用相同的applescript:
NSString * source = @"tell application \"System Events\" to set visible of process \"Safari\" to false";
NSAppleScript * script = [[NSAppleScript alloc] initWithSource:source];
[script executeAndReturnError:nil];
[script release];
或者,如果您想避免使用 Apple Script 并使用捆绑标识符而不是可以本地化的应用程序名称,如 Mike 指出的:
for (NSDictionary *app in [[NSWorkspace sharedWorkspace] launchedApplications])
{
if ([@"com.apple.Safari" isEqualToString:[app objectForKey:@"NSApplicationBundleIdentifier"]])
{
ProcessSerialNumber psn;
GetCurrentProcess(&psn); // Initialize the Process Manager
psn.highLongOfPSN = [[app objectForKey:@"NSApplicationProcessSerialNumberHigh"] intValue];
psn.lowLongOfPSN = [[app objectForKey:@"NSApplicationProcessSerialNumberLow"] intValue];
ShowHideProcess(&psn, NO);
}
}