Naoto Ida
Naoto Ida

Reputation: 1295

Checking if item exists in status bar?

Is it possible to check whether or not a system item is visible in the status bar on OSX, i.e. the Bluetooth icon?

Has anyone tried doing this before? No mentions on the documentation what so ever.

Upvotes: 2

Views: 79

Answers (1)

vadian
vadian

Reputation: 285180

The paths of the active menu bar items are listed in ~/Library/Preferences/com.apple.systemuiserver.plist

You can check this way

NSURL *userLibraryURL = [[NSFileManager defaultManager] URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *systemUIServerPreferences = [userLibraryURL URLByAppendingPathComponent:@"Preferences/com.apple.systemuiserver.plist"];
NSDictionary *data = [NSDictionary dictionaryWithContentsOfURL:systemUIServerPreferences];
BOOL bluetoothIsInMenuBar = [data[@"menuExtras"] containsObject:@"/System/Library/CoreServices/Menu Extras/Bluetooth.menu"];

NSLog(@"%d", bluetoothIsInMenuBar);

or using NSPredicate

NSURL *userLibraryURL = [[NSFileManager defaultManager] URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *systemUIServerPreferences = [userLibraryURL URLByAppendingPathComponent:@"Preferences/com.apple.systemuiserver.plist"];
NSDictionary *data = [NSDictionary dictionaryWithContentsOfURL:systemUIServerPreferences];
NSPredicate *bluetoothPredicate = [NSPredicate predicateWithFormat:@"SELF contains 'Bluetooth'"];
BOOL bluetoothIsInMenuBar = [[data[@"menuExtras"] filteredArrayUsingPredicate:bluetoothPredicate] count];        

NSLog(@"%d", bluetoothIsInMenuBar);

Upvotes: 2

Related Questions